2017-10-11 65 views
0

是否可以检测并列出来自excel Vba的所有打开的PDF文件?我知道我可以检查一个特定的已知PDF文件和路径,但是在这种情况下,文件名和路径将不可知。检测并列出来自excel VBA的所有打开的PDF文件

感谢

+1

您可以使用Win API进行此操作。请参阅:https://support.microsoft.com/en-us/help/183009/how-to-enumerate-windows-using-the-win32-api。使用像Autohotkey或AutoIT这样的包装语言来执行此操作可能更容易。 –

+1

@Siddharth Rout在[其他论坛]上有一个解决方案(https://www.experts-exchange.com/questions/26817662/VBA-or-VBS-enumerate-all-open-Acrobat-Reader-documents.html) –

+0

@Ryan Wildry谢谢!我总是忘记我可以使用AHK来解决Excel VBA在某些领域缺乏功能的问题。 –

回答

0

我在瑞安Wildry的评论,我可以用AHK这样的事情被提醒。以下是我最终使用的代码:

首先,我在VBA中设置了一个正则表达式模式,以便PDF窗口标题的显示方式。使用了我从Web上为以前的应用程序提取的一些功能。

主要VBA:

Private Sub Get_PDFs() 

    Dim pattern As String 
    Dim ahkParamColl As Collection 
    Dim windowArr() As String 

    'regex pattern to match with open Adobe PDF Files 
    pattern = "^(.+)\.pdf - Adobe Reader$" 

    'add pattern to AHK parameter collection 
    Set ahkParamColl = Nothing 
    Set ahkParamColl = New Collection 
    ahkParamColl.Add (pattern) 

    'run window detection AHK Script 
    Call Functions.Run_AHK("Detect All Open Windows.ahk", ahkParamColl) 

    'send list to array 
    windowArr = Split(GetClipBoardText, Chr(10)) 

End Sub 

函数来调用AHK:

'these are for AHK scripts to run from Excel 
Public Const ahk_ScriptsLoc = """C:\Location of Scripts\" 'starts w/a quote 
Public Const ahk_PgmLoc = "C:\Location of AHK Pogram\AHK.exe" 


Function Run_AHK(AHK_Script_Name As String, Optional Parameters As Collection) 

'Call AHK script from VBA 
Dim i As Integer 
Dim waitOnReturn As Boolean: waitOnReturn = True 
Dim windowStyle As Integer: windowStyle = 1 
Dim AHKscript As String 
Dim wsh As Object 
Set wsh = VBA.CreateObject("WScript.Shell") 

    'set the ahk script string to call 
    AHKscript = ahk_PgmLoc & " " & ahk_ScriptsLoc & AHK_Script_Name & """ """ 

    'add parameters to script string 
    If Not Parameters Is Nothing Then 
     For Each s In Parameters 
      AHKscript = AHKscript & s & """ """ 
     Next s 
    End If 

    'run ahk script 
    wsh.Run AHKscript, windowStyle, waitOnReturn 

End Function 

函数获取剪贴板文本:

Public Function GetClipBoardText() 
    Dim DataObj As MsForms.DataObject 
    Set DataObj = New MsForms.DataObject 

    On Error GoTo Whoa 

    '~~> Get data from the clipboard. 
    DataObj.GetFromClipboard 

    '~~> Get clipboard contents 
    myString = DataObj.GetText(1) 
    GetClipBoardText = myString 

    Exit Function 
Whoa: 
    If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty" 
End Function 

主要AHK(从Here钩住):

;regex pattern sent from calling application 
pattern = %1% 

;get all window names and loop through 
WinGet windows, List 
Loop %windows% 
{ 
    id := windows%A_Index% 
    WinGetTitle wt, ahk_id %id% 
    ;if window matches pattern, add to list 
    IF (RegexMatch(wt,pattern)>0) then 
    { 
     s .= wt . "`n" 
    } 
} 
;send list to clipboard 
Clipboard := s 

所以VBA宏将设置发送到AHK脚本的正则表达式模式。如果需要,我可以在以后使用它来处理其他文档类型或命名模式。然后AHK将被调用,它循环遍历每个打开的窗口,检查它是否匹配定义的模式,然后将其附加到一个字符串。这个字符串被发送到剪贴板,然后VBA读取并分割成一个数组供我使用。

我确信有可能是一种更有效的方式,但这是一种有趣的方式,也是我可以放在一起的唯一方式。