Save and print an excel file A4 VBA

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
563
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello, how can I save the file directly in the same path as the Excel file without the selection message appearing
I do not want to specify a special location, I want wherever I put the main file, it will be saved next to it
Thank you


VBA Code:
Sub PDFActiveSheet()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
 
Hi sofas,

maybe

VBA Code:
Sub PDFActiveSheet()
' https://www.mrexcel.com/board/threads/save-and-print-an-excel-file-a4-vba.1223335/
  Dim wsA As Worksheet
  Dim wbA As Workbook
  Dim strTime As String
  Dim strName As String
  Dim strPath As String
  Dim strFile As String
  Dim strPathFile As String
  
  On Error GoTo errHandler
  Set wbA = ActiveWorkbook
  Set wsA = ActiveSheet
  strTime = Format(Now(), "yyyymmdd\_hhmm")
  strPath = wbA.Path
  If strPath = "" Then
    MsgBox "Please save the workbook and start macro again.", vbInformation, "Workbook not saved"
    GoTo exitHandler
  End If
  strPath = strPath & "\"
  strName = Replace(wsA.Name, " ", "")
  strName = Replace(strName, ".", "_")
  strFile = strName & "_" & strTime & ".pdf"
  strPathFile = strPath & strFile
  
  wsA.ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=strPathFile, _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False
  MsgBox "PDF file has been created: " _
    & vbCrLf _
    & strPathFile

exitHandler:
  Set wsA = Nothing
  Set wbA = Nothing
  Exit Sub

errHandler:
  MsgBox "Could not create PDF file"
  Resume exitHandler
End Sub

Ciao,
Holger
 
Upvote 0
Solution

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