Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-30-2019, 11:48 AM
noslenwerd noslenwerd is offline VBA to find text, replace with multiple lines of text Windows 10 VBA to find text, replace with multiple lines of text Office 2016 for Mac
Novice
VBA to find text, replace with multiple lines of text
 
Join Date: Dec 2019
Posts: 15
noslenwerd is on a distinguished road
Default VBA to find text, replace with multiple lines of text


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):

Code:
                                     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 VBA to find text in Word, replace with multiple lines of text
Reply With Quote
  #2  
Old 12-30-2019, 05:42 PM
gmaxey gmaxey is online now VBA to find text, replace with multiple lines of text Windows 10 VBA to find text, replace with multiple lines of text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Well TabValue (assuming that has been set to the value of your long string of replacement text) is too long for a Replacement.Text property.

You could have that long string set somewhere in the document e.g., a bookmark named "bmText" and use:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim strFind As String
Dim strReplace As String
  strFind = "<<testtag>>"
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = strFind
    While .Execute
      oRng.Text = ActiveDocument.Bookmarks("bmText").Range.Text
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 12-31-2019, 10:51 AM
noslenwerd noslenwerd is offline VBA to find text, replace with multiple lines of text Windows 10 VBA to find text, replace with multiple lines of text Office 2016 for Mac
Novice
VBA to find text, replace with multiple lines of text
 
Join Date: Dec 2019
Posts: 15
noslenwerd is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Well TabValue (assuming that has been set to the value of your long string of replacement text) is too long for a Replacement.Text property.

You could have that long string set somewhere in the document e.g., a bookmark named "bmText" and use:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim strFind As String
Dim strReplace As String
  strFind = "<<testtag>>"
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = strFind
    While .Execute
      oRng.Text = ActiveDocument.Bookmarks("bmText").Range.Text
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
Sorry not sure I follow.

Are you suggesting I house all the html content (about 40 lines) in a bookmark?

I am technically not using bookmarks in the overall macro, so not sure that will work.
Reply With Quote
  #4  
Old 12-31-2019, 11:04 AM
gmaxey gmaxey is online now VBA to find text, replace with multiple lines of text Windows 10 VBA to find text, replace with multiple lines of text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

That was just one suggestion. You could house it in another document, in a table cell, a building block or perhaps a concatenated string. It is just too long for the Replace.Text property.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace header text across multiple files LG1972 Excel 1 12-25-2018 04:27 AM
VBA to find text, replace with multiple lines of text Find & Replace text in Field Code across multiple documents RPM7 Word VBA 6 05-12-2017 12:58 AM
VBA to find text, replace with multiple lines of text Need assistance to find and replace text within headers/footers in excel - multiple worksheets teddycatnc Excel 3 03-07-2016 12:04 AM
Macro to find coloured text and replace with form-field/formtext containing that text tarktran Word VBA 1 11-26-2014 08:12 AM
VBA to find text, replace with multiple lines of text Find and Replace multiple lines/paragraphs jcw Word 1 11-18-2011 11:47 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:05 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft