Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

Using %userprofile% in macro that changes path

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cissy - 13 Sep 2005 22:59 GMT
Hi,  thanks for your help.

I'm using Word XP.  I have one line of code that works:

Options.DefaultFilePath(Path:=wdWorkgroupTemplatesPath) = "C:\Program
Files\Microsoft Office\Templates\IRTemplates"

and one that does not work:

Options.DefaultFilePath(Path:=wdUserTemplatesPath) =
"%Userprofile%\application data\microsoft\Templates"

I've checked the paths and all the folders are there.  Is there any reason
why %Userprofile% shouldn't work?  Thanks for your help
Jay Freedman - 14 Sep 2005 04:35 GMT
>Hi,  thanks for your help.
>
[quoted text clipped - 10 lines]
>I've checked the paths and all the folders are there.  Is there any reason
>why %Userprofile% shouldn't work?  Thanks for your help

Why should it work? First, variables delimited by % signs in batch
files refer to environment variables, and there isn't any such
environment variable in a default Windows installation. Second, VBA
doesn't interpret the % signs the way batch files do. It just isn't
supported in VBA.

Instead, you can include the following function in your template. It
uses the PrivateProfileString function to extract the needed
information from the registry.

Function UserProfile() As String
   Const RegPath = "HKEY_CURRENT_USER\Volatile Environment"
   Dim HomeDrive As String, HomePath As String
   
   HomeDrive = System.PrivateProfileString("", _
       RegPath, "HOMEDRIVE")
   HomePath = System.PrivateProfileString("", _
       RegPath, "HOMEPATH")
   UserProfile = HomeDrive & HomePath
End Function

You could use it like this:

 Options.DefaultFilePath(Path:=wdUserTemplatesPath) = _
    Userprofile & "\application data\microsoft\Templates"

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Jay Freedman - 14 Sep 2005 15:04 GMT
>> Hi,  thanks for your help.
>>
[quoted text clipped - 36 lines]
>   Options.DefaultFilePath(Path:=wdUserTemplatesPath) = _
>      Userprofile & "\application data\microsoft\Templates"

Now that I've had a chance to look at a system in a networked environment,
that function may not return the correct path, depending on how the login
script is written. Use this one instead:

Function UserProfile() As String
   Const RegPath = "HKEY_CURRENT_USER\Volatile Environment"

   UserProfile = System.PrivateProfileString("", _
         RegPath, "APPDATA")
End Function

and then change the calling routine to use this:

  Options.DefaultFilePath(Path:=wdUserTemplatesPath) = _
     Userprofile & "\Microsoft\Templates"

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.