MS Office Forum / Word / Programming / September 2006
Formatting PageRef fields
|
|
Thread rating:  |
k8er - 26 Sep 2006 18:19 GMT Hi all,
I generate Word documents from AuthorIT (CM software), and release them in PDF format. I need an automated solution for formatting PageRef fields.
Currently, the PageRef fields come through in the regular body text format. I want to change them to use a specific character style (in this case, the style is called URL, which is blue like a hyperlink). I have tried using a macro to this, and it seems to work in Normal view. But when I change to Print Preview, or convert to PDF, the PageRef fields revert back to plain text.
Is there a way to permanently change the PageRef field to use a specific character style?
I'm using the following macro:
Dim myField As Field For Each myField In ActiveDocument.Fields If myField.Type =3D wdFieldHyperlink Or myField.Type =3D = wdFieldPageRef Then myField.Result.Style =3D ActiveDocument.Styles("URL") End If Next myField
What am I doing wrong?
Best regards, Kate
Dave Lett - 26 Sep 2006 20:59 GMT Hi Kate,
You are probably applying the character format, but you probably haven't made the field code so that formatting is "sticky". That is, you have to ensure that the field code includes the PreserveFormatting switch. The following might solve your issue:
Dim iFld As Integer For iFld = ActiveDocument.Fields.Count To 1 Step -1 With ActiveDocument.Fields(iFld) If .Type = wdFieldPageRef Then If InStr(1, .Code, "MERGEFORMAT") = 0 Then .Code.Text = .Code.Text & " \* MERGEFORMAT" End If .Result.Style = "Legacy" End If End With Next iFld
HTH, Dave
> Hi all, > [quoted text clipped - 26 lines] > Best regards, > Kate Dave Lett - 26 Sep 2006 21:15 GMT On a side note, how do you like AuthorIT?
> Hi Kate, > [quoted text clipped - 51 lines] >> Best regards, >> Kate k8er - 27 Sep 2006 02:40 GMT Hi Dave,
Thank you for the suggestion. To answer the side note first, AuthorIT is great. Being able to keep all of our content in a database where we can edit and reuse it is awesome. But the implementation has been a lot more complicated than I expected. Biggest problem: Understanding how all the different systems, styles, objects, and templates work together. Setting up the AuthorIT objects and styles and getting the Word templates just right -- and trying to match our existing documentation styles and output -- has been a big challenge. Especially since I'm not a Word Macro Guru. I'll be happy to tell you more about AuthorIT if you're interested.
As for the macro, I need some remedial help -- it's not quite working yet.
I removed the macro I was using and added yours. The Debugger didn't like .Result.Style = "Legacy" (it gave Run-time error 5834) so I replaced "Legacy" with the name of the style I want to use "URL". That seemed to run ok. But when I converted to PDF, all the page references came up with "Error unknown switch argument." Do you know what might be causing that?
Thanks again for your reply. -Kate
> On a side note, how do you like AuthorIT? > [quoted text clipped - 53 lines] > >> Best regards, > >> Kate Graham Mayor - 27 Sep 2006 07:56 GMT I suspect that it would be better to remove the MERGEFORMAT switch and adda CHARFORMAT switch instead which will allow the field to take on formatting applied to the field. The following modification should do that.
Dim iFld As Integer For iFld = ActiveDocument.Fields.Count To 1 Step -1 With ActiveDocument.Fields(iFld) If .Type = wdFieldPageRef Then If InStr(1, .Code, "MERGEFORMAT") <> 0 Then .Code.Text = Left(.Code.Text, Len(.Code.Text) - 16) & _ " \* CHARFORMAT " End If If InStr(1, .Code, "CHARFORMAT") = 0 Then .Code.Text = .Code.Text & " \* CHARFORMAT " End If .Result.Style = "URL" End If End With Next iFld
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi Kate, > [quoted text clipped - 48 lines] >> Best regards, >> Kate Dave Lett - 27 Sep 2006 14:17 GMT Hi Graham,
I have found two issues with using CHARFORMAT and was wondering if you could shed some light on them.
1) It appears that you cannot use CHARFORMAT when inserting a PAGEREF field through the Insert Cross References dialog box. That is, CHARFORMAT appears to only be compatible with a PAGEREF field that points to a bookmark. When I update the PAGEREF field that is a crosss reference, Word displays Error! Unknown switch argument. as the result.
2) The CHARFORMAT switch takes the formatting of the first letter of the field that it appears in, which means that if you format the result range and update the field, then the field will lose the formatting that you applied. To make the format stick, you have to apply the formatting to the "P" in the PAGEREF field (in this case) to make the formatting stick.
Finally, I'm getting some strange results after converting to PDF:
PAGEREF _Ref147109471 \h \* CHARFORMAT
Error! Unknown switch argument.
PAGEREF _Ref147109471 \h \* MERGEFORMAT
[NO RESULT!! It's there in Word, but disappears upon conversion]
PAGEREF _Ref147109471 \h
1 [Formatted properly, formatting applied to result]
PAGEREF _Ref147109471 \h MERGEFORMAT
1 [Formatted properly, formatting applied to result, but notice that "\*" is not included]
PAGEREF myHeading \* CHARFORMAT
1 [Formatted properly, formatting applied to "P" of PAGEREF]
PAGEREF myHeading \* MERGEFORMAT
1 [Formatted properly, formatting applied to result]
Dave
>I suspect that it would be better to remove the MERGEFORMAT switch and adda >CHARFORMAT switch instead which will allow the field to take on formatting [quoted text clipped - 68 lines] >>> Best regards, >>> Kate Graham Mayor - 27 Sep 2006 15:16 GMT 1. Hmmm. I tested this a few times using cross references to Figures and occasionally got the error - but more often than not I didn't. When inserting pageref fields, either manually or via the Cross References dialog, I did not get the error, and the macro code I modified has worked every time - I just tested it again to make sure. Then forced an update to make sure there was no error. In fact I stole your code for my own use as it was more efficient than the one I was using to change mergeformat to charformat;)
Do you still get the error if you add quotes around the referenced item?
2.If you format the entire range then the charformat switch should cause that range formatting to be adopted (which I thought was the requirement). the mergeformat field, in theory at least, should cause it to retain the formatting of the bookmark being referred to.
3. I did not get that error when I converted to PDF (Acrobat 7 - patched to date and using the add-in tools to create the pdf) but if you are getting an error when you update the field, that would account for it as the fields will update as part of the 'print' process. Unfortunately I cannot produce the error message consistently enough to bottom the problem, :(
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi Graham, > [quoted text clipped - 121 lines] >>>> Best regards, >>>> Kate k8er - 27 Sep 2006 15:59 GMT Hi guys,
I tried Graham's suggestion, and I didn't get any errors -- it seemed to run fine. But also the character format wasn't sticky. As soon as I change to Print View, or convert to PDF, the PageRef fields revert back to the plain text style. Is there something else that needs to be added to make the character format sticky?
Thanks, Kate
> 1. Hmmm. I tested this a few times using cross references to Figures and > occasionally got the error - but more often than not I didn't. When [quoted text clipped - 143 lines] > >>>> Best regards, > >>>> Kate Dave Lett - 27 Sep 2006 19:59 GMT Hi all,
Using Graham's suggestion, I've revised the material a little (I used Replace in one of the lines) and applied the formatting to the first character of the PageRef field. Seems to be working smashingly on my machine (hope it works on yours, too, Kate).
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True Dim iFld As Integer For iFld = ActiveDocument.Fields.Count To 1 Step -1 With ActiveDocument.Fields(iFld) If .Type = wdFieldPageRef Then If InStr(1, .Code, "MERGEFORMAT") <> 0 Then .Code.Text = Replace(.Code.Text, "MERGE", "CHAR") End If If InStr(1, .Code, "CHARFORMAT") = 0 Then .Code.Text = .Code.Text & " \* CHARFORMAT " End If .Code.Select Set oRng = Selection.Range With oRng .Start = oRng.Characters(1).Start .End = oRng.Characters(2).End .Style = "URL" End With .Update End If End With Next iFld ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
HTH, Dave
> Hi guys, > [quoted text clipped - 161 lines] >> >>>> Best regards, >> >>>> Kate k8er - 27 Sep 2006 20:42 GMT Dave,
Need a little more help here -- I tried this code, but it gave me "compile error -- variable not found" when it got to oRng. Do you know why I'd get that error?
-Kate
> Hi all, > [quoted text clipped - 194 lines] > >> >>>> Best regards, > >> >>>> Kate Dave Lett - 27 Sep 2006 21:25 GMT Maybe. If you have Option Explicit set, then you need to declare that variable.
At the very begining of the routine, insert
Dim oRng as Range
HTH, Dave
> Dave, > [quoted text clipped - 220 lines] >> >> >>>> Best regards, >> >> >>>> Kate Graham Mayor - 28 Sep 2006 06:47 GMT It works for me ;)
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi all, > [quoted text clipped - 201 lines] >>>>>>> Best regards, >>>>>>> Kate
|
|
|