+ Reply to Thread
Results 1 to 10 of 10

Thread: cannot save a workbook to the desktop of any user

  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Gotemba, Shizuoka, Japan
    MS-Off Ver
    Excel 2007
    Posts
    283

    cannot save a workbook to the desktop of any user

    The below script saves a workbook to the desktop of user 123456.
    Is there a way to write the code so that the workbook will be saved to any users desktop?

    ActiveWorkbook.SaveAs FileName:="C:\Users\123456\Desktop\Test.xlsx", _
            FileFormat:=xlWorkbookDefault, CreateBackup:=False

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: cannot save a workbook to the desktop of any user

    Maybe:

    ActiveWorkbook.SaveAs FileName:="C:\Users\" & _
            Application.UserName & _
            "\Desktop\Test.xlsx", _
                    FileFormat:=xlWorkbookDefault, CreateBackup:=False


    Regards

  3. #3
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Gotemba, Shizuoka, Japan
    MS-Off Ver
    Excel 2007
    Posts
    283

    Re: cannot save a workbook to the desktop of any user

    TMShucks, I tried your suggestion before starting the thread. It didn't work.
    The below code saves the workbook into the Documents folder of any user.

    ActiveWorkbook.SaveAs FileName:="Test.xlsx", _
            FileFormat:=xlWorkbookDefault, CreateBackup:=False

  4. #4
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: cannot save a workbook to the desktop of any user

    sub snb()
        ActiveWorkbook.SaveAs CreateObject("wscript.shell").specialfolders("desktop") & "\Test.xlsx", xlWorkbookDefault
    end sub



  5. #5
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Gotemba, Shizuoka, Japan
    MS-Off Ver
    Excel 2007
    Posts
    283

    Re: cannot save a workbook to the desktop of any user

    snb, thanks a lot. That did it.

  6. #6
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: cannot save a workbook to the desktop of any user

    Sorry, I was making the naive assumption that the user name and the application user name were the same. Clearly, not ;-)

    If you need the logged in user name you can get it with the following code ... but that is just a little longer than snb's code (why is that not a surprise)

    Regards


    Option Explicit
    
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
     
    Function UserNameWindows() As String
        
        Dim lngLen As Long
        Dim strBuffer As String
        
        Const dhcMaxUserName = 255
        
        strBuffer = Space(dhcMaxUserName)
        lngLen = dhcMaxUserName
        If CBool(GetUserName(strBuffer, lngLen)) Then
            UserNameWindows = Left$(strBuffer, lngLen - 1)
        Else
            UserNameWindows = ""
        End If
    End Function
    
    Sub test()
    
    ActiveWorkbook.SaveAs Filename:="C:\Users\" & _
            UserNameWindows & _
            "\Desktop\Test.xlsm", _
                    FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
    End Sub

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: cannot save a workbook to the desktop of any user

    @TMS

    Are you familiar with:

    environ("username")
    
    and
    
    createobject("wscript.network").username



  8. #8
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: cannot save a workbook to the desktop of any user

    @snb: the first one yes, the second one, no.

    I learned and used the Function GetUserName method a long time ago and tend to resort to "what I know". As ever, good to have some options.

    Thank you for the reminder and the lesson ;-)

    Regards

  9. #9
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: cannot save a workbook to the desktop of any user

    @TMS

    You're welcome.



  10. #10
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Gotemba, Shizuoka, Japan
    MS-Off Ver
    Excel 2007
    Posts
    283

    Re: cannot save a workbook to the desktop of any user

    TMShucks, your lenghty code kind of made me worry that it was a bit premature to call the problem solved. I had not tried snb's code on a different computer using a different user name.

    Now, I did. snb's solution works for any user on any computer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0