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
  • 6
  • 6
LVL 19
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 19
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:
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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 19
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 19
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 19
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 19
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 7 33
Access 2016 - Blank, Null, or empty string? 9 31
Combobox row source 2 20
Access 2007 - Create Query That Returns One Record Per Customer 4 30
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

776 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