vba to save a file to a location without knowing what the username is

shahdelsol

Active Member
Joined
Jul 21, 2009
Messages
276
Office Version
  1. 365
Platform
  1. Windows
I am trying to save a file to a folder on a desktop without knowing what username is and I came up with this code but it doesn't work. Can anyone correct me? Thanks

VBA Code:
Sub SaveAsPDF()
    
    
    Dim myloc As String
    
    myloc = "C:\Users\" & Environ("username") & "\desktop\" & " \ books \ """
    
 
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "myloc" & ActiveSheet.Range("J1").Value & " " & ActiveSheet.Range("C10").Value & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
    
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Just at a quick glance- if you have your path right- you are generating a double backslash in this line between desktop and books... You have backslash at the end of desktop and also one at the start of books... It also look like you have some added spaces in books, but that could just be the way the code posted.

VBA Code:
myloc = "C:\Users\" & Environ("username") & "\desktop\" & " \ books \ """
 
Upvote 0
Just at a quick glance- if you have your path right- you are generating a double backslash in this line between desktop and books... You have backslash at the end of desktop and also one at the start of books... It also look like you have some added spaces in books, but that could just be the way the code posted.

VBA Code:
myloc = "C:\Users\" & Environ("username") & "\desktop\" & " \ books \ """
You are correct. I noticed those after I posted but even after I corrected it, it didn't work however I am not getting any error but I don't see any file being saved either. Any suggestions?

Here is the correction:

VBA Code:
myloc = "C:\Users\" & Environ("username") & "\desktop" & "\books\"
 
Upvote 0
Never mind it is working now. I had to remove " " from myloc. Thanks for help
 
Upvote 0
Great, I am glad you got it working. I was happy to help. Thanks for the feedback!
 
Upvote 0
No need to know Username.
Change references as required.
Code:
Sub Or_So()
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        CreateObject("WScript.Shell").specialfolders("Desktop") & "\Test Report.pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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