Home > Software design >  Trying to create a .pdf file with a VBA script, but it doesn't get saved in the location I spec
Trying to create a .pdf file with a VBA script, but it doesn't get saved in the location I spec

Time:11-15

I am currently writing a subroutine that allows me to save a PDF in a folder by using data from an excel sheet, however when I run the code the PDF is nowhere to be found in the expected folder.

here is the subroutine:

(I am very new to this, apologies for any mistake)

Sub createfile()
        'Declare worksheet
    Dim ws As Worksheet
    Set ws = ActiveSheet

    
    ' declare location to save file as string
    Dim sStoreFile As String
    sStoreFile = "L:\My Documents\ExcelTestDocument"
    
    ' declare filename variable as string
    Dim sFilename As String
    ' declare number of files to create
    Dim iFileCount As Integer
    iFileCount = Range("A1").End(xlDown).Row - 1
    
    Dim i As Integer
    For i = 1 To iFileCount
        
        ' set the value of the file from spreadsheet
        sFilename = ws.Range("g" & i   1).Value
        

        
        ' If extension has "pdf" then save as PDF
        If Right(sFilename, 3) = "pdf" Then
            ws.Range("A" & iFileCount   5).Clear
            ws.Range("A" & iFileCount   5).Value = sFilename
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFilename
            ActiveSheet.ExportAsFixedFormat Type:=xlType
        End If
        
        
   
    Next i
    
   MsgBox "All files have been completed"
    
 End Sub

I was expecting to find the PDF file in the folder I mentioned in the subroutine, but even after the subroutine is completed, and I am prompted with the MSG box saying all files have been created, I still am not able to find any files

CodePudding user response:

You have to add sStoreFile to the Filename:

 ' set the value of the file from spreadsheet
   sFilename = sStoreFile  & "\" & ws.Range("g" & i   1).Value
  • Related