Link to home
Start Free TrialLog in
Avatar of AndyAinscow
AndyAinscowFlag for Switzerland

asked on

Display word document on access form - webbrowser control.

I would like to display a many page word document (saved as docx file on disc) on an access (accdb format) database form.  I can use an unbound object but that will not display any scroll bars and will only display the first page.  I have seen on the internet suggestions to use the webbrowser control.
I can set the source for the webbrowser (properties in design mode) to the file on disc.  When I show the form however I get a dialog asking if I want to open or save the word document.  Weird and not at all what I want.  Another suggestion was to use the form:current event to use the webbrowsercontrol.Navigate functionality to set the path in code.  So I try that and get a runtime error that the webbrowsercontrol.Navigate property isn't recognised.

User generated image
Am I doing something wrong or is the webbrowser just not suitable for this task?
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

it would be much better to store path\filename in the database, keep the document external, and open the document using Word:
application.FollowHyperlink "c:\path\filename.docx"

Open in new window

Avatar of AndyAinscow

ASKER

>>it would be much better to.....open the document using Word:
Actually it wouldn't.  It is being used on single monitor systems and the access based interface is opened maximised - so the user would have to keep switching between apps.  Which is why I specified ...display word doc on access form....
the attached access database example shows 2 methods:
1. using a bound object frame
2. and using a web browser control.
To render in the web browser, the document must be converted to a format that can be rendered.

Path\Filenames are stored in a table that the form is bound to. There is a command button to update the display in the control.  Assuming the document is Word.  
Private Sub cmd_ShowDocument_Click()
'161016 crystal
   Dim sPathFile As String _
      , sPathFileHTML As String
   Dim oWordApp As Object _
      , oDoc As Object
'   Dim oWordApp As Word.Application _
'      , oDoc As Word.Document
   With Me.PathFilename
      If IsNull(.Value) Then
         MsgBox "No document to show", , "Path\Filename not specified"
         Exit Sub
      End If
      sPathFile = .Value
   End With
   With Me.OLEBound_PathFile
      .SourceDoc = sPathFile
      .Action = acOLECreateLink
   End With
   'save word doc as HTML
   sPathFileHTML = CurrentProject.Path & "\temp.html"
   Set oWordApp = CreateObject("Word.Application")
   Set oDoc = oWordApp.Documents.Open(sPathFile)
   oDoc.SaveAs2 FileName:=sPathFileHTML, FileFormat:=8 'wdFormatHTML
   oDoc.Close False
   oWordApp.Quit
   
   'open in browser control
   
   With Me.WebBrowser__PathFile
      .Navigate sPathFileHTML
   End With
   
   Set oDoc = Nothing
   Set oWordApp = Nothing
   
End Sub

Open in new window

if you double-click the object in the bound object frame, another window opens with scrollbars.  the bound object frame loads faster because it doesn't have to convert the document first.

You will probably want to add error handling.

basic error handling code for VBA (3:48)
https://www.experts-exchange.com/videos/1478/Excel-Error-Handling-Part-1-Basic-Concepts.html

there are more error handling videos in this series.

Example database:
BoundObjectFrame-vs-WebBrowserContro.zip
Thanks, I'll look into both of those tomorrow.

I've got a question specific to the unbound object control here:
https://www.experts-exchange.com/questions/28976714/Display-word-document-on-access-form-unbound-object.html

I wanted this question specific to the webbrowser control.
you're welcome

The example shows 2 methods, one of which uses a web browser control. The other uses a BOUND object frame which is updated depending on the desired path\filename
Hmmmm.
I have converted a dcox file into a htm file (save as option in word).

Adding a webbrowser to a form and setting the control source file to the htm file will open the document in the browser.  I have scroll bars.  So far so good.  The bad bit is this isn't a word docx file. I know you have supplied code to convert a docx file into the necessary format but I worry about the performance hit.  What is rather more of a problem is your code:
With Me.WebBrowser__PathFile
      .Navigate sPathFileHTML
   End With

Open in new window

doesn't work.  In fact it is effectively what I posted in the question and has exactly the same error which you can see in my screenshot.

ps.  The other code with the bound object I would have preferred in my other question but for completeness here - no scroll bars so I can't navigate through the document.  This was the same problem as the unbound object control.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you would have opened the sample database I posted, you will see that IS the web browser control that was used ... and the code DOES work.
sample database and code works.
Fair point, but didn't you have warning bells going off because I posted on two separate occasions the .navigate on my webbrowser control resulted in a run time error ?

ps.  At that time I didn't realise there were two different webbrowser controls with different API's.  I was using, for me, the webbrowser control.  Which was the first one I met on the 'controls' toolbar when designing a form.
that is why I took the time to make a sample ... so that you could see which control to use. I meant to tell you how I put it in too but forgot -- you could have asked ...

I also took the time to write conversion code for Word so that the web browser control could be used for your documents.  That code could be put into its own procedure to loop through all of your word docs and save them as html so they didn't have to be converted on the fly using a temporary file.

I realize this has been frustrating for you ... just trying to help as best I can
That is why the .navigate fails and how to solve it.