replace styled word with prefix and postfix strings

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

replace styled word with prefix and postfix strings

Post by Robie »

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.
45.png
Thanks
Robie
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
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

Post by HansV »

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.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: replace styled word with prefix and postfix strings

Post by Rudi »

You might need an extra paragraph mark in there?

Code: Select all

[code]^p^&^p
^p[/code]
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
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

Post by HansV »

No, why? The original Code styled paragraph already has a paragraph mark ¶ at the end.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: replace styled word with prefix and postfix strings

Post by Robie »

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.
Thanks Hans. That is perfect. :fanfare:

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?

User avatar
HansV
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

Post by HansV »

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

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: replace styled word with prefix and postfix strings

Post by Rudi »

Conceded... In a quick test I got this:
1.jpg
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.

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: replace styled word with prefix and postfix strings

Post by Robie »

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).
Thank you so much Hans for the explanation and the link.
Now, I know where my first port of call would be.

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: replace styled word with prefix and postfix strings

Post by Robie »

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:

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
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
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
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

Post by HansV »

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

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: replace styled word with prefix and postfix strings

Post by Robie »

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
Thanks Hans but the last 4 WARNING paragraphs are replaced with a single WARNING: (followed by 4 paragraphs).
46.png
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
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

Post by HansV »

Are you sure that you ran the version that I posted? I get
S0803.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: replace styled word with prefix and postfix strings

Post by Robie »

HansV wrote:Are you sure that you ran the version that I posted? I get
S0803.png
Apologies Hans, I didn't copy your code. Silly me. :groan:

It works perfectly.

Thank you once again Hans. :clapping: :fanfare: :cheers: