Open a PDF using VBA in Excel Open a PDF using VBA in Excel vba vba

Open a PDF using VBA in Excel


If it's a matter of just opening PDF to send some keys to it then why not try this

Sub Sample()    ActiveWorkbook.FollowHyperlink "C:\MyFile.pdf"End Sub

I am assuming that you have some pdf reader installed.


Use Shell "program file path file path you want to open".

Example:

Shell "c:\windows\system32\mspaint.exe c:users\admin\x.jpg"


Hope this helps. I was able to open pdf files from all subfolders of a folder and copy content to the macro enabled workbook using shell as recommended above.Please see below the code .

Sub ConsolidateWorkbooksLTD()Dim adobeReaderPath As StringDim pathAndFileName As StringDim shellPathName As StringDim fso, subFldr, subFlodrDim FolderPathDim Filename As StringDim Sheet As WorksheetDim ws As WorksheetDim HK As StringDim s As StringDim J As StringDim diaFolder As FileDialogDim mFolder As StringDim Basebk As WorkbookDim Actbk As WorkbookApplication.ScreenUpdating = FalseSet Basebk = ThisWorkbook' Open the file dialogSet diaFolder = Application.FileDialog(msoFileDialogFolderPicker)diaFolder.AllowMultiSelect = FalsediaFolder.ShowMsgBox diaFolder.SelectedItems(1) & "\"mFolder = diaFolder.SelectedItems(1) & "\"Set diaFolder = NothingSet fso = CreateObject("Scripting.FileSystemObject")Set FolderPath = fso.GetFolder(mFolder)For Each subFldr In FolderPath.SubFolderssubFlodr = subFldr & "\"Filename = Dir(subFldr & "\*.csv*")Do While Len(Filename) > 0J = FilenameJ = Left(J, Len(J) - 4) & ".pdf"   Workbooks.Open Filename:=subFldr & "\" & Filename, ReadOnly:=True   For Each Sheet In ActiveWorkbook.Sheets   Set Actbk = ActiveWorkbook   s = ActiveWorkbook.Name   HK = Left(s, Len(s) - 4)   If InStrRev(HK, "_S") <> 0 Then   HK = Right(HK, Len(HK) - InStrRev(HK, "_S"))   Else   HK = Right(HK, Len(HK) - InStrRev(HK, "_L"))   End If   Sheet.Copy After:=ThisWorkbook.Sheets(1)   ActiveSheet.Name = HK   ' Open pdf file to copy SIC Decsription   pathAndFileName = subFlodr & J   adobeReaderPath = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"   shellPathName = adobeReaderPath & " """ & pathAndFileName & """"   Call Shell( _    pathname:=shellPathName, _    windowstyle:=vbNormalFocus)    Application.Wait Now + TimeValue("0:00:2")    SendKeys "%vpc"    SendKeys "^a", True    Application.Wait Now + TimeValue("00:00:2")    ' send key to copy     SendKeys "^c"    ' wait 2 secs     Application.Wait Now + TimeValue("00:00:2")      ' activate this workook and paste the data        ThisWorkbook.Activate        Set ws = ThisWorkbook.Sheets(HK)        Range("O1:O5").Select        ws.Paste        Application.Wait Now + TimeValue("00:00:3")        Application.CutCopyMode = False        Application.Wait Now + TimeValue("00:00:3")       Call Shell("TaskKill /F /IM AcroRd32.exe", vbHide)       ' send key to close pdf file        SendKeys "^q"       Application.Wait Now + TimeValue("00:00:3") Next Sheet Workbooks(Filename).Close SaveAs = True Filename = Dir()LoopNextApplication.ScreenUpdating = TrueEnd Sub

I wrote the piece of code to copy from pdf and csv to the macro enabled workbook and you may need to fine tune as per your requirement

Regards,Hema Kasturi