File system shortcut resolves environment variable

ExcelGzh

Board Regular
Joined
Mar 29, 2020
Messages
131
Office Version
  1. 365
Platform
  1. Windows
I use an Excel spreadsheet to create Windows file system shortcuts. The spreadsheet has 3 columns. shortcut name, shortcut folder and target folder. A vba routine then runs through the list and creates or recreates the shortcuts. If the target moves - and it frequently does at my workplace - I can change the target name and re-run the vba routine. I have used this routine successfully for quite a while in a mapped network drive environment. We have recently started using Microsoft Teams and we access the documents by synchronising the Teams channels to the local computer. This means all documents have the path <userprofile>\<path>\<document>. This then means that all shortcuts have to be delivered to Teams in a way that takes account of the user profile path. The code I am using is:

sub CreateTeamsShortcuts
Dim MyShortcut as Object
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
Set MyShortcut =WSHShell.CreateShortcut ("%USERPROFILE%\" & <shortcutfolder>, <shortcutname>)
MyShortcut.TargetPath = "%USERPROFILE%\" & <targetfolder>
MyShortcut.save
End sub

The problem I have is that while CreateShortcut retains the environment variable USERPROFILE in the text string and places the shortcut in the correct place, the TargetPath does not. It always resolves the text string to my own user profile which of course is useless for anyone else attempting to use the shortcut.

Is there a way of preventing TargetPath from resolving the environment variable USERPROFILE and just placing the original text string in the shortcut properties?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Does splitting it up work? Something like... "%USER" & "PROFILE%" & ...
 
Upvote 0
No that didn't work. I've also tried "C:\Users\%USERNAME%" and also put the whole string inside another pair of double quotes. It was even able to resolve """%USERPROFILE%\""" & <shortcutfolder>, <shortcutname>)
 
Upvote 0
What if you assigned it to a variable first?

VBA Code:
Dim myTargetPath
myTargetPath = "%USERPROFILE%\" & <targetfolder>
MyShortcut.TargetPath = myTargetPath
 
Upvote 0
Tried that as well. And also using Replace to retrospectively insert %USERPROFILE% but TargetPath always resolved whatever and however the string was given.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top