PDA

View Full Version : Replace and merge phrases



klosar
05-25-2010, 12:36 PM
I need some (for me at least) advance stuff regarding macro...I m new to this and I would really appreciate any help.
This is what I have:

- Mark 1 (something written here), again some text here
- Mark 1 (something different here), more text here
- Mark 1 (words here), more text

Mark 1 is always the same (it stands for names, so there are two words here). This is what i need:
1. Macro needs to delete Mark 1 in all the lines
2. This part - ), - needs to be replaced with - ); -
3. Everything after the ); needs to be erased
4. All the lines need to be merged

This is how it should look like in the end

- (something written here); (something different here);(words here);

Thnx

mdmackillop
06-01-2010, 11:51 AM
Option Explicit

Sub test()
Dim Match, Matches
Dim RegExp As Object
Dim t As String

Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.IgnoreCase = True
End With

RegExp.Pattern = "\(.*?\)"
Set Matches = RegExp.Execute(ActiveDocument.Range.Text)

t = "-"
For Each Match In Matches
t = t & Match & "; "
Next

ActiveDocument.Content.InsertAfter Text:=vbCr & t
End Sub

fumei
06-01-2010, 12:24 PM
Is this posted elsewhere? I sure seem to remember dealing with this.

klosar
06-04-2010, 01:20 AM
First,Thnx for helping me.


This is what happens. I run macro (for some reason it takes some time for it to process, 30-60 seconds), but nothing happens. No errors or anything. Things just remain how they were before.

Tinbendr
06-04-2010, 02:12 AM
With the sample provided, it works for me.

It adds it to the bottom on the document while leaving the original untouched.

If you want to add the results to a new document then...
Sub test2()
Dim Match, Matches
Dim RegExp As Object
Dim t As String

Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.IgnoreCase = True
End With

RegExp.Pattern = "\(.*?\)"
Set Matches = RegExp.Execute(ActiveDocument.Range.Text)

Documents.Add

t = "-"
For Each Match In Matches
t = t & Match & "; "
Next

ActiveDocument.Content.InsertAfter Text:=vbCr & t
End Sub

klosar
06-05-2010, 12:24 PM
Tinbendr version runs great, thank you guys!! If you could just modify it to run only on selected part of the text, not the whole thing like now, dont know how to modify that....If you dont find the time never mind, it s amazing as it is.
Thank you very much!

mdmackillop
06-05-2010, 12:53 PM
Just change this line

Set Matches = RegExp.Execute(Selection)

klosar
06-06-2010, 11:14 AM
That was it. Thanks again to everyone for support! You helped me a lot :D

geekgirlau
06-07-2010, 08:12 PM
Hi Klosar,

Welcome to the board.

I've just renamed your thread to make it more specific to your actual problem.

Generally 99.9% of all threads are from people needing help with a macro! It's a good idea to use a title that describes the specific problem - you'll get much more responses that way.