Using %userprofile% in macro that changes path

C

Cissy

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
 
J

Jay Freedman

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

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"
 
J

Jay Freedman

Jay said:
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"

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top