Display word document on access form - webbrowser control.

Posted on 2016-10-16
Last Modified: 2016-10-22
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.

Am I doing something wrong or is the webbrowser just not suitable for this task?
Question by:AndyAinscow
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
LVL 21
ID: 41845678
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

LVL 44

Author Comment

ID: 41845698
>>it would be much better 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....
LVL 21
ID: 41845759
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
   '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)

there are more error handling videos in this series.

Example database:
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 44

Author Comment

ID: 41845766
Thanks, I'll look into both of those tomorrow.

I've got a question specific to the unbound object control here:

I wanted this question specific to the webbrowser control.
LVL 21
ID: 41845777
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
LVL 44

Author Comment

ID: 41846202
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.
LVL 44

Accepted Solution

AndyAinscow earned 0 total points
ID: 41846776
OK.  Looks like the bit I found on the internet wasn't correct in that the webbrowser control can't open a docx format file directly.

I've also sorted out why the error happens with .navigate.

There is a webbrowser control on the access 'controls' toolbar.  That doesn't support a .navigate property.  One has to use the following format for a file:
   With Me.Webbrowser6
      .ControlSource = "=(""G:\xxxxxxxx.htm"")"
   End With

Open in new window

There is another webbrowser control (same name - pretty useful!!) available under the activex controls when one searches for all available activex controls.  That does supprt the .navigate property.
LVL 21
ID: 41846804
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.
LVL 21
ID: 41846813
sample database and code works.
LVL 44

Author Comment

ID: 41846833
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.
LVL 21
ID: 41846834
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
LVL 44

Author Closing Comment

ID: 41855096
That is why the .navigate fails and how to solve it.

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question