+ Reply to Thread
Results 1 to 9 of 9

close PDF file

Hybrid View

  1. #1
    Registered User
    Join Date
    08-03-2012
    Location
    Qld, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    close PDF file

    Hi. Does anyone know the code to close a PDF file? Thanks.
    Last edited by josh1981; 08-18-2012 at 06:42 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close PDF file

    Hello josh1981,

    Do you want to close the PDF file in Acrobat Reader or Acrobat Reader?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    08-03-2012
    Location
    Qld, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: close PDF file

    I suppose I would like to close acrobat reader.

    ---------- Post added at 11:51 AM ---------- Previous post was at 11:41 AM ----------

    Here is the code bellow if it helps. It is to create a PDF file of the excel worksheet, then email the PDF file.

    Sub EmailPR()
    Application.ScreenUpdating = False
    ' Generate PDF file
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
            "C:\AutoReport\Performance Report.pdf", Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
            True
    Application.ScreenUpdating = False
    
    'This part to email
    'If you get this error : The transport failed to connect to the server
    'then try to change the SMTP port from 25 to 465
    
        Dim iMsg As Object
        Dim iConf As Object
        Dim StrBody As String
        Dim Flds As Variant
    
        Set iMsg = CreateObject("CDO.Message")
        Set iConf = CreateObject("CDO.Configuration")
    
        iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxxxxxxxx@gmail.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxxxxx"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
            .Update
        End With
    
        StrBody = "Please see attached the performance report" & vbNewLine & vbNewLine & _
                  "Regards" & vbNewLine & _
                  "Draft Surveyor" & vbNewLine & _
                  ThisWorkbook.Sheets("Prep").Range("B5") & vbNewLine & _
                  ThisWorkbook.Sheets("Prep").Range("D79")
    
        With iMsg
            Set .Configuration = iConf
            .To = Sheets("Prep").Range("D79").Value
            .CC = ""
            .BCC = ""
            ' Note: The reply address is not working if you use this Gmail example
            ' It will use your Gmail address automatic. But you can add this line
            ' to change the reply address  .ReplyTo = "Reply@something.nl"
            .From = """xxxx"" <xxxxxxxxxxx@gmail.com>"
            .Subject = ThisWorkbook.Sheets("Prep").Range("B1").Value
            .TextBody = StrBody
            .Addattachment "C:\AutoReport\Performance Report.pdf"
            .Send
        End With
    Beep
    End Sub

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close PDF file

    Hello josh1981 ,

    The code you posted only attaches a PDF file to the email. It does not open Acrobat Reader. If you want to close Acrobat Reader then copy the code below into a new VBA module. You can call "CloseAcrobatReader" from within your VBA code.

    
    Private Declare Function FindWindow _
        Lib "user32.dll" Alias "FindWindowA" _
            (ByVal lpClassName As String, _
             ByVal lpWindowName As String) _
        As Long
    
    Private Declare Function SendMessage _
        Lib "user32.dll" Alias "SendMessageA" _
            (ByVal hWnd As Long, _
             ByVal Msg As Long, _
             ByVal wParam As Long, _
             ByVal lParam As Long) _
        As Long
    
    Sub CloseAcrobatReader()
    
        Dim hWnd As Long
        Dim nRet As Long
        
        Const WM_CLOSE As Long = &H10
        
            hWnd = FindWindow("AcrobatSDIWindow", vbNullString)
            
            If hWnd <> 0 Then
                nRet = SendMessage(hWnd, WM_CLOSE, 0, 0)
            End If
            
    End Sub

  5. #5
    Registered User
    Join Date
    08-03-2012
    Location
    Qld, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: close PDF file

    Hi Leith. I ran the code and it did not do anything, so that would probably suggest that Acrobat Reader is not open as you have said. When my code opens the PDF file it has at the top bar Performance Report.pdf - Adobe Reader, so I assumed from that Acrobat Reader was open.
    I used the macro recorder with excel 2010 to generate the PDF file if that helps.
    Maybe it is the case that I just need the code to close the PDF file. Can you help with that?

  6. #6
    Registered User
    Join Date
    08-03-2012
    Location
    Qld, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: close PDF file

    Hi Leith. Your code does work on its own, however when I have called "CloseAcrobatReader" nothing happens. This is what I have got without the email code, with the code that you have given me in a seperate module:

    Sub EmailPR()
    Application.ScreenUpdating = False
    ' Generate PDF file
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
            "C:\AutoReport\Performance Report.pdf", Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
            True
    Application.ScreenUpdating = False
    
    CloseAcrobatReader
    
    'This part to email
    'If you get this error : The transport failed to connect to the server
    'then try to change the SMTP port from 25 to 465
    
       
    End Sub


    ---------- Post added at 06:01 PM ---------- Previous post was at 05:58 PM ----------

    Hi Leith. I put CloseAcrobatReader at the bottom of the code after the email code and it now closes the PDF file. Thanks very much for your help.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close PDF file

    Hello josh1981,

    Excel 2007 and later can create and read PDF files without opening Acrobat reader. The code you just posted will save the active sheet as a PDF but will not open Acrobat reader to do so. Unless Acrobat reader is open (you should see the icon on the taskbar), the macro will not do anything because it can not find a running instance of Acrobat Reader to close.

  8. #8
    Registered User
    Join Date
    08-03-2012
    Location
    Qld, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: close PDF file

    OK, after spending all day on this I have realised my mistake.

    Change OpenAfterPublish:= _
    True
    To OpenAfterPublish:= _
    False

    This way the pdf file will not be opened.

    Thanks for your help Leith, I am sure what you have given me will come in handy.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close PDF file

    Hello Josh,

    I forgot about that argument opening Acrobat when set to true. Good catch.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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