|
|
|
date: Fri, 15 Aug 2008 21:08:09 -0700 (PDT),
group: microsoft.public.word.vba.general
back
Re: Replacing Text w/Autotext - Greg, Ed, Graham,Russ
I am not sure what the question is now. ScratchMacroII that I posted
does perform the task of replacing [A], [B], [C] and [D] with
similiarly named AutoText entries.
On Aug 15, 4:03 pm, Irene wrote:
> "jerem" wrote:
> > Summer,
>
> > Thank you, thank you, thank you. Someone understand me. I've been trying
> > to explain in every possible way I could that I want text - What text? [A],
> > [B], [C], etc. that is dispersed throughout a document to be replaced by its
> > similarly named autotext entry - autotext entry - autotext entry, not
> > autotext code. (These similarly named autotext entries have been set up
> > prior to running the macro). The code I've been using (which appears below)
> > works beautifully and fast (about 2 seconds flat replacing all [A], [B], [C],
> > etc. with their similarly named autotext entry) - the only problem was I
> > didn't know how to get it to loop so that if there are 4 [A]'s in the
> > document it finds all 4 and replaces them with their similarly named autotext
> > entry, then do a similar find and replace for the [B]'s, [C]'s and so on and
> > so forth. So in order to combat that problem I've used a counter
>
> > Dim i As Integer
> > i = 4 (why because there are 4 [A]'s in this document that I need
> > replaced)
> > Do
> > i = i - 1
> > Selection.Find.ClearFormatting
> > With Selection.Find
> > .Text = "[A]"
> > .Replacement.Text = ""
> > .Forward = True
> > .Wrap = wdFindContinue
> > .Format = False
> > .MatchCase = True
> > .MatchWholeWord = False
> > .MatchWildcards = False
> > .MatchSoundsLike = False
> > .MatchAllWordForms = False
> > End With
> > Selection.Find.Execute
> > Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
> > "AUTOTEXT [AB] ", PreserveFormatting:=False
> > Loop Unitl i = 0
> > i = 6 (why because there are 6 [B]'s in this document that I need replaced)
> > Do
> > i = i - 1
> > Selection.Find.ClearFormatting
> > With Selection.Find
> > .Text = "[B]"
> > .Replacement.Text = ""
> > .Forward = True
> > .Wrap = wdFindContinue
> > .Format = False
> > .MatchCase = True
> > .MatchWholeWord = False
> > .MatchWildcards = False
> > .MatchSoundsLike = False
> > .MatchAllWordForms = False
> > End With
> > Selection.Find.Execute
> > Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
> > "AUTOTEXT [B] ", PreserveFormatting:=False (this sentence replaces the found
> > text with the autotext entry)
> > Loop Unitl i = 0
>
> > The problem (which was not a functional problem) was I didn't want to have
> > to tell it how many [A]'s, [B]'s to replace (what if someone decided to add
> > another [A] in the document later on down the line, then it would become a
> > functional problem because it would miss replacing that text). So my initial
> > question that was posed was can I get this to somehow do a Replace All for
> > each find or can someone give me a way to loop each set of finds. This then
> > spawned all kinds of suggestions (which I do appreciate and have tried to
> > implement), however, they don't seem to do anything. Do I know how to write
> > a macro - yes, have written many. Am I fluent in VBA - no. I let my macro
> > recorder do my code-writing. I scavenge this site and others and grab pieces
> > here and there.
>
> > So, I never had a problem with my macro working - I just wasn't happy with
> > having to designate how many of each bracketed text [A], [B], etc. there were
> > and I only did designate it because I didn't know how to get each Find and
> > Replace to loop until it was done with the [A]'s, [B]'s, [C]'s, etc.
>
> > If you see my previous inquiry (which was similarly named - Replace Found
> > Text wtih AutoText entries) I also went into detail why this was not being
> > handled by a merge (documents lock up - cannot breach the merge relationship
> > between the documents, which causes blacklining problems).
>
> > So, Greg, Graham, Ed, Russ - if you're out there and reading this:
>
> > I appreciate all of your help. I cannot get your code to work for my
> > problem and it very well may be that your code is good and my VBA savvy is
> > lacking. Again, I appreciate your effort in this.
>
> > Graham, here's the example you requested:
>
> > Autotext entries that I have already placed in AutoText [A] = Graham, [B] =
> > Greg, [C] = Ed, [D] = Russ, [E] = Summer, [F] = jerem
>
> > Dear [A], [B], [C], [D],
>
> > Thank you [A], [B], [C], and [D] for reading my problem and trying to solve
> > it. I know you have put a lot of thought into my problem. I'd like to thank
> > [A] for responding so quickly and [B] for being fundamental in his response
> > and [C] for being patient and [D] for his perserverance.
>
> > [B], I'm not familiar with arrays and so when that code didn't work, had no
> > idea why it didn't work. [C], I'm sure that code was brilliant and am sorry
> > that at this stage I cannot appreciate the genius of it. I will hold onto it
> > for the future so hopefully I can appreciate it when I do improve my VBA
> > language skills. [A], I detect your frustation in your last communication
> > and understand it. I've been trying to communicate EXACTLY what I needed and
> > thought I had in more than one of my communications.
>
> > Again, I appreciate all your help. Sorry to frustrate any or all of you.
> > [E], [A], [B], [C], [D], anyone have a loop statement for me?
>
> > Very truly yours,
>
> > [F]
>
> > "Summer" wrote:
>
> > > This code is fine - I have ATe code that does this:
>
> > > Mr [A] saw Mr [C] and went to [B].
>
> > > Run macro it does this:
>
> > > Mr AppleA saw Mr JonesC and went to LondonB.
>
> > > It is calling Autotext named [A] or [B] or [C] from Normal - you may need to
> > > specify location of ATe.
>
> > > Do you know how to run a macro?
>
> > > "Graham Mayor" wrote in message
> > >news:%23$KIqD56HHA.3716@TK2MSFTNGP03.phx.gbl...
> > > > Before we waste any more time on this, what *EXACTLY* are we looking for
> > > > in the Document
> > > > and what *EXACTLY* do you want to replace the found string with?
>
> > > > Can you reproduce a sample?
>
> > > > It appears that you have a block of text thus '[AB]' and you want to
> > > > replace it with an autotext field of the same name if
> > > > {Autotext "[AB"]} and then presumably update that field to show the
> > > > content of the autotext?
>
> > > > Is that the full extent, or are there a variety of texts to find and
> > > > replace? If so what are they?
>
> > > > --
> > > > <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> > > > Graham Mayor - Word MVP
>
> > > > My web sitewww.gmayor.com
> > > > Word MVP web sitehttp://word.mvps.org
> > > > <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
> > > > jerem wrote:
> > > >> Hi Greg, Ed, Russ, etc.
>
> > > >> Greg, tried the code you gave me:
> > > >> Sub ScrathMacroII()
> > > >> Dim myArray As Variant
> > > >> Dim i As Long
> > > >> Dim oRng As Word.Range
> > > >> myArray = Split("[A]|[B]", "|") '[C] etc.
> > > >> For i = 0 To UBound(myArray)
> > > >> Set oRng = ActiveDocument.Range
> > > >> With oRng.Find
> > > >> .Text = myArray(i)
> > > >> .Replacement.Text = ""
> > > >> .Forward = True
> > > >> .Wrap = wdFindStop
> > > >> While .Execute
> > > >> oRng.InsertAutoText
> > > >> oRng.Collapse wdCollapseEnd
> > > >> Wend
> > > >> End With
> > > >> Next i
> > > >> End Sub
>
> > > >> and it doesn't seem to do anything.
>
> > > >> Ed, similar problem with the code below:
> > > >> Sub Test()
> > > >> ReplaceStuff ("[A]")
> > > >> ReplaceStuff ("[B]")
> > > >> ...
> > > >> ...
> > > >> End Sub
>
> > > >> Sub ReplaceStuff(pText As String)
> > > >> Dim MyRange As Range
> > > >> Dim FindString As String
>
> > > >> FindString = Replace(Replace(pText, "[", "\["), "]", "\]")
>
> > > >> With Selection.Find
> > > >> Do While .Execute(FindText:=FindString, _
> > > >> MatchWildcards:=True, _
> > > >> Wrap:=wdFindContinue, _
> > > >> Forward:=True) = True
> > > >> Set MyRange = Selection.Range
> > > >> MyRange.InsertAutoText
> > > >> Loop
> > > >> End With
> > > >> End Sub
>
> > > >> So.......just one more question with the code below -- rather than
> > > >> using the Dim i As Integer
> > > >> i = 4
> > > >> Do
> > > >> i = i - 1
> > > >> and the Loop Until i = 0 to get my set of instructions to loop until
> > > >> all [A]'s appearing in the document are replaced with similarly named
> > > >> autotext entries (and the only reason I'm using these statements is
> > > >> because it's the only way I know how to get the darn thing to loop> > > >> I know all of you have been trying to help me by giving me other
> > > >> suggestions (but it's not accomplishing what I need to get done) but
> > > >> the code I've come up with does accomplish it. I just need something
> > > >> like
>
> > > >> Keep repeating the instructions below
> > > >> Selection.Find.ClearFormatting
> > > >> With Selection.Find
> > > >> .Text = "[AB]"
> > > >> .Replacement.Text = ""
> > > >> .Forward = True
> > > >> .Wrap = wdFindContinue
> > > >> .Format = False
> > > >> .MatchCase = True
> > > >> .MatchWholeWord = False
> > > >> .MatchWildcards = False
> > > >> .MatchSoundsLike = False
> > > >> .MatchAllWordForms = False
> > > >> End With
> > > >> Selection.Find.Execute
> > > >> Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
> > > >> Text:= _ "AUTOTEXT [AB] ", PreserveFormatting:=False
> > > >> Until there are no more to replace then go to the next set of
> > > >> instructions following this one
>
> > > >> I know there are Whiles and Wends but have tried to fool around with
> > > >> these but can't seem to get them to work. I just need someone to
> > > >> get these instructions to loop without use a counter. ?????????- Hide quoted text -
>
> - Show quoted text -
date: Fri, 15 Aug 2008 21:08:09 -0700 (PDT)
author: Greg Maxey
|
|