vba 检查某个pdf文件是否打开并关闭它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25714915/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a certain pdf file is open and close it
提问by user3286479
I use this code to export a pdf file from a word document.
我使用此代码从 Word 文档导出 pdf 文件。
Before exporting I need to check first if a file with the same name is already open, and if so close it then export.
在导出之前,我需要先检查同名文件是否已打开,如果已打开,则关闭它然后导出。
I tried many things but had no luck.
我尝试了很多东西,但没有运气。
Dim adbApp As Acrobat.AcroApp
Dim adbDoc As Acrobat.AcroAVDoc
Dim adbPageView As Acrobat.AcroAVPageView
Set adbApp = CreateObject("AcroExch.App")
Set adbDoc = CreateObject("AcroExch.AVDoc")
If adbDoc.Open("C:\Current Letter Preview.pdf", "") = True Then '==> If the file is not open, this line opens it
    adbDoc.Close (1) '==> Then close it
    If adbDoc Is Nothing Then '==> Doesn't understand that I want to check if any pdf files are open
        adbApp.Exit
    End If
    Set adbApp = Nothing
End If
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
If IsFileOpen("C:\TemporaryLetter.docx") Then
    Set wordApp = GetObject(, "Word.Application")
    wordApp.Documents("C:\TemporaryLetter.docx").Close '==> Is there something like that regarding acrobat IAC?
Else
    Set wordApp = CreateObject("Word.Application")
    With wordApp
        .Visible = True
        .WindowState = 2
    End With
End If
Set wordDoc = wordApp.Documents.Open("C:\TemporaryLetter.docx")
wordDoc.ExportAsFixedFormat OutputFileName:="C:\Current Letter Preview.pdf", _
ExportFormat:=wdExportFormatPDF
wordDoc.Close savechanges:=wdDoNotSaveChanges
Set wordDoc = Nothing
If wordDoc Is Nothing Then
    wordApp.Quit
End If
Set wordApp = Nothing
Call adbDoc.Open("C:\Current Letter Preview.pdf", "")
adbDoc.BringToFront
Set adbPageView = adbDoc.GetAVPageView()
Call adbPageView.ZoomTo(0, 100)
Set adbDoc = Nothing
Set adbPageView = Nothing
回答by Siddharth Rout
To check if the file is open or not, you can see the code that I posted HERESo the usage will be
要检查文件是否打开,您可以查看我在此处发布的代码,因此用法将是
Sub Sample()
    Dim Ret
    '~~> Change this to the relevant file path and name
    Ret = IsFileOpen("C:\Current Letter Preview.Pdf")
    If Ret = True Then
        MsgBox "File is open"
    Else
        MsgBox "File is Closed"
    End If
End Sub
Function IsFileOpen(FileName As String)
    Dim ff As Long, ErrNo As Long
    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0
    Select Case ErrNo
    Case 0:    IsFileOpen = False
    Case 70:   IsFileOpen = True
    Case Else: Error ErrNo
    End Select
End Function
And to close a file, you will have to use APIs FindWindowand PostMessage
要关闭文件,您必须使用 API FindWindow和PostMessage
I have tested the code with Adobe Reader and hence in the code below, the name that I am searching for is "Current Letter Preview.pdf - Adobe Reader"You may have a different name. Please change as applicable.
我已经使用 Adobe Reader 测试了代码,因此在下面的代码中,我正在搜索的名称是"Current Letter Preview.pdf - Adobe Reader"You may have a different name。请根据情况进行更改。
Option Explicit
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassname As String, ByVal lpWindowName As String) As Long
Private Const WM_CLOSE = &H10
Sub Sample()
    Dim Hwnd As Long
    '~~> Find the window of the pdf file
    Hwnd = FindWindow(vbNullString, "Current Letter Preview.pdf - Adobe Reader")
    If Hwnd Then
        '~~> Close the file
        PostMessage Hwnd, WM_CLOSE, 0, ByVal 0&
    Else
        MsgBox "Pdf File not found"
    End If
End Sub

