UNC Path
Universal Naming Convention (UNC) is a method of displaying a path that will work for everyone connected to your network. Its yang is mapped drives, which are specific to each machine. For example, I have a server named Server1 and I’ve mapped my Q drive to the Accounting folder under Server1. I could access that folder using the UNC method
\\Server1\Accounting\MyBook.xls
or the mapped drive method
Q:\MyBook.xls
If I want my co-worker to open MyBook, she would have to have ‘Q’ mapped the same as me in order to use Q:\MyBook.xls, but the UNC method will work for her and everyone else.
I recently had to convert mapped drives to UNC paths in a VBA project. Actually, I had to compare the .Path property of the ActiveWorkbook to a saved UNC path to determine if the workbook was saved in the proper folder. If the user opened the ActiveWorkbook by navigating through his mapped drive, the .Path property was guaranteed not to match (from a string comparison perspective), and the program would assume that the workbook was saved improperly.
My quick fix was to no longer compare the paths, but compare the files in each of the paths. I include it here only for information, not because you should ever use this method. It got the job done, however, until I could implement a more robust method. I used a three part test to determine if the ActiveWorkbook was saved in my predetermined path: First make sure the workbook was saved (Len(ActiveWorkbook.Path)>0), then make sure the file exists in the path (Len(Dir(gsPATH & ActiveWorkbook.Name))>0), and last make sure the first file in each of the paths was the same (Dir(gsPath, vbNormal)=Dir(ActiveWorkbook.Path & "\", vbNormal)). It’s easy to see that this is fraught with danger, but it was quick and was a nice band aid.
Next I went to Karl Peterson’s UncName Page to learn about the API I needed. Karl has a lot of stuff in there that I don’t need, but it was very instructive to read through his code.
Today, I read a post at SpyJournal about sending links via email instead of attaching documents. It was a great post and it’s a good use of the Web toolbar (the only good use to date, by the way). It got me thinking that the Address control on the Web toolbar could be used to get the UNC path for a workbook. The workbook would have to be open and active, but if that is the case, it’s a simple way to get the UNC path.
From the Immediate Window:
?application.CommandBars("Web").Controls("Address:").text
\\Server1\AllCompanies\DKInc\Production\APM1553Purchase.xls
?activeworkbook.FullName
S:\DKInc\Production\APM1553Purchase.xls
Emily:
List mapped drive
Sub NetworkMapDrive()
Set WshNetwork = CreateObject(”WScript.Network”)
21 June 2006, 10:36 pmSet oDrives = WshNetwork.EnumNetworkDrives
DrivesStr = “Network drive Mappings:” & Chr(13)
For i = 0 To oDrives.Count - 1 Step 2
DrivesStr = DrivesStr & “Drive ” & oDrives.Item(i) & ” = ” & oDrives.Item(i + 1) & Chr(13)
Next
MsgBox DrivesStr
End Sub
Dick Kusleika:
Thanks Emily, that’s cool.
22 June 2006, 7:02 amRichard Schollar:
Emily
That was a useful piece of code - thank you very much. Good topic too, Dick
Richard
23 June 2006, 1:43 amDoug Glancy:
Emily,
I just ran into a problem that your code helped solve. Thanks, and thanks Dick for bringing up the topic.
10 July 2006, 4:11 pmTim:
Thanks for the link
Here is a another post that Doug (who contributes on my tech tips site) has written
22 August 2006, 8:04 amThis has very good in depth information about just how UNC paths work
http://www.spyjournal.biz/techtips/2004/10/slashes-in-urls.html
Simon:
Emily,
I am having problems getting your code to run -
Compile error: Syntax error
This happens on the following lines (both are coloured red in the VBA editor):
DrivesStr = “Network drive Mappings:” & Chr(13)
DrivesStr = DrivesStr & “Drive ” & oDrives.Item(i) & ” = ” & oDrives.Item(i + 1) & Chr(13)
Anyone else have this problem?
Thanks,
Simon
4 September 2006, 2:36 amSimon:
Sorry, ignore last comment - Where I had copied and pasted the code from the page the ” characters needed to be replaced in the VBA editor…
Thanks!
4 September 2006, 2:38 amAli:
I want this code in C#. I am not getting the text property of my commandbar button. how to handle it in C#?
19 March 2007, 12:43 amDiegoAndresJAY:
Emily is my hero!!!
11 July 2007, 2:09 pmJon:
I need to do the opposite of what you did in your article.
Our IT company keeps making changes & the UNC links in Excel become invalid because of it.
We need to use the mapped drive letter system so when the IT guys change things they don’t mess us up.
The only kind of link I’ve been able to put into Excel is a UNC, I can browse to the mapped drive letter when I insert the link, but after I’m finished it automatically converts it into a UNC.
How do I keep this from happening? I’m using Excel 2003 SP1
Thanks in advance.
5 March 2008, 8:32 am