Hi
Is it possible to replace a styled paragraph with prefix and postfix string and set the paragraph style to normal under VBA?
Better explained thrr the screenshot.
Thanks
Robie
replace styled word with prefix and postfix strings
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
replace styled word with prefix and postfix strings
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78608
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: replace styled word with prefix and postfix strings
Press Ctrl+Home to move to the start of the document.
Press Ctrl+H to activate the Replace dialog.
Click More >>.
Click in the Find what box.
Leave it empty, but click Format > Style... and select the Code style.
Click in the Replace with box.
Enter the following in it: [code]^p^&[/code]^p
Click Format > Style... and select the Normal style.
Click Replace All.
Press Ctrl+H to activate the Replace dialog.
Click More >>.
Click in the Find what box.
Leave it empty, but click Format > Style... and select the Code style.
Click in the Replace with box.
Enter the following in it: [code]^p^&[/code]^p
Click Format > Style... and select the Normal style.
Click Replace All.
Best wishes,
Hans
Hans
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: replace styled word with prefix and postfix strings
You might need an extra paragraph mark in there?
^p[/code]
Code: Select all
[code]^p^&^p
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- Administrator
- Posts: 78608
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: replace styled word with prefix and postfix strings
No, why? The original Code styled paragraph already has a paragraph mark ¶ at the end.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: replace styled word with prefix and postfix strings
Thanks Hans. That is perfect.HansV wrote:Press Ctrl+Home to move to the start of the document.
Press Ctrl+H to activate the Replace dialog.
Click More >>.
Click in the Find what box.
Leave it empty, but click Format > Style... and select the Code style.
Click in the Replace with box.
Enter the following in it: [code]^p^&[/code]^p
Click Format > Style... and select the Normal style.
Click Replace All.
But, what does ^& signify? I presume it means 'the selection' (i.e. the text/paragraphs found with the Find style) or does it mean something else entirely?
-
- Administrator
- Posts: 78608
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: replace styled word with prefix and postfix strings
Yep, ^& is the code for 'Find what text'. See http://support.microsoft.com/kb/95474/en-us" onclick="window.open(this.href);return false; for an overview of available codes (although the article is old, it still applies to recent versions of Word).
Best wishes,
Hans
Hans
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: replace styled word with prefix and postfix strings
Conceded... In a quick test I got this:
In a more thorough test including styled paragraphs (and clicking Replace All), it works.
In a more thorough test including styled paragraphs (and clicking Replace All), it works.
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: replace styled word with prefix and postfix strings
Thank you so much Hans for the explanation and the link.HansV wrote:Yep, ^& is the code for 'Find what text'. See http://support.microsoft.com/kb/95474/en-us" onclick="window.open(this.href);return false; for an overview of available codes (although the article is old, it still applies to recent versions of Word).
Now, I know where my first port of call would be.
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: replace styled word with prefix and postfix strings
Hi
I am trying the above with minor changes but to no avail.
For example, this time I am looking for WARNING styled paragraphs and replace them with the word "WARNING: (the found text)". There is no poststring to this. But, this doesn't seem to work. :-(
The code is as follows:
Attached is a test document where I am doing this. It has a module with couple of my test macros and the above sub.
Perhaps, it is a simple tweek to fix this issue.
Thanks.
Robie
I am trying the above with minor changes but to no avail.
For example, this time I am looking for WARNING styled paragraphs and replace them with the word "WARNING: (the found text)". There is no poststring to this. But, this doesn't seem to work. :-(
The code is as follows:
Code: Select all
Sub ReplaceAllWarningParagraphs()
'
' ReplaceAllWarningParagraphs Macro
'
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Warning")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Normal")
With Selection.Find
.Text = ""
.Replacement.Text = "WARNING: ^&^p"
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Perhaps, it is a simple tweek to fix this issue.
Thanks.
Robie
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78608
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: replace styled word with prefix and postfix strings
There is no need to add an extra ^p. And I assume that you want to each paragraph set to the Warning style with "WARNING: ..." separately, instead of a whole block at once.
Code: Select all
Sub ReplaceAllWarningParagraphs()
'
' ReplaceAllWarningParagraphs Macro
'
'
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Warning")
.Replacement.ClearFormatting
.Replacement.Style = ActiveDocument.Styles("Normal")
.Text = "*^13"
.Replacement.Text = "WARNING: ^&"
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: replace styled word with prefix and postfix strings
Thanks Hans but the last 4 WARNING paragraphs are replaced with a single WARNING: (followed by 4 paragraphs).HansV wrote:There is no need to add an extra ^p. And I assume that you want to each paragraph set to the Warning style with "WARNING: ..." separately, instead of a whole block at once.
Code: Select all
Sub ReplaceAllWarningParagraphs() ' ' ReplaceAllWarningParagraphs Macro ' ' Selection.HomeKey Unit:=wdStory With Selection.Find .ClearFormatting .Style = ActiveDocument.Styles("Warning") .Replacement.ClearFormatting .Replacement.Style = ActiveDocument.Styles("Normal") .Text = "*^13" .Replacement.Text = "WARNING: ^&" .Forward = True .Wrap = wdFindStop .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll End With End Sub
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78608
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: replace styled word with prefix and postfix strings
Are you sure that you ran the version that I posted? I get
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: replace styled word with prefix and postfix strings
Apologies Hans, I didn't copy your code. Silly me.HansV wrote:Are you sure that you ran the version that I posted? I get
It works perfectly.
Thank you once again Hans.