+ Reply to Thread
Results 1 to 5 of 5

VBA: Print to PDF and Overwrite Existing file

Hybrid View

  1. #1
    Registered User
    Join Date
    03-11-2020
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    VBA: Print to PDF and Overwrite Existing file

    Hi all

    Just a quick one (hopefully). Having trouble with establishing an overwrite protocol when saving something as PDf.

    The current code I am utilising is below, however it does not prompt to overwrite, instead not saving at all (I presume due to the error in the 4th line)
    Code is as follows:
    Sub PrintPDFTaxRecTrust()
        Dim FileName As String
        Application.DisplayAlerts = False
        On Error Resume Next
    
        sPath = ThisWorkbook.Path
        CurrentYear = Sheets("WP Index").Range("K6").Value
        
        With Worksheets("Tax Rec")
            sName = CurrentYear & " Tax Rec - " & .Range("A1").Value
    
            FileName = ThisWorkbook.Path & "\" & sName & ".pdf"
    
            Range("A1:H80").Select
            ActiveSheet.PageSetup.PrintArea = "A1:H80"
            .FitToPagesTall = False
            .FitToPagesWide = 1
    
    
            .ExportAsFixedFormat _
                    Type:=xlTypePDF, _
                    FileName:=FileName, _
                    Quality:=xlQualityStandard, _
                    IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, _
                    OpenAfterPublish:=False
        End With
    
    End Sub
    Thank you in advance!

  2. #2
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA: Print to PDF and Overwrite Existing file

    Hi, in the first place I would check if the file already exists, because as you said, no warning is given,
    secondly I would rewrite this, as this gives an error (you ruled that out in line 4):
            ActiveSheet.PageSetup.PrintArea = "A1:H80"
            .FitToPagesTall = False
            .FitToPagesWide = 1
    into
            ActiveSheet.PageSetup.PrintArea = "A1:H80"
            With ActiveSheet.PageSetup
              .FitToPagesTall = False
              .FitToPagesWide = 1
            End With
    Cheers
    Erwin
    I started learning VBA because I was lazy ...
    Still developing.... being more lazy...

  3. #3
    Registered User
    Join Date
    03-11-2020
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Re: VBA: Print to PDF and Overwrite Existing file

    Gday Erwin

    Thanks for your reply mate, really do appreciate it.
    Great changes, I have now updated this!

    In respect to check if the file exists and overwrite it - any ideas?

  4. #4
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: VBA: Print to PDF and Overwrite Existing file

    Yes, you can add this into your code:
            Filename = ThisWorkbook.Path & "\" & sName & ".pdf"
            FileCheck (Filename)
            Select Case FileCheck(Filename)
            Case 1
                MsgBox ("The file " & Filename & " exist")
                'your code
            Case -1
                MsgBox ("The file " & Filename & " is already open")
                'your code
            Case 0
                MsgBox ("The file " & Filename & " is not in current directory")
                'so you can put the rest of your code here
            End Select
    and below your macro:
    Function FileCheck(ByVal sFull As String) As Integer
    'returnvalue:
    ' -1 is open
    ' 1 is ondisk
    ' 0 is not on disk/error
    
        Dim iRes%, sName$
        Dim wb As Workbook
        sName = Dir(sFull)
        iRes = -(Len(sName) > 0)
        If iRes = 1 Then
            For Each wb In Application.Workbooks
                If StrComp(wb.Name, sName, vbTextCompare) = 0 Then
                    iRes = -1
                    Exit For
                End If
            Next
        End If
        FileCheck = iRes
    End Function
    Cheers
    Erwin

  5. #5
    Registered User
    Join Date
    03-11-2020
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Re: VBA: Print to PDF and Overwrite Existing file

    Hi Eastwood,
    Just wanted to say a huge thank you for your assistance and coding.
    The code worked exactly as required; I simply changed a few things for my needs.

    Really do appreciate it

    cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Overwrite existing file
    By dekueb in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2018, 03:12 PM
  2. overwrite existing file automaticly
    By hakunaberlin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-24-2016, 02:23 PM
  3. Need to Overwrite existing XML file - Error 58
    By VBABegin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2016, 11:11 AM
  4. PDF Export with overwrite existing file prompt
    By Harribone in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2015, 04:50 PM
  5. Save As-Overwrite existing File
    By loulorisgomez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2012, 07:24 AM
  6. Prevent overwrite of existing file when saving as pdf
    By glenin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2010, 06:04 PM
  7. Export And Overwrite Existing file
    By okelly in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-20-2007, 11:19 AM

Tags for this Thread

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.6.0 RC 1