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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Greg,

Thank you so much. I know you are absolutely right – I have been working
with some similar problems recently. In Steve’s situation, there is a great
chance that none of the Time fields are in shapes but since your macro will
catch any such fields, it may be safer to use that one.

By the way, you have published a lot of helpful information and macro
solutions on your Website.


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


"Greg Maxey" wrote:

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: replace field in primary header using find and replace in a macro
    ... Word MVP web site http://word.mvps.org ... Note that if field codes are shown when you start the macro, ... Dim oField As Field ... Dim oFld As Word.Field ...
    (microsoft.public.word.vba.general)
  • Re: fax from word or outlook
    ... If you want to merge to a faxmodem and your merge is fairly simple you could try the following macro. ... I was unable to make this macro work on client computers connected to the Shared Fax Service that is provided as part of Microsoft's Small Business Server 2003. ... Dim bFaxServerAvailable As Boolean ... ' DisplayName is a "user-friendly" name used ...
    (microsoft.public.word.mailmerge.fields)
  • Re: How to Merge data from an ascii file into a Word Doc
    ... position in turn and run the macro InsertLineVariable. ... Dim oVars As Variables ... Dim vVar As Variant ... Dim iCount As Integer ...
    (microsoft.public.word.docmanagement)
  • Re: How can my macro run faster ?
    ... I'd keep all those "Sub/End Sub" statements. ... ' SortDossierOrder Macro ... Dim wks As Worksheet ... Dim cLastRow As Long ...
    (microsoft.public.excel.newusers)
  • Re: How can my macro run faster ?
    ... below still have the Sub name and End Sub included. ... ' SortDossierOrder Macro ... Dim wks As Worksheet ... Dim cLastRow As Long ...
    (microsoft.public.excel.newusers)