Save sheet as pdf goes to wrong folder

Arie Bos

Board Regular
Joined
Mar 25, 2016
Messages
224
Office Version
  1. 365
Platform
  1. Windows
I created a new folder 'Apples', and want to save a file in that folder. But my code keeps saving the pdf in the folder in which the new 'Apples' folder was created, and not in the sub-folder 'Apples' itself.
So I tried to put &"\" in fileNameSave between vDir & PDFName, but then a compile error turns up.

Anyone knows how the pdf can be saved one level lower in 'Apples'?

VBA Code:
Public Function vDir() As String
    vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName")
End Function
Sub CommandButton1_Click()
fileSaveName = vDir & Range("PDFName")
Sheets("Overview").Select
'Save 'Overview' as PDF
ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=vDir & PDFName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    From:=1, To:=1, _
    OpenAfterPublish:=False
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this...
VBA Code:
Public Function vDir() As String
    vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName")
End Function

Sub CommandButton1_Click()
    Dim FileSaveName As String
    FileSaveName = vDir & Range("PDFName")
    
    'Check if the File Save Name is correct in the immediate window.
    Debug.Print FileSaveName
    
    Sheets("Overview").Select
    'Save 'Overview' as PDF
    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=FileSaveName, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        From:=1, To:=1, _
        OpenAfterPublish:=False
End Sub
 
Upvote 0
Thank you Trixterz,
Unfortunately it still saves the pdf in the above folder, not in Apples itself...
 
Upvote 0
I solved it with the following. code, but I think it not very efficient...


VBA Code:
Public Function vDir() As String
    vDir = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName")
End Function

Public Function vDir1() As String
    vDir1 = Environ("userprofile") & "\Documents\Soniq\Sales\" & Range("CompanyName") & "\"
End Function

Sub CommandButton1_Click()
      Dim FileSaveName As String
      FileSaveName = vDir1 & Range("PDFName")

      'Check if the File Save Name is correct in the immediate window.
      Debug.Print FileSaveName

      Sheets("Overview").Select
      'Save 'Overview' as PDF
      ActiveSheet.ExportAsFixedFormat _
          Type:=xlTypePDF, _
          Filename:=FileSaveName, _
          Quality:=xlQualityStandard, _
          IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, _
          From:=1, To:=1, _
          OpenAfterPublish:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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