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:
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.
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now