SOLN: How to convert occurrences of a string into a field.

  • Thread starter Frederik Romanov
  • Start date
F

Frederik Romanov

3 Part message
1 - Attn Jay Freedman
2 - Attn Peter Jamieson
3 - Solution !!!

Before I start, a question that arose. How can one assign /alter
/delete keyboard shortcuts for existing macros. I am forced to record
a new macro with the shortcut I want, paste the code from the existing
macro into this, delete the existing macro and rename the new macro -
there must be an easier way than this?

Fred.
------------------------------
Better F*** than D***


These replies reference
From: (e-mail address removed) (Frederik Romanov)
Newsgroups: microsoft.public.word.formatting.longdocs,microsoft.public.word.docmanagement,comp.os.ms-windows.apps.word-proc,microsoft.public.word.newusers,microsoft.public.word.conversions
Subject: How can one convert occurrences of a string into a field ?


1 - Attn Jay Freedman
---------------------------------------------------------------------

Thanks for the reply. I have tried the macro you suggested, after
tailoring it to perform another substitution (this version is below) :

Functional Checks -> FIELD{CHECKTYPE}

I created the field then entered and ran the macro, however it seems
to enter an infinite loop and I have to kill the WORD process in the
end without seeing if it has made any substitutions.

The first time I ran it, I stepped through all the lines of code, as
they all executed fine I hit F5 to continue. From this infinite loop
I could fairly quickly start the program manager to kill the word
process.

Then I tried it again by just running directly from the macros
pulldown list. Launching it this way I had to attempt several times
(over five minutes) to get a program manager app to launch so that I
could kill the process.

Is there something funny in the original macro, or has one of my
modifications turned it bad?

Final question - if I step through a VBA macro running in Excel, I can
see the effects taking place real time in the Excel window - this is
not the case with this macro in Word, is there a reason?

Fred

Sub MakeCheckField()
Dim curRange As Range
Set curRange = ActiveDocument.Range

With curRange.Find
.ClearFormatting
.Text = "Functional Checks"
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWholeWord = True
.MatchWildcards = False

Do While .Execute
ActiveDocument.Fields.Add _
Range:=curRange, _
Type:=wdFieldDocProperty, Text:="CHECKTYPE", _
preserveformatting:=True
curRange.Collapse wdCollapseEnd
Loop
End With
End Sub



2 - Attn Peter Jamieson
------------------------------------------------------------

Thanks too, I tried a few variants of your idea, however neither of
them allowed me to substitute normal text with a field code :

Sub ATP2Field()
'
' ATP2Field Macro
' Macro recorded 03-06-05 by Fred
'

' Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
' "DOCPROPERTY ""test-ATP"" ", PreserveFormatting:=True

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "functional ATP"
.Replacement.Text = "DOCPROPERTY ""test-ATP"" "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub


Sub ATP2Field_man()
'
' ATP2Field-man Macro
' Macro recorded 03-06-05 by Fred
'

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "functional ATP"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"DOCPROPERTY ""test-ATP"" ", PreserveFormatting:=True

End Sub


3 - Solution !!! ------------------------------------------------------------

But then after experimenting I found the following solution.

To find the next occurrence of the target text "LRU"

Ctrl ;

To replace this with the field <LRU>

Ctrl '

(This field should be defined in the normal way prior to starting).
Works like a beaut.

Notice that these shortcuts are purposefully close together to allow
head-up operation.


Normal.Dot
---------------------

Sub Text_Mark()
'
' Shortcut : ^;
' Text_Mark: Mark the next occurrence of "LRU"
' Macro recorded 03-06-09 by Fred
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "LRU"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub


Sub Text_Field()
'
' Shortcut : ^'
' Text_Field: Replace marked text with the field DOCPROPERTY "LRU"
' Macro recorded 03-06-09 by Fred
'
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"DOCPROPERTY ""LRU"" ", PreserveFormatting:=True

End Sub
 
J

Jay Freedman

Hi, Fred,

I can only reply about your first question and part 1, and I'll let Peter
respond to part 2.

To change a keyboard assignment for a macro, use the standard keyboard
customization method: Open the Tools | Customize dialog and click the
Keyboard button. First check the template listed in the "Save changes in"
box, and change that if necessary. Click "Macros" in the Categories list,
and click your macro's name in the Macros list. Select the existing key
assignment and click the Remove button; then enter the new assignment and
click the Assign button. Click the Close button in each dialog.

If a macro appears to go into an infinite loop, you can (usually) stop it by
pressing Ctrl+Break, rather than killing the entire Word program from Task
Manager. You should then be able to open the VBA editor and examine the
values of the variables by using the Watch window, and continue the code in
single-step mode to see why the macro is looping.

I made a simple document with a page of text containing three instances of
the text "Functional Checks", copied and pasted your macro exactly as it
appears below, and ran it with no problem -- it didn't go into an infinite
loop, regardless of whether I single-stepped, used F5, or ran from the
Macros dialog -- and it did insert the correct fields. There may be
something wrong with the document you're using for testing, but the macro
seems to be sound.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top