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 / January 2008

Tip: Looking for answers? Try searching our database.

Macro to make text/field codes superscripted in header/footers.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Renée - 03 Jan 2008 21:32 GMT
Hello,

I want to create a macro that will search the whole document including
headers and footers for specified text and then replace with superscripted
text. the field codes used are {SUBJECT  \*MERGEFORMAT} and {TITLE  
\*MERGEFORMAT}    

I found this reference but am not sure how to incorporate the superscript
part.

http://word.mvps.org/FAQs/Customization/ReplaceAnywhere.htm

Does anyone have any advice. I've only used recorded macros and this is my
first foray into the macro world. So any advice/guidance is appreciated.
Thanks.

Renée
Doug Robbins - Word MVP - 04 Jan 2008 08:58 GMT
Hi Renée,

The following will do what you want:

Dim afield As Field
Dim rngStory As Word.Range
Dim lngJunk As Long
With ActiveDocument
   'Fix the skipped blank Header/Footer problem as provided by Peter Hewett
   lngJunk = .Sections(1).Headers(1).Range.StoryType
   'Iterate through all story types in the current document
   For Each rngStory In .StoryRanges
   'Iterate through all linked stories
       Do
           For Each afield In rngStory.Fields
               If afield.Type = wdFieldTitle Or wdFieldSubject Then
                   afield.Code.Font.Superscript = True
                   afield.Update
               End If
           Next afield
           'Get next linked story (if any)
           Set rngStory = rngStory.NextStoryRange
       Loop Until rngStory Is Nothing
   Next
End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hello,
>
[quoted text clipped - 13 lines]
>
> Renée
Graham Mayor - 04 Jan 2008 10:54 GMT
This is not quite as simple a task, as you want to reformat fields that have
a mergeformat switch, which will conspire against you. To format all the
Subject and Title fields in your document you need a slightly different
approach. The following will change the mergeformat switch for the two field
types to a charformat switch, then apply superscript to the first characters
of the fields which in conjunction with the charformat switch will display
the whole fields as superscript. http://www.gmayor.com/installing_macro.htm

Public Sub ChangeField()
Dim rngStory As Word.Range
Dim oRng As Range
Dim iFld As Integer
Dim lngJunk As Long

lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each rngStory In ActiveDocument.StoryRanges
Do
Selection.HomeKey
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
For iFld = rngStory.Fields.Count To 1 Step -1
   With rngStory.Fields(iFld)
       If .Type = wdFieldTitle Or .Type = wdFieldSubject Then
           If InStr(1, .Code, "MERGEFORMAT") <> 0 Then
               .Code.Text = replace(.Code.Text, "MERGEFORMAT",
"CHARFORMAT")
           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
               .Font.Superscript = True
           End With
           .Update
       End If
   End With
Next iFld
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
With ActiveWindow.View
   .Type = wdPrintView
   .SeekView = wdSeekMainDocument
   .ShowFieldCodes = False
   .Type = wdPrintView
End With
End Sub

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hello,
>
[quoted text clipped - 13 lines]
>
> Renée
Renée - 04 Jan 2008 20:16 GMT
Hello again,

The header text did become superscripted so the macro worked in that sense
-- big thanks.But I want to replace for example, instance of CP with a
superscripted CP (treat it like a trademark) in the header. Where do I insert
the find and replace all code? Is it safe to assume that I would replace
starting at "With0Rng" and "End With". Would this code work as a replacement?

With Selection.Find
       .Text = "CP"
       .Replacement.Text = "CP"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
    With Selection
        With .Font
           .Superscript = True
   End With
   Selection.Find.Execute Replace:=wdReplaceAll
End With

Any help or guidance is greatly appreciated. If you know of any good
resources; I'd like to understand the code better.
Thanks

Renée

> This is not quite as simple a task, as you want to reformat fields that have
> a mergeformat switch, which will conspire against you. To format all the
[quoted text clipped - 64 lines]
> >
> > Renée
Graham Mayor - 05 Jan 2008 09:45 GMT
Something like

Public Sub ReplaceAnywhere()
Dim rngStory As Word.Range
Dim lngJunk As Long
With ActiveDocument
   lngJunk = .Sections(1).Headers(1).Range.StoryType
   For Each rngStory In .StoryRanges
       Do
         With rngStory.Find
           .ClearFormatting
           .Replacement.ClearFormatting
           .Text = "<CP>"
           .Replacement.Font.Superscript = True
           .Replacement.Text = "^&"
           .MatchWildcards = True
           .Execute replace:=wdReplaceAll
       End With
       Set rngStory = rngStory.NextStoryRange
       Loop Until rngStory Is Nothing
   Next
End With
End Sub

should do the trick

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hello again,
>
[quoted text clipped - 106 lines]
>>>
>>> Renée
 
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.