Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-29-2015, 07:48 AM
7mgte 7mgte is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jan 2015
Posts: 4
7mgte is on a distinguished road
Default VBA to Open a Document to a Specific Heading


Greetings All!

What I am trying to do is open a MS Word document from Access and then move to a specific heading number (eg 3.4.2.1). The document name and heading number exist in Access, and I have done a simple "open a document" using VBA from Access. Where I am stuck is trying to move to the heading number (in a heading style). I have searched through a lot of documentation, but never found a property that contains the heading number.

Any thoughts?

Thanks!

Don
Reply With Quote
  #2  
Old 01-29-2015, 08:13 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 2,448
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

I don't work in Access, but the principles of opening a document and processing it from VBA are fairly universal and go something like the following code. One way to open at your heading would would be to set a named bookmark at the start of that location (say "bkHeading1") and then select that bookmark e.g.

Code:
Dim wdApp As Object
Dim wdDoc As Object
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    Set wdDoc = wdApp.Documents.Open(Filename:="C:\Path\DocName.docx")
    wdDoc.Range.bookmarks("bkHeading1").Select
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-29-2015, 08:22 AM
7mgte 7mgte is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jan 2015
Posts: 4
7mgte is on a distinguished road
Default

Graham,

I had actually considered the bookmark approach based on some articles I saw on using hyperlinks to open a document to a particular place in a document. Unfortunately, the documents exist already and do not have bookmarks. Also, in my hyperlink experiment on a test document with a bookmark, while the hyperlink opened the document, it did not move to the bookmark. Not sure if that is the result of some policy issue.

Another avenue I started pursuing was do a text search for the section number to reach the TOC. The figure out how to "Ctl-Click" to jump from the TOC to the desired section. But that seems like a kluge.

It amazes me that heading numbers for an open document are not available programmatically. For a comment extraction tool I tried getting heading numbers and could never find a programmatic source.

Thanks for the suggestion!

Don
Reply With Quote
  #4  
Old 01-29-2015, 08:58 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 17,820
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Since you know the number (which I assume is part of the heading text), you could use Find in VBA to find that number in the relevant Heading Style. For example:
Code:
Dim wdDoc As Document, StrFnd As String
'insert the code that references the document here
StrFnd = "string from database"
With wdDoc.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWholeWord = True
    .Text = StrFnd
    .Style = "Heading 1"
    .Replacement.Text = ""
    .Execute
  End With
  If .Find.Found = True Then
    'do something with the heading
  End If
End With
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 01-30-2015, 12:06 AM
Guessed's Avatar
Guessed Guessed is offline Windows 7 32bit Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 781
Guessed is on a distinguished road
Default

This following is a function you could use to go to the right heading. You will need to pass in a string and the function will jump to the first heading that contains that string. The string can also be the heading number. Note I included a sub at the end to show how you might call the function.
Code:
Function GoToMyHeading(str As String)
  Dim doc As Document
  Dim vHeadings As Variant
  Dim v As Variant
  Dim i As Integer
  
  Set doc = ActiveDocument
  vHeadings = doc.GetCrossReferenceItems(wdRefTypeHeading)
  i = 0
  
  For Each v In vHeadings
    i = i + 1
    If InStr(v, str) > 0 Then
      Selection.GoTo What:=wdGoToHeading, Which:=wdGoToAbsolute, Count:=i
      Exit Function
    End If
  Next v
  
  MsgBox "Couldn't find the heading containing: " & str
End Function

Sub GoToMyHeading_Test()
  GoToMyHeading "3.2.1"
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 02-02-2015, 02:19 PM
7mgte 7mgte is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jan 2015
Posts: 4
7mgte is on a distinguished road
Default Solved!

Andrew,

Awesome! The GetCrossReferencedItems/wdRefTypeHeading is the key! I need to read up on that a bit more. This might solve another problem for me too!

Had to make two minor tweeks to your example code:

If InStr(Trim(v), Trim(strTargetHeading)) = 1 Then

added the Trim() and set the find location to one. The first test case I ran using "3.1" found "2.3.1".

Thanks for the help!

Don
Reply With Quote
  #7  
Old 02-02-2015, 02:48 PM
Guessed's Avatar
Guessed Guessed is offline Windows 7 32bit Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 781
Guessed is on a distinguished road
Default

That's a good pickup on the need for '=1' when passing in the heading number. As you probably worked out I didn't test it properly. I had thought it would be good to have the function also return hits if you passed in the text from the heading instead of just the number but I guess that was just a little too ambitious.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
heading numbering, search, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Restore Heading 1, Heading 2, etc. within a Word Document cheech1981 Word 9 01-11-2017 02:14 AM
Formula to open external file with specific program (like open with) pemartins Excel 16 02-24-2014 11:39 PM
Deleting A blank Line that has a specific heading style , word 2010 & 2013 SteveWcg Word 5 01-08-2014 10:37 PM
Macro to replace one specific heading style with another ubns Word VBA 44 09-04-2012 08:17 PM
Using Hyperlink to open Word Document at Specific Header rossi45 Word 2 05-04-2012 06:03 PM


All times are GMT -7. The time now is 06:29 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft