Re: replace field in primary header using find and replace in a macro
- From: Greg Maxey <gmaxey@xxxxxxxxx>
- Date: Tue, 5 Aug 2008 15:04:35 -0700 (PDT)
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.
.
- Follow-Ups:
- Re: replace field in primary header using find and replace in a macro
- From: Greg Maxey
- Brilliant
- From: SteveB
- Re: replace field in primary header using find and replace in a ma
- From: Lene Fredborg
- Re: replace field in primary header using find and replace in a macro
- References:
- replace field in primary header using find and replace in a macro
- From: SteveB
- RE: replace field in primary header using find and replace in a macro
- From: Lene Fredborg
- replace field in primary header using find and replace in a macro
- Prev by Date: Re: IsTime Function?
- Next by Date: Remember variable when it is called from another sub
- Previous by thread: RE: replace field in primary header using find and replace in a macro
- Next by thread: Re: replace field in primary header using find and replace in a ma
- Index(es):
Relevant Pages
|