Home > database >  Link to a location in a Word document from Access form (using VBA)?
Link to a location in a Word document from Access form (using VBA)?

Time:10-10

I have an Access database that has controls that link to a User Guide (Word file) on our shared drive. In that user guide, I have bookmarks to different sections of the user guide (for example, how to use the "Agreements" subform).

At present, my controls just open the Word doc to the beginning. I'm wondering if there is a way to automatically open the Word doc to a specific location (e.g., to the "Agreements" bookmark).

Here is the code I have so far to open the Word doc:

Private Sub cmdUserGuide_Click()

    Dim LWordDoc As String
    Dim oApp As Object

    LWordDoc = "\\shareddrive\UserGuide.docx"

   If Dir(LWordDoc) = "" Then
      MsgBox "Document not found."

   Else
      'Create an instance of MS Word
      Set oApp = CreateObject(Class:="Word.Application")
      oApp.Visible = True
    
      oApp.Documents.Open FileName:=LWordDoc
   End If
End Sub

Thanks for your help!

CodePudding user response:

You can use the GoTo method to get the job done:

WordApplication.ActiveDocument.Selection.Goto what:=wdgotobookmark, Name:="YourBookmark"

CodePudding user response:

If you can use labels to open the document, you don't need any code at all.

Use its Hyperlink property to set the file name and append the bookmark name after a #.

\\shareddrive\UserGuide.docx#MyBookmark

or

file://C:\path\mydoc.docx#SomeBookmark

(file:// is not actually needed)

  • Related