Re: replace field in primary header using find and replace in a macro



On Aug 5, 2:14 pm, Lene Fredborg <l...@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
I am not sure what "fails" means here - does the macro display an error or
does it not change any fields?

Note that if field codes are shown when you start the macro, none of the
fields will be changed due to the code line:

    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes

When field codes are shown, the line toggles the field codes off and TIME is
not found.

Try to replace that line with:

    ActiveWindow.View.ShowFieldCodes = True

and the corresponding line at the end of the macro with:

    ActiveWindow.View.ShowFieldCodes = False

Does this solve the problem?

Note: You macro will replace any occurrence of the letters “time” with
“createtime” – this may result in undesired replacements in the document. You
may want to use something like the following instead – it iterates though all
stories in the document and checks the fields. If a TIME field is found, it
is changed to a CREATEDATE field:

Sub ReplaceTimeFieldByCreateDateField()

    Dim oField As Field
    Dim oStory As Range

    For Each oStory In ActiveDocument.StoryRanges
        If Not oStory Is Nothing Then
            For Each oField In oStory.Fields
                If oField.Type = wdFieldTime Then
                    oField.Code.Text = _
                        Replace(oField.Code.Text, "TIME", "CREATEDATE")
                End If
            Next oField
        End If
    Next oStory

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word



"SteveB" wrote:
OK this is my situation

I can record a macro and as I am recording it the process works fine.
I replace the field function Time with a Createdate one and it does this in
a Primary header's fields aswell as the main document area.

all the fields in question are inside primary headers

upon re-running the macro it fails.

regardless of where I place the cursor prior to running the macro.

please help me - I am in danger of  becoming insane over this !

This is the macro

Sub Replacer()
'
' Replacer Macro
' Macro recorded 05/08/2008 by Simun
'
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "TIME"
        .Replacement.Text = "CREATEDATE"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
End Sub

Thanks in advance for any help !

Steve- Hide quoted text -

- Show quoted text -

Lene,

I am sure that your code would be perfectly suitable 99.99% of the
time, but it will miss TIME fields that are contained in the TextRange
of shapes located in headers or footers. This is something that I
discovered when I was working on my VBA Find and Replace AddIn that is
posted on my website. To get them all you would need to process each
shape in the shaperange using something like this:

Sub ScratchMacro()
Dim rngstory As Word.Range
Dim oFld As Word.Field
Dim oShp As Word.Shape
Dim i As Long
'Handle unlinked sections
i = ActiveDocument.Sections(1).Headers(1).Range.StoryType
With ActiveDocument
For Each rngstory In .StoryRanges
'Iterate through all linked stories
Do
For Each oFld In rngstory.Fields
With oFld
If .Type = wdFieldTime Then
.Code.Text = Replace(oFld.Code.Text, "TIME",
"CREATEDATE")
End If
.Update
End With
Next oFld
On Error Resume Next
Select Case rngstory.StoryType
Case 6, 7, 8, 9, 10, 11
'Interate through all shapes in header/footers
If rngstory.ShapeRange.Count > 0 Then
For Each oShp In rngstory.ShapeRange
If oShp.TextFrame.HasText Then
For Each oFld In oShp.TextFrame.TextRange.Fields
With oFld
If oFld.Type = wdFieldTime Then
oFld.Code.Text = _
Replace(oFld.Code.Text, "TIME", "CREATEDATE")
End If
.Update
End With
Next oFld
End If
Next oShp
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngstory = rngstory.NextStoryRange
Loop Until rngstory Is Nothing
Next rngstory
End With
End Sub

Thought you would like to know.
.



Relevant Pages

  • Re: changing text color
    ... ;   undefined variable: princ ... You need to document parameters like that better, perhaps by naming it ... FMT tell _nothing_. ... FMT suggests your macro takes FORMAT-style formatting ...
    (comp.lang.lisp)
  • Re: [PATCH 07/10] tracing: add raw trace point recording infrastructure
    ... The current event tracer can automatically pick up trace points ... that are registered with the TRACE_FORMAT macro. ... but just want to use the convenience of printf. ...   TRACE_EVENT_FORMAT(name, ...
    (Linux-Kernel)
  • Re: a little problem with .Range(.Cells(2,1),.Cells(2,49)).
    ... the make macro possibility is very smart. ... syntax we used in VBS files written in Notepad, ... all the cells already have a border. ...   With oExcelSheet ...
    (microsoft.public.scripting.vbscript)
  • Re: Reduce Size of Blank Rows (again)
    ... previous macros, when I run the macro you provided, the cells flicker / blink ...   Dim LastRow As Long ...
    (microsoft.public.excel.newusers)