PDA

View Full Version : [SOLVED:] How can I SELECT the paragraph of a CrossReferenceItem?



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"?

gmaxey
02-22-2017, 03:23 PM
First, with what you have provided, when you run test_FunctionGoToHeading, the code doesn't accurately do anything because you get a compile error as there is no function (provided) Function_GoToContractSection.

Second, neither are functions as a functions return a value


Sub Test()
GoToHeading "Management Resources", "MR"
End Sub
Sub GoToHeading(strText As String, strBMName As String)
Dim lngIndex As Long
Dim oRng As Word.Range
Dim varHeadings As Variant
varHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
For lngIndex = 1 To UBound(varHeadings)
If InStr(varHeadings(lngIndex), strText) > 0 Then
MsgBox ("Found match in " & lngIndex & "th CrossReferenceItem")
Selection.GoTo What:=wdGoToHeading, Count:=lngIndex
Set oRng = Selection.Paragraphs(1).Range
ActiveDocument.Bookmarks.Add strBMName, oRng
End If
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

Jason_S
02-22-2017, 03:59 PM
Greg, thank you for the education. I tried your code above, but I still get the same result -- that is, the proper CrossReferenceItem is "4", but the line "Selection.GoTo What:=wdGoToHeading, Count:=lngIndex" delivers me to the paragraph BELOW Section 1.1 (which I confirmed is formatted as Normal, not as a numbered heading). This is the 4th line of text, but not the 4th Heading.... Not sure why it doing this.. very odd.

gmaxey
02-22-2017, 04:06 PM
Can you attach a sample document illustrating the problem?

Jason_S
02-22-2017, 04:23 PM
Sure here is a link: https://dl.dropboxusercontent.com/u/33423772/How%20can%20I%20SELECT%20the%20paragraph%20of%20a%20CrossReferenceItem.docx

gmaxey
02-22-2017, 04:36 PM
Here is worked as expected. See image.

Jason_S
02-22-2017, 04:40 PM
It must be a bug in the Mac Word 2011 build... I run the same code and it inserts the bookmark around the text below Section 1.1.

gmaxey
02-22-2017, 05:24 PM
You might try a simple VBA find and if the found text\range is formatted as "Heading 1" then add your bookmark.

Jason_S
02-22-2017, 05:28 PM
Actually, I did the following (for those that are following this thread). The following works for the solution requested above for Word for Mac 2011. I assume it also works on PC and Word for Mac 2016. For the record, I am in process of downloading Word for Mac 2016 and if greg's code above works in that version, I'll add another note here. Anyhow, here is the code that works:

========CODE START (2 subroutines)

Sub SecondTryAtSectionSelection(ClauseTitle As String, strBMName As String)


Dim para As Paragraph
Dim oRng As Word.Range



For Each para In ActiveDocument.Paragraphs
FirstSentence = para.Range.Sentences(1).Text
If InStr(FirstSentence, ClauseTitle) > 0 Then
para.Range.Select
Selection.Collapse wdCollapseStart
Selection.Extend character:="."
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set oRng = Selection.Range
ActiveDocument.Bookmarks.Add strBMName, oRng
End If
Next para


End Sub
Sub test_GotoHeading()

SecondTryAtSectionSelection "Management Resources", "MR"


End Sub
========CODE End

gmaxey
02-23-2017, 07:49 AM
Jason,

While that may work, it is horribly inefficient. What if you were trying to process the text of War and Peace. Can you imagine how long it would take. It is like trying to find a round peg in a barrel of square pegs by trying to push each one through a round hole.

Why don't you just find the roundish pegs and see if their roundness is true enough to fit the hole:


Sub test_GotoHeading()
ThirdInteration "For Temporary Employee", "TE"
lbl_Exit:
Exit Sub
End Sub

Sub ThirdInteration(strFind As String, strBMName As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = strFind & "."
'Is is found?
Do While .Execute
'Does it start the paragraph?
If InStr(oRng.Paragraphs(1).Range.Text, strFind) = 1 Then
'Is that paragraph outlined numbered?
If oRng.Paragraphs(1).Range.ListFormat.ListType = wdListOutlineNumbering Then
oRng.End = oRng.End - 1
ActiveDocument.Bookmarks.Add strBMName, oRng
Exit Do
End If
End If
Loop
End With
lbl_Exit:
Exit Sub
End Sub


P.S. why do you declare some variables but not others?