|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
Quote:
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? |
#4
|
|||
|
|||
Try ...
Code:
Dim x As Long Code:
Dim x As Integer |
#5
|
|||
|
|||
Good idea. Seems I'm a bit rusty at this game.
|
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
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 |
#8
|
||||
|
||||
Works for me! Did you check the 'use wildcards' Find option?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
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. |
Tags |
append, replace, vb6 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 color row of Word table based on found text | kristib | Word VBA | 4 | 11-15-2015 02:42 PM |
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 |