PDA

View Full Version : Remove the last word of a paragraph



heedaf
06-09-2014, 01:27 PM
I have a document that has different sized words at the end of each paragraph but they all start with the same first three characters. I can easily select these three characters but I'm unable to select the rest of the word so I can automatically delete it. Can anyone tell me how to do this in VBA? I've got a document that has several thousand of these and it would be nice to have VBA do it for me.

Example:

Run Paul run. SR10056
Run Paul run. SR13344
Run Paul run. SR155334553

So I would like to be able to delete the SR1****** on each line.

I've used

ActiveDocument.Bookmarks("\word").range

but it only selects what I've searched on. I've been able to selected the line and paragraph using (line and para) so I would think there would be a settting that would allow the word to be selected.

heedaf
06-09-2014, 04:14 PM
I have a document that has different sized words at the end of each paragraph but they all start with the same first three characters. I can easily select these three characters but I'm unable to select the rest of the word so I can automatically delete it. Can anyone tell me how to do this in VBA? I've got a document that has several thousand of these and it would be nice to have VBA do it for me.

Example:

Run Paul run. SR10056
Run Paul run. SR13344
Run Paul run. SR155334553

So I would like to be able to delete the SR1****** on each line.

I've used

ActiveDocument.Bookmarks("\word").range

but it only selects what I've searched on. I've been able to selected the line and paragraph using (line and para) so I would think there would be a settting that would allow the word to be selected.

heedaf
06-09-2014, 04:18 PM
I found a solution (not the greatest but it works) . Use the find method and do the following:
strTemp = selection.text
selection.moveleft
do while right(strTemp, 1) <> vbCr 'look for the carriage return
selection.delete
strTemp = selection.text
loop

macropod
06-09-2014, 05:11 PM
You can do this without a macro, using a wildcard Find/Replace, where:
Find = SR1[!^13 ]@^13
Replace = ^p
You could also use the macro recorder to turn it into a macro.