How do I write a macro that will save to My Documents that will work on different machines or for different users (where the path to My Documents may not be the same).
Can I use the environment variable USERPROFILE to do this? If so, how?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Saving from a Macro (2003 SP1)
Copy the following code at the top of a module:
Public Const CSIDL_PERSONAL = &H5
Private Const NOERROR = 0
Private Declare Function SHGetSpecialFolderLocation Lib “shell32” _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
ppidl As Long) _
As Long
Private Declare Function SHGetPathFromIDList Lib “shell32” _
(pidl As Long, _
ByVal pszPath As String) _
As Long
Private Declare Sub CoTaskMemFree Lib “ole32” _
(ByVal pv As Long)
Private Const MAX_PATH = 260
Function GetSpecialFolderLocation(ByVal lngCSIDL As Long) As String
Dim lngRet As Long
Dim strLocation As String
Dim pidl As Long
lngRet = SHGetSpecialFolderLocation(0, lngCSIDL, pidl)
If lngRet = NOERROR Then
strLocation = Space$(MAX_PATH)
lngRet = SHGetPathFromIDList(ByVal pidl, strLocation)
If Not lngRet = 0 Then
GetSpecialFolderLocation = _
Left$(strLocation, InStr(strLocation, vbNullChar) – 1)
End If
Call CoTaskMemFree(pidl)
End If
End Function
Use like this:
ActiveWorkbook.SaveAs GetSpecialFolderLocation(CSIDL_PERSONAL) & “Test.xls”
OK. Scripting is turned on.
So, I insert the suggested code into my macro that does the save:
Dim strMyDocs As String
strMyDocs = CreateObject(“WScript.Shell”).SpecialFolders(“MyDocuments”)
Now, in my macro I am saving a range from a worksheet to an HTML file like so:
ThisWorkbook.Worksheets(1).Range(“Details”).Copy wbkTemp.Worksheets(1).[A1]
wbkTemp.SaveAs Filename:=”C:documents and settingssuudesktoppage1.htm”, FileFormat:=xlHtml ‘ save Test2 as HTML
Should I just change the path and filename to strMyDocs? This seems to work, but I am not following all of this with certainty.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications