PDA

View Full Version : Open PDF by selecting file



m_court15
06-28-2011, 01:46 PM
ok, now I am trying to create an open dialog box that you can select a pdf file and open it.

I am trying to have the open box pop up when the form is started, then the user is suppose to select the pdf file they want to view. Then, the file should populate the acroPDF1 box.

Private Sub UserForm_Initialize()
Dim As FileDialog
Set dlgopen = Application.FileDialog(msoFileDialogOpen)
Application.FileDialog(msoFileDialogOpen).Filters.Add "PDF", "*.pdf"
With dlgopen
.InitialFileName = "c:\users\matt courtright\desktop\test userform\"
.AllowMultiSelect = False
.Show
End With

AcroPDF1.LoadFile "C:\users\matt courtright\desktop\test userform\p10298.pdf"
End Sub


I am trying to combine these two sets of code somehow. Hope I explained it enough; it might need more clarification.

Thanks,
Matt

m_court15
06-29-2011, 06:26 AM
So this is where I'm at:

Private Sub UserForm_Initialize()
Dim fd As FileDialog
Dim ffs As FileDialogFilters
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
Set ffs = .Filters
With ffs
.Clear
.Add "PDF", "*.pdf"
End With
.AllowMultiSelect = False
If .Show = False Then Exit Sub
AcroPDF1 = LoadPDF(.SelectedItems(1))
End With
Exit Sub
End Sub

AcroPDF1 = LoadPDF(.SelectedItems(1)) <-----
This is the line of code I am having trouble with. This is the part that should populate my acroPDF1 control once the file is chosen.
I'm a little stumped.

m_court15
07-08-2011, 01:32 PM
bump. Any ideas?

Jay Freedman
07-09-2011, 11:15 AM
Instead of the line that's causing the problem, use

AcroPDF1.LoadFile (.SelectedItems(1))

The LoadFile method is a member of the AcroPDF1 object, which you'll find in the popup list when you type a dot after AcroPDF1. There is no such thing as a LoadPDF function, and even if there was one you wouldn't assign its result to AcroPDF1.

m_court15
07-11-2011, 05:27 AM
ok, thanks. I appreciate the response.

So is there a way to open a selected pdf through an "open" dialog box and have it open within my userform?

Jay Freedman
07-11-2011, 04:49 PM
Sure, there's a way... but this is technically no different from using the Application.FileDialog as in your original post; either one just acquires the desired filename:

Private Sub UserForm_Initialize()
With Dialogs(wdDialogFileOpen)
.Name = "*.pdf"
If .Display = -1 Then
AcroPDF1.LoadFile WordBasic.FileNameInfo(.Name, 1)
End If
End With
End Sub
For documentation of the FileNameInfo function, see http://www.word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm.

m_court15
07-12-2011, 05:40 AM
Thanks Jay! Works perfect. I had been trying to figure this out for a long time... Seems so simple now. Thanks again!