Home |
Search |
Today's Posts |
#1
![]() |
|||
|
|||
![]()
I'm relatively new to Word macros, and I'm having two problems relating to
changing text. The first is that I can't seem to get my macros to highlight all instances of specific text and change its format. I know how to record the action, and the action works as I'm recording the macro, but not when I run it. The second is that I can't get my macros to find all instances of a paragraph, cut it, and paste it elsewhere in the document. A simple example of what I'm trying to do: I need to go from: Line A Line B Line C Line A Line B Line C To: Line B Line C Line A Line B Line C Line A I know how to specify the movement of paragraphs positioned at specific lines, but this won't work well for this particular project -- I'm having to make the same movements on a variable number of blocks of text in the same document. Thanks very much for your help! |
#2
![]() |
|||
|
|||
![]()
The macro recorder is limited in its abilities and won't record formatting
information as part of the replace function. You'll have to add it manually to the macro. What formatting do you want to replace? The couple of examples below should help you sort it out: Sub ReplaceExample() Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find '********************** .Text = "" .Font.Name = "Arial" .Font.Bold = True .Font.Size = "12" .Replacement.Text = "" .Replacement.Font.Name = "Bookman Old Style" .Replacement.Font.Size = "11" .Replacement.Font.Italic = True '********************** .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False End With Selection.Find.Execute replace:=wdReplaceAll End Sub Sub ReplaceExample2() Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find '********************** .Style = "Body Text" .Replacement.Style = "FR1" '********************** .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False End With Selection.Find.Execute replace:=wdReplaceAll End Sub The second part is rather more complicated unless all the lines you want to move are easily identifiable or you want to swap every three lines around - See http://www.gmayor.com/replace_using_wildcards.htm -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org Nexan wrote: I'm relatively new to Word macros, and I'm having two problems relating to changing text. The first is that I can't seem to get my macros to highlight all instances of specific text and change its format. I know how to record the action, and the action works as I'm recording the macro, but not when I run it. The second is that I can't get my macros to find all instances of a paragraph, cut it, and paste it elsewhere in the document. A simple example of what I'm trying to do: I need to go from: Line A Line B Line C Line A Line B Line C To: Line B Line C Line A Line B Line C Line A I know how to specify the movement of paragraphs positioned at specific lines, but this won't work well for this particular project -- I'm having to make the same movements on a variable number of blocks of text in the same document. Thanks very much for your help! |
#3
![]() |
|||
|
|||
![]()
Thanks, Graham! As far as the formatting goes, all I really need to do for
now is to select one specific paragraph and change it to bold. "Graham Mayor" wrote: The macro recorder is limited in its abilities and won't record formatting information as part of the replace function. You'll have to add it manually to the macro. What formatting do you want to replace? The couple of examples below should help you sort it out: Sub ReplaceExample() Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find '********************** .Text = "" .Font.Name = "Arial" .Font.Bold = True .Font.Size = "12" .Replacement.Text = "" .Replacement.Font.Name = "Bookman Old Style" .Replacement.Font.Size = "11" .Replacement.Font.Italic = True '********************** .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False End With Selection.Find.Execute replace:=wdReplaceAll End Sub Sub ReplaceExample2() Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find '********************** .Style = "Body Text" .Replacement.Style = "FR1" '********************** .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False End With Selection.Find.Execute replace:=wdReplaceAll End Sub The second part is rather more complicated unless all the lines you want to move are easily identifiable or you want to swap every three lines around - See http://www.gmayor.com/replace_using_wildcards.htm -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org Nexan wrote: I'm relatively new to Word macros, and I'm having two problems relating to changing text. The first is that I can't seem to get my macros to highlight all instances of specific text and change its format. I know how to record the action, and the action works as I'm recording the macro, but not when I run it. The second is that I can't get my macros to find all instances of a paragraph, cut it, and paste it elsewhere in the document. A simple example of what I'm trying to do: I need to go from: Line A Line B Line C Line A Line B Line C To: Line B Line C Line A Line B Line C Line A I know how to specify the movement of paragraphs positioned at specific lines, but this won't work well for this particular project -- I'm having to make the same movements on a variable number of blocks of text in the same document. Thanks very much for your help! |
#4
![]() |
|||
|
|||
![]()
"Graham Mayor" wrote:
The second part is rather more complicated unless all the lines you want to move are easily identifiable or you want to swap every three lines around - See http://www.gmayor.com/replace_using_wildcards.htm To further clarify what I'm needing to do, the letters in my example will be wildcards, for the most part. I know how to get the macro to find the wildcards but not how to cut and past them. The Help menu seems to suggest using the "Highlight All" option under Find for such things; however, after I've cut the appropriate text, I can't get the macro to paste it where it needs to go -- the error claims that no text has been selected. |
#5
![]() |
|||
|
|||
![]()
Nexan wrote:
"Graham Mayor" wrote: The second part is rather more complicated unless all the lines you want to move are easily identifiable or you want to swap every three lines around - See http://www.gmayor.com/replace_using_wildcards.htm To further clarify what I'm needing to do, the letters in my example will be wildcards, for the most part. I know how to get the macro to find the wildcards but not how to cut and past them. The Help menu seems to suggest using the "Highlight All" option under Find for such things; however, after I've cut the appropriate text, I can't get the macro to paste it where it needs to go -- the error claims that no text has been selected. If you can identify unique strings for the replace function to select, you can use round brackets to collect the relative parts of the string and replace the strings in a different sequence. You don't need to use cut and paste. -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org |
#6
![]() |
|||
|
|||
![]()
If I give you a specific example, could you show me what you mean? Let's say
I'm trying to change the following, with the text after the colons being variables: Date: 3/1/2005 Name: Joe Blow Date: 3/2/2005 Name: Jane Doe To: Name: Joe Blow Date: 3/1/2005 Name: Jane Doe Date: 3/2/2005 Thanks, Graham! I really appreciate all your help! "Graham Mayor" wrote: Nexan wrote: "Graham Mayor" wrote: The second part is rather more complicated unless all the lines you want to move are easily identifiable or you want to swap every three lines around - See http://www.gmayor.com/replace_using_wildcards.htm To further clarify what I'm needing to do, the letters in my example will be wildcards, for the most part. I know how to get the macro to find the wildcards but not how to cut and past them. The Help menu seems to suggest using the "Highlight All" option under Find for such things; however, after I've cut the appropriate text, I can't get the macro to paste it where it needs to go -- the error claims that no text has been selected. If you can identify unique strings for the replace function to select, you can use round brackets to collect the relative parts of the string and replace the strings in a different sequence. You don't need to use cut and paste. -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org |
#7
![]() |
|||
|
|||
![]()
At it's most basic, wildcard search for
(Date*^13)(Name*^13) replace with \2\1 Search for (Date*[0-9]{4}^13)(Name*^13) to tie it down a bit tighter. -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org Nexan wrote: If I give you a specific example, could you show me what you mean? Let's say I'm trying to change the following, with the text after the colons being variables: Date: 3/1/2005 Name: Joe Blow Date: 3/2/2005 Name: Jane Doe To: Name: Joe Blow Date: 3/1/2005 Name: Jane Doe Date: 3/2/2005 Thanks, Graham! I really appreciate all your help! "Graham Mayor" wrote: Nexan wrote: "Graham Mayor" wrote: The second part is rather more complicated unless all the lines you want to move are easily identifiable or you want to swap every three lines around - See http://www.gmayor.com/replace_using_wildcards.htm To further clarify what I'm needing to do, the letters in my example will be wildcards, for the most part. I know how to get the macro to find the wildcards but not how to cut and past them. The Help menu seems to suggest using the "Highlight All" option under Find for such things; however, after I've cut the appropriate text, I can't get the macro to paste it where it needs to go -- the error claims that no text has been selected. If you can identify unique strings for the replace function to select, you can use round brackets to collect the relative parts of the string and replace the strings in a different sequence. You don't need to use cut and paste. -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org |