Jason_S
02-22-2017, 01:50 PM
Problem I am having: Below code, while accurately finding the target CrossReferencedItem, does NOT select the correct heading.... Instead of counting absolute headings, code appears to be counting by paragraphs.... See greater explanation of problem below the code herein.
Background:
I have a document using outlined numbering, legal style (i.e., 1, 1.1, 1.1(A), 1.1(A)(i)). Each heading, whatever the level, has a single word as the Section Title, like this:
1. Introduction. [Formatted as Heading 1 style]
[A bunch of text here, as separate paragraph, formatted as normal style]
1.1 For Full-Time Employees. [Formatted as Heading 2 Style]
[A bunch of text here, as separate paragraph, formatted as normal style]
1.2 For Temporary Employee. [Formatted as Heading 2 Style]
[A bunch of text here, as separate paragraph, formatted as normal style]
2. Management Resources. [Formatted as Heading 1 Style]
[A bunch of text here, as separate paragraph, formatted as normal style]
In plain english, I'm trying to write a macro that says, for example, the following, assuming MyString = "Management Resources": Here is plain text idea I need to translate to code "If MyString = "Management Resources" then Select the text "Management Resources" at the appropriate section (whether Heading 1, Heading 2, etc.) and insert a bookmark.
========== Code I am working with (two Subroutines)
Sub Function_GotoDocumentSectionByName(ClauseTitle as string)
Dim doc As Document
Dim MyHeadings As Variant
Set doc = ActiveDocument
MyHeadings = doc.GetCrossReferenceItems(wdRefTypeHeading)
For x = 1 To UBound(MyHeadings)
If InStr(MyHeadings(x), ClauseTitle) > 0 Then
MsgBox ("Found match in " & x & "th CrossReferenceItem")
Selection.GoTo What:=wdGoToHeading, Which:=wdAbsolute, Count:=x
Selection.Collapse wdCollapseStart
Selection.Extend character:="."
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
' Add Code Here to Add the Bookmark (I don't need help with this...took out this code to make this question easier to understand)
End If
Next x
End sub
Sub test_FunctionGotoHeading()
Function_GotoContractSection ("Management Resources")
End sub
=================== END OF CODE
When I run test_FunctionGoToHeading, the code accurately determines that "Management Resources" is the title of the 4th item in the array created by GetCrossReferenceItems(wdRefTypeHeading). But the selection point is the paragraph "1.1 For Full-Time Employees" which is the 2nd heading, and the 3rd paragraph. So, despite knowing that the 4th heading in the GetCrossReferenceItems array is the one I'm after, Selection.GoTo What:=wdGoToHeading, Which:=wdAbsolute, Count:=4 doesn't select it....
So please help: I need to replace the line "Selection.GoTo What:=wdGoToHeading, Which:=wdAbsolute, Count:=x" with what????
Potential Ideas:
- If I can get the proper ListString for the section titled "Management Resources"
[ListString = "2."], what is the proper syntax for range.liststring("2.").select?
- Is there any syntax that would be the equivalent of "GetCrossReferenceItems(4).select"?
Background:
I have a document using outlined numbering, legal style (i.e., 1, 1.1, 1.1(A), 1.1(A)(i)). Each heading, whatever the level, has a single word as the Section Title, like this:
1. Introduction. [Formatted as Heading 1 style]
[A bunch of text here, as separate paragraph, formatted as normal style]
1.1 For Full-Time Employees. [Formatted as Heading 2 Style]
[A bunch of text here, as separate paragraph, formatted as normal style]
1.2 For Temporary Employee. [Formatted as Heading 2 Style]
[A bunch of text here, as separate paragraph, formatted as normal style]
2. Management Resources. [Formatted as Heading 1 Style]
[A bunch of text here, as separate paragraph, formatted as normal style]
In plain english, I'm trying to write a macro that says, for example, the following, assuming MyString = "Management Resources": Here is plain text idea I need to translate to code "If MyString = "Management Resources" then Select the text "Management Resources" at the appropriate section (whether Heading 1, Heading 2, etc.) and insert a bookmark.
========== Code I am working with (two Subroutines)
Sub Function_GotoDocumentSectionByName(ClauseTitle as string)
Dim doc As Document
Dim MyHeadings As Variant
Set doc = ActiveDocument
MyHeadings = doc.GetCrossReferenceItems(wdRefTypeHeading)
For x = 1 To UBound(MyHeadings)
If InStr(MyHeadings(x), ClauseTitle) > 0 Then
MsgBox ("Found match in " & x & "th CrossReferenceItem")
Selection.GoTo What:=wdGoToHeading, Which:=wdAbsolute, Count:=x
Selection.Collapse wdCollapseStart
Selection.Extend character:="."
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
' Add Code Here to Add the Bookmark (I don't need help with this...took out this code to make this question easier to understand)
End If
Next x
End sub
Sub test_FunctionGotoHeading()
Function_GotoContractSection ("Management Resources")
End sub
=================== END OF CODE
When I run test_FunctionGoToHeading, the code accurately determines that "Management Resources" is the title of the 4th item in the array created by GetCrossReferenceItems(wdRefTypeHeading). But the selection point is the paragraph "1.1 For Full-Time Employees" which is the 2nd heading, and the 3rd paragraph. So, despite knowing that the 4th heading in the GetCrossReferenceItems array is the one I'm after, Selection.GoTo What:=wdGoToHeading, Which:=wdAbsolute, Count:=4 doesn't select it....
So please help: I need to replace the line "Selection.GoTo What:=wdGoToHeading, Which:=wdAbsolute, Count:=x" with what????
Potential Ideas:
- If I can get the proper ListString for the section titled "Management Resources"
[ListString = "2."], what is the proper syntax for range.liststring("2.").select?
- Is there any syntax that would be the equivalent of "GetCrossReferenceItems(4).select"?