vba 使用 FollowHyperlink 打开打开的 PDF 后关闭它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27880203/
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
Close an opened PDF after opening it using FollowHyperlink
提问by bsapaka
I am opening a pdf file using the FollowHyperlink
method, shown here:
我正在使用该FollowHyperlink
方法打开一个 pdf 文件,如下所示:
Sub Sample()
ActiveWorkbook.FollowHyperlink "C:\MyFile.pdf"
End Sub
which was found at this thread.
这是在这个线程中找到的。
My question is, how do I close the pdf?
我的问题是,如何关闭pdf?
回答by Ricky
Here are two options i use.
这是我使用的两个选项。
Option1: This option I use it to kill all open internet browsers when they are not visible(aka I messed up). There could be a method to single the file out this way but i am not entirely sure it is possible without an API call as @Jeeped mentioned. I will list the API Call second.
选项1:这个选项我用它来杀死所有不可见的打开的互联网浏览器(也就是我搞砸了)。可能有一种方法可以通过这种方式将文件分开,但我不完全确定没有@Jeeped 提到的 API 调用是否可行。我将列出第二个 API 调用。
To find out which Adobe type you are running. Open Windows Task Manager > Processes and find the .exe with the description Adobe Reader.
找出您正在运行的 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
Option2 API Call Method:
Option2 API 调用方式:
Here you will be able to find your PDF file by title. I modified the code to find Adobe but the source is listed below if you would like further reading on how it works. Just add the title as it appears at the top of your PDF file.
在这里,您将能够按标题找到您的 PDF 文件。我修改了代码以找到 Adobe,但如果您想进一步阅读它的工作原理,下面列出了源代码。只需添加出现在 PDF 文件顶部的标题即可。
Source: http://support.microsoft.com/kb/168204
来源: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
Hope this helps you on your way!
希望这对您有所帮助!