MS Office Forum / Word / Long Documents / May 2005
How can I retrieve the format of a header reference?
|
|
Thread rating:  |
Max Moor - 16 May 2005 02:52 GMT Hi All, I'm learning, little by little. I know that I can reference the text that a cross reference field shows in the doc with:
strCode1 = oRg.Fields(1).Result.Text
Now, I'd like to be able to return the style of the header that the cross-reference field refers to. For example, I search and find a cross reference to a header. The code line above returns "Rubber Chickens."
In the document, Rubber Chickens' style is 'Heading 2'. Can I, in VB code, get "Rubber Chickens" by way of the cross-reference field? I tried:
strCode2 = oRg.Fields(1).Result.Style
Of course, that gave me the style of the cross-reference field, not the heading it refers to (as I wish).
Thanks for the help, Max
Jezebel - 16 May 2005 03:41 GMT When you insert a cross-reference to a heading, Word creates a hidden bookmark for that heading. This is the _Ref12345678 part of your REF field code. (The leading underscore is what makes it a hidden bookmark. You can see these on the Bookmarks dialog by checking the 'Hidden Bookmarks' checkbox.)
You can use this _Ref code to retrieve the heading itself, and thus its style and other properties --
Dim pRef as string
pRef = mid$(oRg.Fields(1).Code, 6, 13) ActiveDocument.Bookmarks.ShowHidden = TRUE pStyle = ActiveDocument.Bookmarks(pRef).Range.Style
The Mid$() finction is dubious here -- it would be more reliable to use Split() or Instr() to extract the reference.
> Hi All, > I'm learning, little by little. I know that I can reference the text [quoted text clipped - 15 lines] > > Thanks for the help, Max Max Moor - 16 May 2005 04:16 GMT > When you insert a cross-reference to a heading, Word creates a hidden > bookmark for that heading. This is the _Ref12345678 part of your REF [quoted text clipped - 13 lines] > The Mid$() finction is dubious here -- it would be more reliable to > use Split() or Instr() to extract the reference. Split(oRg.Fields(1).Code)(2) it is. Once again, thanks for the help. I know that once this code is working, it will repeatedly save me hours of time over the years. As I found with Access, you MVPs make the learning curve so much more bearable.
- Max
Greg - 16 May 2005 13:15 GMT Max
I have been following your posts to try to pick up tips. I just started dabbling in Word VBA are year or so ago and still have lots to learn. How did you ever figure out to use the "(2)'" at the end of the Split statement to return the value that you wanted? I searched the VBA help and found no similiar example. Is this something that you knew from your experience in other programs?
Max Moor - 16 May 2005 17:03 GMT "Greg" <gmaxey@mvps.org> wrote in news:1116245707.750799.283430 @g47g2000cwa.googlegroups.com:
> Max > [quoted text clipped - 4 lines] > VBA help and found no similiar example. Is this something that you > knew from your experience in other programs? Hi Greg, Yes, I knew that from my Access trials. Split returns a string array, and the (2) is just an index to it... a shortcut, since I only care about the '_Refxxx' anyway. More often than not, I'd declare a variable to dump the array into:
Dim avarOpenArgs As Variant
If (Not IsNull(Me.OpenArgs)) Then
avarOpenArgs = Split(Me.OpenArgs, "\")
...
-TTFN
Greg - 16 May 2005 17:27 GMT Max,
Thanks. When I saw it, I figured immediately that it was an index value. I just couldn't figure out how you came upon using it. I have been using this new gained knowlegde to play around with some code for rearranging a list of names:
Sub Test() Dim oPara As Word.Paragraph Dim oRng As Word.Range Dim pRef0 As String Dim pRef1 As String Dim pRef2 As String Dim pRef3 As String Dim pRef4 As String Dim i As Long
For Each oPara In ActiveDocument.Paragraphs Set oRng = oPara.Range oRng.MoveEnd wdCharacter, -1 i = UBound(Split(oRng)) + 1 Select Case i Case Is > 1 pRef1 = Split(oRng)(0) pRef2 = Split(oRng)(1) Select Case i Case Is < 3 oRng.Text = pRef2 & ", " & pRef1 Case Is < 4 pRef3 = Split(oRng)(2) If InStr("SR.Sr.JR.Jr.IIIVIII", pRef3) > 0 Then oRng.Text = pRef2 & ", " & pRef1 & ", " & pRef3 Else oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2 End If Case Is = 4 pRef3 = Split(oRng)(2) pRef4 = Split(oRng)(3) If InStr("SR.Sr.JR.Jr.IIIVIII", pRef4) > 0 Then oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2 _ & ", " & pRef4 Else oRng.Text = pRef4 & ", " & pRef1 & " " & pRef2 _ & " " & pRef3 End If Case Else MsgBox oRng & " contains too many elements" _ & " for this procedure to rearrange." End Select Case Else End Select Next End Sub
I should change the variable names, but it seems to work pretty good for rearranging names into from First Name, MI, Last Name, Suffix to Last Name, First Name MI, Suffix
Greg - 16 May 2005 13:46 GMT Jezebel,
Please enlighten. How would you use Instr to return the reference?
Max Moor - 16 May 2005 17:17 GMT "Greg" <gmaxey@mvps.org> wrote in news:1116247564.625166.297570 @g43g2000cwa.googlegroups.com:
> Jezebel, > > Please enlighten. How would you use Instr to return the reference? I'll have a go (someone else will make a prettier one)
strRef = Mid$(oRg.Fields(1).Code, InStr(oRg.Fields(1).Code, "_Ref"), 13)
The only thing I don't like is the hard coded '13'. Does anyone know a pre-defined system constant for a hidden bookmark length? For that matter, is 13 even guaranteed?
-Max
Greg - 16 May 2005 18:47 GMT Max,
If Mid$ is buggy in the method Jezebel provided, it seems it would be even buggier here :-)
Jezebel - 17 May 2005 11:14 GMT Mid$() is buggy in the code I provided because the start and end points were assumed. (I was just using a shortcut for the sake of example.
Using Instr() establishes the starting point reliably. As Max points out the 13 is also dubious -- the hidden bookmarks I've seen always seem to be that length, but since the number is obviously random, I wouldn't rely on it. Better would be to do a second Instr() to find the first space that follows the _Ref.
But Split() is a simpler approach.
> Max, > > If Mid$ is buggy in the method Jezebel provided, it seems it would be > even buggier here :-) Greg - 17 May 2005 13:22 GMT Jezebel/Max,
Thanks for the polite schooling ;-)
The split() with index statement gave me plenty to play with during an otherwize slow day at the office :-)
|
|
|