PDA

View Full Version : VBA to find text in Word, replace with multiple lines of text



noslenwerd
12-30-2019, 11:49 AM
Having issues writing script to find a single tag, and replace it with multiple lines of text.

I wrote some VBA to find a tag such as:

<<testtag>>

And looking to replace it with something like below:

<h2>Live Chat</h2>
<p>A <a href="https://www.comm100.com/blog/live-chat-benefits.html">research report</a> found that Live Chat was hands down the preferred method of communication for online shoppers when compared to other service channels. A <a href="https://www.business2community.com/b2b-marketing/4-powerful-ways-live-chat-website-improves-b2b-inbound-sales-01775265">separate study</a> by the American Marketing Association states that customers that use live chat are three times more likely to make a purchase compared to those who don’t.</p><br>
<p>a <b>Best practices</b> for live chat include:</p>

The problem is it only replaces with the first line above: "<h2>Live Chat</h2>"

Any ideas? Below is the chunk of code responsible for this (please note I am using excel to execute in Word):



With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With


This has been cross posted at https://www.msofficeforums.com/word-vba/44133-vba-find-text-replace-multiple-lines-text.html#post147653

SamT
12-31-2019, 11:22 AM
Thread Moved to Word Forum

mariakenneth
04-17-2024, 02:24 AM
Having issues writing script to find a single tag, and replace it with multiple lines of text.

I wrote some VBA to find a tag such as:

<<testtag>>

And looking to replace it with something like below:

<h2>Live Chat</h2>
<p>A <a href="https://www.comm100.com/blog/live-chat-benefits.html">research report</a> found that Live Chat was hands down the preferred method of communication for online shoppers when compared to other service channels. A <a href="https://www.business2community.com/b2b-marketing/4-powerful-ways-live-chat-website-improves-b2b-inbound-sales-01775265">separate study</a> by the American Marketing Association states that customers that use live chat are three times more likely to make a purchase compared to those who don’t.</p><br>
<p>a <b>Best practices</b> for live chat include:</p>

The problem is it only replaces with the first line above: "<h2>Live Chat</h2>"

Any ideas? Below is the chunk of code responsible for this (please note I am using excel to execute in Word):



With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With


This has been cross posted at https://www.msofficeforums.com/word-vba/44133-vba-find-text-replace-multiple-lines-text.html#post147653 omegle new (https://omegle-new.com/)
It seems like you're having trouble with your VBA script for finding and replacing text in Word with multiple lines of text. The issue you're facing is that it only replaces with the first line of the replacement text. It looks like you're using Excel to execute this in Word.


One potential solution could be to use the Replace function instead of the Find method in VBA. Here's an example of how you might do this:


vba
Copy

Sub ReplaceTextInWord()
Dim WordApp As Object
Dim WordDoc As Object
Dim TagName As String
Dim TagValue As String


' Set up the Word application and document
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\path\to\your\document.docx")


' Define the tag and replacement text
TagName = "<<testtag>>"
TagValue = "<h2>Live Chat</h2>" & vbCrLf & _
"<p>A <a href=""https://www.comm100.com/blog/live-chat-benefits.html"">research report</a> found that Live Chat was hands down the preferred method of communication for online shoppers when compared to other service channels. A <a href=""https://www.business2community.com/b2b-marketing/4-powerful-ways-live-chat-website-improves-b2b-inbound-sales-01775265"">separate study</a> by the American Marketing Association states that customers that use live chat are three times more likely to make a purchase compared to those who don’t.</p><br>" & vbCrLf & _
"<p>a <b>Best practices</b> for live chat include:</p>"


' Perform the replacement
WordDoc.Content.Text = Replace(WordDoc.Content.Text, TagName, TagValue)


' Save and close the document
WordDoc.Save
WordDoc.Close
WordApp.Quit
End Sub
This code opens the Word document, performs the replacement using the Replace function, and then saves and closes the document. You can adjust the paths and file names as needed.


Please give this a try and let me know if it helps!

Aussiebear
04-17-2024, 05:19 AM
Welcome VBAX mariakenneth. Thank you taking the time to post a suggestion.