Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2018, 09:28 AM
moorea21 moorea21 is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 64bit
Novice
Macro to append text to end of paragraphs/lines where target text is found.
 
Join Date: Jun 2018
Posts: 8
moorea21 is on a distinguished road
Default Macro to append text to end of paragraphs/lines where target text is found.

Hi,


I am trying to use this code:-


Sub Rpl()
Dim singleLine As Paragraph


Dim lineText As String
Dim MyPos

For Each singleLine In ActiveDocument.Paragraphs
lineText = singleLine.Range.Text

MyPos = InStr(lineText, "Test Text")
'If MyPos = 1 Then Replace(lineText , lineText & "LxW ")
If MyPos = 1 Then lineText = lineText & "LxW "
Next singleLine
End Sub


Although the value of 'lineText' gets updated, it doesn't replace it in the document, so I tried a VB6 'replace' function, which is invalid. How would I get the new value of lineText to replace the old value in the document?


Thanks
Reply With Quote
  #2  
Old 06-19-2018, 11:31 AM
d4okeefe d4okeefe is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 10 Macro to append text to end of paragraphs/lines where target text is found. Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

Try this. I prefer to use a for loop (rather than a for each loop) here.
Code:
Sub Rpl()
    Dim lineText As String
    Dim MyPos
    
    Dim x As Integer
    For x = 1 To ActiveDocument.Paragraphs.Count
        lineText = ActiveDocument.Paragraphs(x).Range.Text
        MyPos = InStr(lineText, "Test Text")
        If MyPos = 1 Then
            ActiveDocument.Paragraphs(x).Range.Text = lineText & "LxW "
        End If
    Next x
End Sub
Also, notice that in your original you only change the string -- lineText. The actual paragraph -- singleLine -- is never changed.
Reply With Quote
  #3  
Old 06-19-2018, 12:18 PM
moorea21 moorea21 is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 64bit
Novice
Macro to append text to end of paragraphs/lines where target text is found.
 
Join Date: Jun 2018
Posts: 8
moorea21 is on a distinguished road
Default

Quote:
Originally Posted by d4okeefe View Post
Also, notice that in your original you only change the string -- lineText. The actual paragraph -- singleLine -- is never changed.

Of course! Obvious now it's pointed out, thank you for that.


Only trouble is, I'm getting an overrun mnaeesage; error 6. There are over 430,000 paragraphs in the document, is that beyond what x can equal?
Reply With Quote
  #4  
Old 06-19-2018, 12:44 PM
d4okeefe d4okeefe is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 10 Macro to append text to end of paragraphs/lines where target text is found. Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

Try ...
Code:
Dim x As Long
... in place of ...
Code:
Dim x As Integer
Reply With Quote
  #5  
Old 06-19-2018, 01:38 PM
moorea21 moorea21 is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 64bit
Novice
Macro to append text to end of paragraphs/lines where target text is found.
 
Join Date: Jun 2018
Posts: 8
moorea21 is on a distinguished road
Default

Good idea. Seems I'm a bit rusty at this game.
Reply With Quote
  #6  
Old 06-19-2018, 06:27 PM
macropod's Avatar
macropod macropod is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You don't even need a macro for this - just a wildcard Find/Replace, where, for example:
Find = (Test Text*)^13
Replace = \1 LxW^p
And if you're wedded to using a macro, the above recorded as one would be way faster than looping through every paragraph.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-20-2018, 12:45 AM
moorea21 moorea21 is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 64bit
Novice
Macro to append text to end of paragraphs/lines where target text is found.
 
Join Date: Jun 2018
Posts: 8
moorea21 is on a distinguished road
Default

Thanks Paul, I tried that, it couldn't find the target text. Below is an example of the format of text the document contains:


Test Text 21.04.18 22:03:00
Hello!




Test Text2 21.04.18 22:04:00
Hello!


etc.


The first line should end up reading



Test Text 21.04.18 22:03:00 LxW

Reply With Quote
  #8  
Old 06-20-2018, 12:49 AM
macropod's Avatar
macropod macropod is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Works for me! Did you check the 'use wildcards' Find option?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 06-20-2018, 12:52 AM
moorea21 moorea21 is offline Macro to append text to end of paragraphs/lines where target text is found. Windows 7 64bit Macro to append text to end of paragraphs/lines where target text is found. Office 2010 64bit
Novice
Macro to append text to end of paragraphs/lines where target text is found.
 
Join Date: Jun 2018
Posts: 8
moorea21 is on a distinguished road
Default

Erm, no... Duh!
Yes it works great, a lot faster than a macro, which I only used as I couldn't work out how to do it your way! Thanks.
Reply With Quote
Reply

Tags
append, replace, vb6

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to append text to end of paragraphs/lines where target text is found. Macro to hide all paragraphs that do not have the found result jplat Word VBA 1 09-10-2017 09:13 PM
Delete Text from Insertion Point to Found Text feenyman99 Word 0 04-17-2016 05:40 PM
Macro to append text to end of paragraphs/lines where target text is found. Macro to color row of Word table based on found text kristib Word VBA 4 11-15-2015 02:42 PM
Macro to append text to end of paragraphs/lines where target text is found. Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank caboy Word VBA 2 04-01-2015 07:00 AM
Macro to Insert Text Into Cells Having Multiple Lines revans611 Excel Programming 4 10-24-2011 10:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:36 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