|
-
Jul 27th, 2007, 10:54 PM
#1
Thread Starter
Junior Member
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
-
Jul 28th, 2007, 12:10 AM
#2
Re: How to Open PDF Files Via Visual Basic Macro Code ?
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 28th, 2007, 12:15 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 28th, 2007, 10:23 AM
#4
Thread Starter
Junior Member
Re: How to Open PDF Files Via Visual Basic Macro Code ?
 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
-
Jul 28th, 2007, 02:16 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 29th, 2007, 04:52 PM
#6
Thread Starter
Junior Member
Re: How to Open PDF Files Via Visual Basic Macro Code ?
 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
-
Jul 29th, 2007, 05:11 PM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 17th, 2009, 04:29 PM
#8
New Member
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
-
Feb 17th, 2009, 06:49 PM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 17th, 2009, 09:34 PM
#10
Addicted Member
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
-
Feb 18th, 2009, 02:28 AM
#11
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 18th, 2009, 06:46 AM
#12
Addicted Member
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.... ???
-
Feb 18th, 2009, 05:16 PM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 26th, 2009, 03:43 AM
#14
New Member
Re: How to Open PDF Files Via Visual Basic Macro Code ?
 Originally Posted by RobDog888
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.
-
Aug 27th, 2009, 06:56 PM
#15
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 28th, 2009, 04:05 PM
#16
New Member
Re: How to Open PDF Files Via Visual Basic Macro Code ?
 Originally Posted by RobDog888
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|