【问题标题】:Check if a certain pdf file is open and close it检查某个pdf文件是否打开并关闭它
【发布时间】:2014-11-01 02:54:34
【问题描述】:

我使用此代码从 word 文档中导出 pdf 文件。

在导出之前,我需要先检查一个同名文件是否已经打开,如果是,请关闭它然后导出。

我尝试了很多东西,但都没有运气。

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

【问题讨论】:

标签: vba excel pdf


【解决方案1】:

要检查文件是否打开,可以看我贴的代码HERE 所以用法会是

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

要关闭文件,您必须使用 API FindWindowPostMessage

我已经使用 Adob​​e Reader 测试了代码,因此在下面的代码中,我正在搜索的名称是 "Current Letter Preview.pdf - Adobe Reader" 您可能有不同的名称。请根据需要进行更改。

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

【讨论】:

  • +1 很好的答案,也因为它可以通过相应的调整来测试许多文件类型:)
  • 不幸的是,当我运行Open filename For Input Lock Read As #ff 时,我得到一个权限错误,也无法被捕获...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 2017-08-29
相关资源
最近更新 更多