2017-10-11 8 views
0

excel Vbaの開いているすべてのPDFファイルを検出して一覧表示することは可能ですか?特定の既知のPDFファイルとパスを確認できますが、この場合ファイル名とパスはわかりません。Excelのすべての開いているPDFファイルを検出して一覧表示するVBA

おかげで

+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-または-VBS-enumerate-all-open-Acrobat-Reader-documents.html)のソリューションがあります。 –

+0

@Ryan Wildryありがとう!私はいつもAHKを使っていくつかの分野でExcel VBAの機能不足を回避できることを忘れています。 –

答えて

0

私はこのようなもののためにAHKを使用することができライアンWildryによってコメントで思い出しました。ここでは、私が使用して終了コードです:

私はVBAの正規表現のパターンを設定するので、どのようにPDFウィンドウのタイトルが表示されます。私は以前のアプリケーションのためにウェブから引っ張ったいくつかの機能を使用しました。

メインVBA:AHKを呼び出す

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 

機能:クリップボードのテキストを取得する

'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はこれを読み込んで、使用する配列に分割します。

私はそこにもっと効率的な方法があると確信していますが、これは楽しい方法であり、一緒にまとめることができる唯一の方法でした。

関連する問題