Saving to users My Documents

  • Thread starter Thread starter Rob Edwards
  • Start date Start date
R

Rob Edwards

Hi,
When creating code to save a file, how do you specify a users 'My
Documents' folder so that it is determined at run time. Therefore,
dependent on the user, the file will be saved in their C:\Documents and
Settings\'user'\My Documents\, i.e. in my case... C:\Documents and
Settings\REdwards68584\My Documents\

I'm sure it must be easy!

Thanks in advance.

Rob

Rob Edwards
(e-mail address removed)
07092 108121
 
Rob,

You can determine the username path from one of the environment variables.
You can cycle through them with the following code:

Sub allEnviron()
z = 1
doagain:
On Error GoTo Quitapp
Debug.Print Environ(z) & " : # " & z
z = z + 1
GoTo doagain
Quitapp:
End Sub

http://HelpExcel.com
 
Rob said:
Hi,
When creating code to save a file, how do you specify a users 'My
Documents' folder so that it is determined at run time. Therefore,
dependent on the user, the file will be saved in their C:\Documents
and Settings\'user'\My Documents\, i.e. in my case... C:\Documents and
Settings\REdwards68584\My Documents\

I'm sure it must be easy!

Thanks in advance.

Rob

This works for me - requires a reference to the Windows Scripting Host
Object Model.

Sub SetMyDocs()
Const SPECIAL_FOLDER_MY_DOCS As Long = 16
Dim o As New IWshRuntimeLibrary.WshShell
MsgBox o.SpecialFolders(SPECIAL_FOLDER_MY_DOCS)
End Sub

HTH
 
Just to Add to Rob's excellent example, to demonstrate late binding
(reference not required)

Sub SetMyDocs()
Const SPECIAL_FOLDER_MY_DOCS As Long = 16
Dim o As Object
Set o = CreateObject("WScript.Shell")
MsgBox o.SpecialFolders(SPECIAL_FOLDER_MY_DOCS)
End Sub
 
Thanks to all!!

Rob Edwards
(e-mail address removed)
(e-mail address removed)
07092 108121
 

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

Back
Top