+ Reply to Thread
Results 1 to 2 of 2

VBA Prompt to close PDF so file can be overwrtitten or close automatically

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Houston, TX
    MS-Off Ver
    office 365
    Posts
    648

    VBA Prompt to close PDF so file can be overwrtitten or close automatically

    Currently I am saving a PDF to a folder and it looks to see if that file had already been saved to that folder. If that file exists it asks if you would like to overwrite. The Problem I have is if you enter anything other 'Y' it will create the new file : How would I add that if they choose to hit cancel then it cancels?

    Yes = Overwrite file / No = Makes the new time stamped file already in code/ Cancel = Exits entirely.

    Also, this is the big issue

    If the user chooses to overwrite the PDF and by chance the user had that PDF open you get a runtime error because the file is open until the user manually closes the file.

    Is it possible that after it knows the file exists and it prompts if you want to overwrite & if you choose overwrite then it makes sure the PDF is closed so it can overwrite eliminating the user from having to close the file manually

    If it can't close automatically then could is show message box that says "the PDF seams to be open can not overwrite please close PDF and rerun." then exit

    That way they would not get the run-time error saying could not save..... People see run time error and automatically stop trying

    Sub Covercopypaste()
    Range("A1:k46").Select
        ThisWorkbook.Save
        Selection.Copy
        Call PDfExist
        
    End Sub
    
    Sub PDfExist()
            MyPath = ActiveWorkbook.Path & "\Morning Reports\"
            fn = "Morning Report" & "_" & Format(Now(), "mm.dd.yy") & ".PDF"
            Fname = MyPath & fn
            
            FileInQuestion = Dir(Fname)
            If FileInQuestion <> "" Then
              If InputBox("Today's PDF Exists, Want to overwrite (Y/N)?   If 'No' an Additional PDF With Time Stamp Will be added to Folder. ") <> "y" Then
                 fn = "Morning Report" & "_" & Format(Now(), "mm.dd.yy hhmm") & ".PDF"
                 Fname = MyPath & fn
              End If
            End If
            Sheets("Morning Report").Range("A1:k46").ExportAsFixedFormat Type:=xlTypePDF, FileName:=Fname, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, OpenAfterPublish:=False
            
            End Sub

  2. #2
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Houston, TX
    MS-Off Ver
    office 365
    Posts
    648

    Re: VBA Prompt to close PDF so file can be overwrtitten or close automatically

    I used and it worked
    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
    I then just used
    Call CloseAcrobatReader
    to make it happen

+ 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. automatically close file and save changes with a new name
    By 2funny4words in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2009, 10:07 PM
  2. Auto close, save to new file without prompt
    By Bafa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2007, 10:31 AM
  3. [SOLVED] Intercepting the file save prompt before close?
    By Neil Bhandar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2006, 11:20 PM
  4. [SOLVED] Close file automatically
    By Lee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2005, 06:20 PM
  5. [SOLVED] CLOSE ALL macro for XL2K (but with prompt to save for each file)?
    By StargateFan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2005, 10:05 AM

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