Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2007

Tip: Looking for answers? Try searching our database.

Using VBA to work with a field including the curly brackets, not just the code or result

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Beamish - 23 Mar 2007 00:42 GMT
I am creating Word documents with lots of clickable hyperlinks (mostly
internal cross-references, table of contents, etc.) and I want to colour
the hyperlinks so that they are easily visible on-screen. I tried a
macro on these lines:

   Dim oField As Field
   For Each oField In ActiveDocument.Fields
      If oField.Type = wdFieldHyperlink Or oField.Type = wdFieldRef _
         Or oField.Type = wdFieldPageRef Then
         oField.Result.Font.Color = wdColorBlue
      End If
   Next oField

The problem is that this affects only the part of the field within the
curly brackets, and when the field is updated the formatting disappears.
I can get round it like this:

   Dim oField As Field
   For Each oField In ActiveDocument.Fields
      If oField.Type = wdFieldHyperlink Or oField.Type = wdFieldRef _
         Or oField.Type = wdFieldPageRef Then
         oField.Select
         Selection.Font.Color = wdColorBlue
      End If
   Next oField

But this is much slower and seems rather inelegant. Does anyone know any
way in VBA of operating on a range which includes the whole of a field,
curly brackets and all, other than selecting it?
Stefan Blom - 23 Mar 2007 11:44 GMT
For the hyperlinks you could just apply the Hyperlink character style
(modified to suit your needs):

For Each f In ActiveDocument.Fields
  If f.Type = wdFieldHyperlink Then
     f.Result.Style = wdStyleHyperlink
  End If
Next f

For the REF and PAGEREF fields, format the field code and add the \*
CHARFORMAT switch, which ensures that the formatting is preserved when
updating:

   Dim oField As Field
   For Each oField In ActiveDocument.Fields
      If oField.Type = oField.Type = wdFieldRef _
            Or oField.Type = wdFieldPageRef Then
         oField.Code.Font.Color = wdColorBlue
         oField.Code.Text = oField.Code.Text & _
            " \* CHARFORMAT"
         oField.update
      End If
   Next oField

Note that the above code does not test for the presence of the \*
CHARFORMAT switch in the field code, which means that it will be added
several times if the macro is run several times. Adding an IF
statement which uses Instr to test for "CHARFORMAT" would take care of
that.

Signature

Stefan Blom
Microsoft Word MVP

> I am creating Word documents with lots of clickable hyperlinks (mostly
> internal cross-references, table of contents, etc.) and I want to colour
[quoted text clipped - 25 lines]
> way in VBA of operating on a range which includes the whole of a field,
> curly brackets and all, other than selecting it?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.