Results 1 to 16 of 16

Thread: How to Open PDF Files Via Visual Basic Macro Code ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    25

    How to Open PDF Files Via Visual Basic Macro Code ?

    How can I code a Macro in Excel to search in a preset directory and pull out, aka open/run
    a certain Adobe Acrobat .pdf file automatically?

    Is this even possible to code in Excel Macro or is it out of its scope of what Excel VB code can do?


    Right now I have a very repetitive task where depending on the value of a certain field in Excel I have to search for the right/corresponding .pdf file and open it up do work in the .pdf as well as the Excel worksheet... I have to do like hundreds of this....

    So is there a quick way to code in Excel that a certain value will open a certain file named .pdf acrobat file? Can other programs/files be called upon and ran/started/opened from within Excel Macro?

    Thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Thread Moved
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    You can use the ShellExecute API to run any file in its associated program. So execute the pdf file and it opens in Acrobat Reader if you have AR installed.

    Code:
    'In a Module...
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
                        ByVal hwnd As Long, _
                        ByVal lpOperation As String, _
                        ByVal lpFile As String, _
                        ByVal lpParameters As String, _
                        ByVal lpDirectory As String, _
                        ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Sub RunMe()
        ShellExecute Me.hwnd, "open", "C:\MyFile.pdf", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    Then to get the files in some directory you will want to use the FindFirstFile and FinNextFile APIs (search the Forums fo r code examples).
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    25

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Quote Originally Posted by RobDog888
    You can use the ShellExecute API to run any file in its associated program. So execute the pdf file and it opens in Acrobat Reader if you have AR installed.

    Code:
    'In a Module...
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
                        ByVal hwnd As Long, _
                        ByVal lpOperation As String, _
                        ByVal lpFile As String, _
                        ByVal lpParameters As String, _
                        ByVal lpDirectory As String, _
                        ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Sub RunMe()
        ShellExecute Me.hwnd, "open", "C:\MyFile.pdf", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    Then to get the files in some directory you will want to use the FindFirstFile and FinNextFile APIs (search the Forums fo r code examples).

    Thanks for the code. When I try to compine this it say's invalid use of the Me. word. - > "Me.hwnd" What doesn't Me stand for? Is it spelled right?


    Thanks

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Sorry, change that to "ThisWorkbook" (no quotes). Me is the VB 6 object that represents the current form or class that you are in with the code. In Excel the equilivalent is ThisWorkbook.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    25

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Quote Originally Posted by RobDog888
    Sorry, change that to "ThisWorkbook" (no quotes). Me is the VB 6 object that represents the current form or class that you are in with the code. In Excel the equilivalent is ThisWorkbook.
    Hello,

    When I compile it it gives "Compile Error: Method or Data member not found"
    and highlights ThisWorkbook.hwnd

    When I compile it as just "ThisWorkbook" I get this error msg:

    Runtime error 438
    Object doesn't support this property or method



    Do you know what I am doing wrong here?

    Thanks

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    sorry, yet again, I was in too much of a hurry. Change ThisWorkbook.hwnd to Application.Hwnd
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    New Member
    Join Date
    Feb 2009
    Posts
    1

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Many thanks for this code, which is really helpful.....!

    Can you please let me know how to open multiple pdf files in the same folder

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    you would enumerate the files in the desired folder using FindFirstFile and FindNextFile API calls. Then with each result you would call the RunMe function but first make an edit to it to take an argument of the filepath with file name.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    with the code of robdog... add this macro

    Code:
    Sub openfile()
    Dim strpath As String
    Dim openfile As Variant
    
    ChDrive "C:\"
    ChDir "C:\testing\"   'change to your own folder name
    openfile = Application.GetOpenFilename _
    (Title:="File to open", _
    FileFilter:="PDF Files *.pdf (*.pdf),")
    
    If openfile = False Then
    MsgBox "No file specified.", vbExclamation, "Bamm!"
    Exit Sub
    Else
    End If
    
        ShellExecute Application.hwnd, "open", openfile, vbNullString, "C:\", SW_SHOWNORMAL
    End Sub

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    But that will make it manual as the user will have to select each file individually.

    I read the post as opening all pdf files in a particular folder, not opening one by one with manual selection but we need to OP to clear it up
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    hmm..yup rob dog the user will do it manually, he need to loop on the folder if he wants to open multiple files .. but don't know why he need to open multiple files.... ???

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Only veeeresh knows why and how he needs this to work. Still awaiting his reply.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Quote Originally Posted by RobDog888 View Post
    sorry, yet again, I was in too much of a hurry. Change ThisWorkbook.hwnd to Application.Hwnd
    the code works absolutely fine..!!!!!!!!!!!!!!!!!!!

    just need to know... how to read data from that pdf ..or to export data from that pdf to an excel sheet? please .................it will be of great help!!!!!

    thanks in advance.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    One way would be to read the pdf file specification documents. Then open the pdf file in binary mode and look for the specification tags that identify what you want to read.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Re: How to Open PDF Files Via Visual Basic Macro Code ?

    Quote Originally Posted by RobDog888 View Post
    One way would be to read the pdf file specification documents. Then open the pdf file in binary mode and look for the specification tags that identify what you want to read.
    please ..help with code..!!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width