Solved

Close PDF created in Excel with VB

Posted on 2014-09-30
2
160 Views
Last Modified: 2014-09-30
Hi Experts,
I have written a script to create 3 pdfs in a workbook.
The good news is that this is working well. The PDFs are being created and saved to the location that I want them to. When they are created I don't want the user to be able to see the PDF after the script has run (as in I want each pdf to be closed).

I'm wondering if / how to do this?
I figure it would be done similarly to the following:
FileNamePDFcatch.Close

But that won't work...

Heres the current script:
Sub PdfCreator()
Dim wb1 As Workbook, wb2 As Workbook
Dim vSheet As Variant, vSheets As Variant
Dim sCatchFile As String
Dim FileNamePDFcatch
Dim FileNamePDFprocess
Dim FileNamePDFA
Dim CopyForm2 As String
'THIS SCRIPT IS CREATING THE EXCEL WORKBOOK THAT NEEDS TO BE SENT TO THE PMO TO ENTER ONTO THE PIPELINE
Set wb1 = ThisWorkbook

Application.DisplayAlerts = False
Application.ScreenUpdating = False

'Add the future names of the PDF (including location) to the cells onth admin sheets - MAY NEED TO UNHIDE?
FileNamePDFcatch = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " " & Range("CURRENTorf").Value & "catch.pdf"
Range("PDFcatch").Value = FileNamePDFcatch
'Add the future names of the PDF (including location) to the cells onth admin sheets - MAY NEED TO UNHIDE?
FileNamePDFprocess = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " " & Range("CURRENTorf").Value & " Process.pdf"
Range("PDFprocess").Value = FileNamePDFprocess
'Add the future names of the PDF (including location) to the cells onth admin sheets - MAY NEED TO UNHIDE?
FileNamePDFA = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " " & Range("CURRENTorf").Value & " A.pdf"
Range("PDFA").Value = FileNamePDFA
       
    'NEED TO MAKE SO THAT THIS IS ONLY VALUES!!! LOCKING CELLS? NOT Needed if PDF... COPY OF ORF THOUGH?
    'Catchment Worksheet
        Sheets("ORF - Catchment").Visible = True
        Sheets("ORF - Catchment").Select
        Sheets("ORF - Catchment").Copy
        'Create the PDF with the filename determined above
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FileNamePDFcatch, _
            Quality:=xlQualityStandard, IncludeDocProperties:=False, _
            IgnorePrintAreas:=False, OpenAfterPublish:=True
        ActiveWindow.Close
        FileNamePDFcatch.Close
       
        'Process PDF to be created here too!
        wb1.Activate
        Sheets("ORF - Process").Visible = True
        Sheets("ORF - Process").Select
        Sheets("ORF - Process").Copy
       
        'Create the PDF with the filename determined above
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FileNamePDFprocess, _
            Quality:=xlQualityStandard, IncludeDocProperties:=False, _
            IgnorePrintAreas:=False, OpenAfterPublish:=True
        ActiveWindow.Close

            wb1.Activate
            'Determine if the checklist is required
            If Range("ACHECKLIST").Value = "Checklist IS Required" Then
            'A Checklist to be created here too!
            Sheets("ORF - A Reporting").Visible = True
            Sheets("ORF - A Reporting").Select
            Sheets("ORF - A Reporting").Copy
            'Create the PDF with the filename determined above
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FileNamePDFA, _
                Quality:=xlQualityStandard, IncludeDocProperties:=False, _
                IgnorePrintAreas:=False, OpenAfterPublish:=True
            ActiveWindow.Close

            Else
            End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub


Any help appreciated :-)
0
Comment
Question by:martywal
2 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40353920
Hi,

Pls try

            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FileNamePDFA, _
                Quality:=xlQualityStandard, IncludeDocProperties:=False, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False


Regards
0
 

Author Closing Comment

by:martywal
ID: 40353922
Thanks for that!

The only change was:

OpenAfterPublish:=False

Thanks again!!!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now