我正在使用 FollowHyperlink
方法打开 pdf 文件,如下所示:
Sub Sample()
ActiveWorkbook.FollowHyperlink "C:\MyFile.pdf"
End Sub
发现了at this thread .
我的问题是,如何关闭 pdf?
最佳答案
这是我使用的两个选项。
选项1:这个选项我用它来杀死所有打开的互联网浏览器,当它们不可见时(又名我搞砸了)。可能有一种方法可以通过这种方式挑选出文件,但我不完全确定如果没有 @Jeeped 提到的 API 调用,这是可能的。我将在第二个列出 API 调用。
找出您正在运行的 Adobe 类型。打开 Windows 任务管理器 > 进程并找到描述为 Adobe Reader 的 .exe。
Sub Kill_All_PDFs()
'***ErrorHandler***
On Error Resume Next
'***Define Variables***
Dim objectWMI As Object
Dim objectProcess As Object
Dim objectProcesses As Object
'***Set Objects***
Set objectWMI = GetObject("winmgmts://.")
Set objectProcesses = objectWMI.ExecQuery( _
"SELECT * FROM Win32_Process WHERE Name = 'AcroRd32.exe'") '< Change if you need be
'***Terminate all Open PDFs***
For Each objectProcess In objectProcesses
Call objectProcess.Terminate
Next
'***Clean Up***
Set objectProcesses = Nothing
Set objectWMI = Nothing
End Sub
选项2 API调用方式:
在这里您将能够按标题找到您的 PDF 文件。我修改了代码以找到 Adobe,但如果您想进一步阅读它的工作原理,下面列出了源代码。只需添加出现在 PDF 文件顶部的标题即可。
来源:http://support.microsoft.com/kb/168204
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As Long
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Long) _
As Long
Private Sub Close_AdobeReader()
Dim lpClassName As String
Dim lpCaption As String
Dim Handle As Long
Const NILL = 0&
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060&
lpClassName = "AcrobatSDIWindow"
lpCaption = "e.g.name - Adobe Reader" '< add Title Here
'* Determine the handle to the Calculator window.
Handle = FindWindow(lpClassName$, lpCaption$)
'* Post a message to Calc to end its existence.
Handle = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NILL)
End Sub
希望这对您有所帮助!
关于vba - 使用 FollowHyperlink 打开打开的 PDF 后将其关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27880203/