View a pdf in a vba userform (SW 2016)

Hello

To display a PDF on a userform, I found two main solutions on the internet: the addition of an "Adobe PDF Reader" controller or the addition of a "WebBrowser" controller .

I chose the solution of adding an "Adobe PDF Reader" controller , but I can't get it to work. Here is the procedure I followed:

  1. Installing Acrobat Reader DC
  2. In the VBA editor, on the "Toolbox"  => right-click on the background of this toolbox  => "Additional Controls... "  => check "Adobe PDF Reader"

    => When I want to put the "Adobe PDF Reader" controller on the userform I get an error message: "Item not found."

 

=> If someone has the right procedure and code to use this "Adobe PDF Reader" controller , it would help me a lot.

 

Thank you for your help.

Hello

I don't have this problem when adding the Adobe PDF Reader control. Basically it shows me an empty box, to display the pdf: pdf1.loadfile "c:\xxxx" file path.

Is adobe reader installed correctly? Maybe also a version issue.

Hello

There is a fairly simple method to insert an image into a Userform. You need to put an image area in your Userform and then go to the code part and implement this: 

Image1.Picture = LoadPicture("C:\Users\bem1. DIMECOALIPRESSE\Desktop\TinyStraightMaster\coucou.jpg")

with the path of your image in jpg format.

I hope I helped you, good luck.

Dimitri. 

1 Like

Finally, after doing many tests, I tried to find the solution of adding an "Adobe PDF Reader" controller to display a pdf in a userform.

So I switched to the other possible solution: the addition of a "WebBrowser" controller 

I got there with the following code:

Private Sub UserForm_Initialize()

Me.WebBrowser1.Navigate "about:blank"
Me.WebBrowser1.Document.write "<HTML><Body><embed src=""C:\xxxx\xxxxx\xxxxx\test 1.pdf"" width=""100%"" height=""100%"" /></Body></HTML>"

End Sub

 

This method shows me the pdf with Adobe Acrobat Reader DC in the userform, but the problem I still have is that Adobe Acrobat Reader DC shows me all its toolbars.

I can't display the pdf automatically in full window/screen (without the toolbars).

Anyone have a solution for that?

 

Hello,

I managed to find a solution, you have to go from the parameters to the document.

In place of the following line of code:

Me.WebBrowser1.Document.write "<HTML><Body><embed src=""C:\xxxx\xxxxx\xxxxx\test 1.pdf"" width=""100%"" height=""100%"" /></Body></HTML>"

Platter:

Me.WebBrowser1.Navigate "C:\xxx\xxx.PDF#pagemode=none"

It's the #pagemode=none that makes you have only the document without the interface.

 

You have the list of parameters available here: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf

5 Likes

Thank you KVuilleumier !

The code you propose works, except for something (in my case), let me explain:

1. The pdfs I display have hyperlinks that display other pdfs,

2. I chose to use a "WebBrowser" for its navigation (WebBrowser1.GoBack/GoForward) which allows me to display the following and previous pdfs.

As a result, when I select a hypertext link the pdf that is displayed is not in FullScreen, if I update each display by adding "#pagemode=none" to the path of the displayed pdf, it is the navigation (WebBrowser1.GoBack/GoForward) that no longer works.

What you offer me KVuilleumier is already a good step forward, but if I manage to have the FullScreen and the GoBack/GoForward navigation it would be great.

Would he have a way to assign this <pagemode=none> to the viewer (WebBrowser1) rather than to the pdf to be displayed? (I haven't managed to do it yet)

 

Hello

Possibility to put the code to test?

2 Likes

I don't know your level in VBA, but you always have the possibility to write the GoBack and GoForward function yourself by memorizing the links (Scratch the WebBrowser event). That way, when you click on a link, you have the possibility to refresh by adding the pagemode and then to come back you use your function.

1 Like

Hello

Cyril.f, I'm going to put some code so you can test.

KVuilleumier, that's what I did: I wrote the GoBack and GoForward functions myself.

On the other hand, I still have a display problem for hypertext links. Here's my code:

Private Sub UserForm_Initialize()

     'J-affiche le 1ere PDF'
     WebBrowser1.Navigate "C:\xxx\xxx\test.PDF#pagemode=none"

End Sub

'------------------------------------------------------------------------------------'

Private Sub WebBrowser1_TitleChange(ByVal Text As String)

    'Si l-URL ne contient pas "#pagemode=none", je l-ajout et recharge la page'
    If Right(WebBrowser1.LocationURL, 14) <> "#pagemode=none" Then
        WebBrowser1.Navigate2 (WebBrowser1.LocationURL & "#pagemode=none")
    End If

End Sub

 

When I select my hyperlink, a new pdf is displayed, but not in full window/screen.

On the other hand, if after displaying the new pdf I click on a CommandButton that launches a Refresh, the pdf will put it in full window/screen.

Private Sub CommandButton1_Click()

    WebBrowser1.Refresh

End Sub

So I tried to drag  this WebBrowser1.Refresh everywhere, to have a full window/screen display without having to click on a button. But no way, I couldn't do it. 

Maybe I'm using the wrong WebBrowser event, but I think I've tested pretty much everything.

Anyone have a solution?

 

Strange Event:

Compared to the code previously presented, if I add a WebBrowser1.Refresh in the WebBrowser event (just before the End If) it doesn't work, but if I also add an "ok" Msgbox (just before the WebBrowser1.Refresh) it works after closing the MsgBox.

The code:

Private Sub UserForm_Initialize()

     'J-affiche le 1ere PDF'
     WebBrowser1.Navigate "C:\xxx\xxx\test.PDF#pagemode=none"

End Sub

'------------------------------------------------------------------------------------'

Private Sub WebBrowser1_TitleChange(ByVal Text As String)

    'Si l-URL ne contient pas "#pagemode=none", je l-ajout et recharge la page'
    If Right(WebBrowser1.LocationURL, 14) <> "#pagemode=none" Then
        WebBrowser1.Navigate2 (WebBrowser1.LocationURL & "#pagemode=none")
        MsgBox "ok"
        WebBrowser1.Refresh    
    End If

End Sub

 Very strange, isn't it?

Hello

Rather than displaying the pdf and then checking if it is in full screen, why don't you do your test before displaying it?

Dim Adresse As String
    
Adresse = "C:\xxx\xxx\test.PDF"
    
If Right(Adresse, 14) <> "#pagemode=none" Then
   Adresse = (Adresse & "#pagemode=none")
End If
    
WebBrowser1.Navigate Adresse

Kind regards

Hello

D.Roger, I agree to have you, it would be great if I could do that, but in my case, I didn't succeed.

Reminder of my case: I display a 1st PDF in full screen (no problem for now). This PDF has hyperlinks that display other PDF(s), and it is by clicking on its hyperlinks that I am unable to display the new PDF(s) in full screen.

In what you propose (if I understood correctly), I should be able to stop the redirect of the WebBrowser1.Navigate when I click on the hyperlink while retrieving the path that the hyperlink returns to modify it (add "#pagemode=none") and relaunch WebBrowser1.Navigate with the new modified path.

If you know how to do that, share your STP code.

 

Hello

I don't know enough about this type of control and unfortunately don't have any code to offer you, I would need the code of your GoBack and GoForward functions to see ...

On the other hand, as for your "Strange Event" it is not that strange, you are trying to refresh your page before the end of its loading, your Msgbox "ok" (just before the WebBrowser1.Refresh) serves as a tempo, replace it with:

While WebBrowser1.ReadyState <> READYSTATE_COMPLETE Or WebBrowser1.Busy = True
    DoEvents
Wend

This allows you to wait for the page to finish loading before refreshing.

Kind regards

3 Likes

Thank you d.roger,

That's what I was missing, except that in my code the While/Wend loop throws an error, so I used a Do Until/Loop loop:

Do Until WebBrowser1.ReadyState = READYSTATE_COMPLETE Or WebBrowser1.Busy = False
   DoEvents
Loop

 

Hello

To avoid the error on the While loop, you could also put only:

WhileTrue
    DoEvents
Wend

for opening local PDF files and:

While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend

For the display of web pages if necessary, pages that are better forbidden in your macro unless all the security related to Internet browsing is taken.

For testing, I also attach a piece of macro that allows you to display a PDF in an "Adobe PDF Reader" control corresponding to your initial request, you have to edit it and change the LoadPDF line "C:\xxx\xxx.PDF#pagemode=none", 1 to put the path of your PDF file.

Kind regards


macroadobepdfreader.swp