[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 60
  • Last Modified:

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.

SNAG-0032.bmp
Am I doing something wrong or is the webbrowser just not suitable for this task?
0
AndyAinscow
Asked:
AndyAinscow
  • 6
  • 6
1 Solution
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
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

0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
>>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....
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
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)
http://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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
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.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
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
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
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.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
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.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
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.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
sample database and code works.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
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.
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
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
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
That is why the .navigate fails and how to solve it.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now