Record Copy and Paste Macro, want to repeat throughout the document

J

jon.jefferson

Hi All!,

I am new to writing macros in Word/VBA. I have recorded a macro that
I need to repeat through the end of the document. All help is
appreciated.


Jon
 
K

Klaus Linke

Hi Jon,

Write a bit more about what you want the macro to do...

You know you can copy stuff to the clipboard, and then replace some text
with the (formatted) clipboard content, using ^c in "Edit > Replace >
Replace with"?

Regards,
Klaus
 
J

JB

Hi Jon,

Write a bit more about what you want the macro to do...

You know you can copy stuff to the clipboard, and then replace some text
with the (formatted) clipboard content, using ^c in "Edit > Replace >
Replace with"?

Regards,
Klaus

Klaus,

Thank you for responding. OK, here is the macro I recorded. What I
need to do via a macro is copy, paste and repeat throughout the entire
document. The format of the document is:

dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member:

I need the macro to copy and paste the "cn=" portion from
"equivalentToMe:" to "Member:"
The following macro works but I need it to repeat through the entire
file, or if you have a better way of doing what I need, that would
help as well.

Selection.Find.ClearFormatting
With Selection.Find
.Text = "cn=""*"""
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.copy
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "newrdn:"
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Repeat
End Sub

Thank You,

Jon
 
K

Klaus Linke

Hi Jon,

Your macro doesn't work with the sample you posted (there's no "newrdn:"),
so I'm not sure what it's supposed to do.

So let me make a sample that works...:

((Before))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member:

~~~~~~~~~~~~

((After))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member: cn=group

~~~~~~~~~~~~

You could do that with a wildcard replacement.
In the "Find/Replace" dialog, check "Match wildcards".

First, you'll have to figure out what you want to copy to the other
location, and put it in braces:
Find what: dn: (cn=[!,]@),

The braces allow you to re-insert that expression later.

Let's also match the following text, up to where we want to insert the
group:
dn:[!^13^11]@(cn=[!,]@),*Member:

(Instead of both [!^13^11]@ and [!,]@ you could also use the * wildcard, but
it's safter to restrict matches a bit)

Now, we want to leave everything as it was, and insert a blank and the stuff
from the second group:
Replace with: ^& \1

^& inserts the whole stuff you matched, \1 puts in what you matched in your
(first) braced expression.

If that's not what you had in mind, exactly, maybe you can figure out how to
adapt that wildcard replacement... or post back!

Regards,
Klaus
 
J

JB

Hi Jon,

Your macro doesn't work with the sample you posted (there's no "newrdn:"),
so I'm not sure what it's supposed to do.

So let me make a sample that works...:

((Before))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member:

~~~~~~~~~~~~

((After))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member: cn=group

~~~~~~~~~~~~

You could do that with a wildcard replacement.
In the "Find/Replace" dialog, check "Match wildcards".

First, you'll have to figure out what you want to copy to the other
location, and put it in braces:
Find what: dn: (cn=[!,]@),

The braces allow you to re-insert that expression later.

Let's also match the following text, up to where we want to insert the
group:
dn:[!^13^11]@(cn=[!,]@),*Member:

(Instead of both [!^13^11]@ and [!,]@ you could also use the * wildcard, but
it's safter to restrict matches a bit)

Now, we want to leave everything as it was, and insert a blank and the stuff
from the second group:
Replace with: ^& \1

^& inserts the whole stuff you matched, \1 puts in what you matched in your
(first) braced expression.

If that's not what you had in mind, exactly, maybe you can figure out how to
adapt that wildcard replacement... or post back!

Regards,
Klaus
Thank you for responding. OK, here is the macro I recorded. What I
need to do via a macro is copy, paste and repeat throughout the entire
document. The format of the document is:
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

I need the macro to copy and paste the "cn=" portion from
"equivalentToMe:" to "Member:"
The following macro works but I need it to repeat through the entire
file, or if you have a better way of doing what I need, that would
help as well.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "cn=""*"""
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.copy
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "newrdn:"
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Repeat
End Sub
Thank You,
Klaus,

Sorry about the macro/example. But here is a more accurate example of
what the above script does:

(((Before))))

dn: cn=user1,o=root

changetype:moddn
newrdn:
deletoldrdn:1
newsuperior: ou=NewOU,o=root

(((After)))

dn: cn=user1,o=root

changetype:moddn
newrdn:cn=user1
deletoldrdn:1
newsuperior: ou=NewOU,o=root

What I would like to happen is actually repeat those steps throughout
the entire document because this (see above example) is just one entry
in the document. There are many many more throughout the document.
Now correct me if I misunderstand, but what you are telling to me to
do is place brackets here (cn=user1) and here: newrdn:( ). Then use
'Find/Replace', checking 'Use Wildcards' put in Find what: (*) (*)
and in the Replace with: field type in (\2) (\1). Is this correct?

thank you again,

Jon
 
K

Klaus Linke

(((Before))))
dn: cn=user1,o=root

changetype:moddn
newrdn:
deletoldrdn:1
newsuperior: ou=NewOU,o=root

(((After)))

dn: cn=user1,o=root

changetype:moddn
newrdn:cn=user1
deletoldrdn:1
newsuperior: ou=NewOU,o=root


Hi Jon,

If I understand your sample, "cn=" always stays the same, in each record,
but "user1" may vary ... right?

First, we need to match "cn=user1" -- let's take the simplest wildcard
expression:
Find what: (cn=*)

.... in brackets, so we can re-use it.
Then, we need to match more text, up to "newrdn:":
Find what: (cn=*),*newrdn:

We want to leave evberything we matched, but insert "cn=user1" after that:
Replace with: ^& \1

That should work, if all the records look like the sample. But it's a pretty
simple wildcard expression.
You could make it much more complex, but safer, say so that it never spans
more than one record, or observes other restrictions.

Regards,
Klaus



JB said:
Hi Jon,

Your macro doesn't work with the sample you posted (there's no
"newrdn:"),
so I'm not sure what it's supposed to do.

So let me make a sample that works...:

((Before))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member:

~~~~~~~~~~~~

((After))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

replace:Member
Member: cn=group

~~~~~~~~~~~~

You could do that with a wildcard replacement.
In the "Find/Replace" dialog, check "Match wildcards".

First, you'll have to figure out what you want to copy to the other
location, and put it in braces:
Find what: dn: (cn=[!,]@),

The braces allow you to re-insert that expression later.

Let's also match the following text, up to where we want to insert the
group:
dn:[!^13^11]@(cn=[!,]@),*Member:

(Instead of both [!^13^11]@ and [!,]@ you could also use the * wildcard,
but
it's safter to restrict matches a bit)

Now, we want to leave everything as it was, and insert a blank and the
stuff
from the second group:
Replace with: ^& \1

^& inserts the whole stuff you matched, \1 puts in what you matched in
your
(first) braced expression.

If that's not what you had in mind, exactly, maybe you can figure out how
to
adapt that wildcard replacement... or post back!

Regards,
Klaus
Write a bit more about what you want the macro to do...
You know you can copy stuff to the clipboard, and then replace some
text
with the (formatted) clipboard content, using ^c in "Edit > Replace >
Replace with"?
JB said:
I am new to writing macros in Word/VBA. I have recorded a macro
that
I need to repeat through the end of the document. All help is
appreciated.


Thank you for responding. OK, here is the macro I recorded. What I
need to do via a macro is copy, paste and repeat throughout the entire
document. The format of the document is:
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root

I need the macro to copy and paste the "cn=" portion from
"equivalentToMe:" to "Member:"
The following macro works but I need it to repeat through the entire
file, or if you have a better way of doing what I need, that would
help as well.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "cn=""*"""
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.copy
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "newrdn:"
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Repeat
End Sub
Thank You,
Klaus,

Sorry about the macro/example. But here is a more accurate example of
what the above script does:

(((Before))))

dn: cn=user1,o=root

changetype:moddn
newrdn:
deletoldrdn:1
newsuperior: ou=NewOU,o=root

(((After)))

dn: cn=user1,o=root

changetype:moddn
newrdn:cn=user1
deletoldrdn:1
newsuperior: ou=NewOU,o=root

What I would like to happen is actually repeat those steps throughout
the entire document because this (see above example) is just one entry
in the document. There are many many more throughout the document.
Now correct me if I misunderstand, but what you are telling to me to
do is place brackets here (cn=user1) and here: newrdn:( ). Then use
'Find/Replace', checking 'Use Wildcards' put in Find what: (*) (*)
and in the Replace with: field type in (\2) (\1). Is this correct?

thank you again,

Jon
 
J

JB

(((Before))))
dn: cn=user1,o=root
changetype:moddn
newrdn:
deletoldrdn:1
newsuperior: ou=NewOU,o=root

dn: cn=user1,o=root
changetype:moddn
newrdn:cn=user1
deletoldrdn:1
newsuperior: ou=NewOU,o=root

Hi Jon,

If I understand your sample, "cn=" always stays the same, in each record,
but "user1" may vary ... right?

First, we need to match "cn=user1" -- let's take the simplest wildcard
expression:
Find what: (cn=*)

... in brackets, so we can re-use it.
Then, we need to match more text, up to "newrdn:":
Find what: (cn=*),*newrdn:

We want to leave evberything we matched, but insert "cn=user1" after that:
Replace with: ^& \1

That should work, if all the records look like the sample. But it's a pretty
simple wildcard expression.
You could make it much more complex, but safer, say so that it never spans
more than one record, or observes other restrictions.

Regards,
Klaus

JB said:
Hi Jon,
Your macro doesn't work with the sample you posted (there's no
"newrdn:"),
so I'm not sure what it's supposed to do.
So let me make a sample that works...:
((Before))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root
replace:Member
Member:
~~~~~~~~~~~~
((After))
~~~~~~~~~~~~
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root
replace:Member
Member: cn=group
~~~~~~~~~~~~
You could do that with a wildcard replacement.
In the "Find/Replace" dialog, check "Match wildcards".
First, you'll have to figure out what you want to copy to the other
location, and put it in braces:
Find what: dn: (cn=[!,]@),
The braces allow you to re-insert that expression later.
Let's also match the following text, up to where we want to insert the
group:
dn:[!^13^11]@(cn=[!,]@),*Member:
(Instead of both [!^13^11]@ and [!,]@ you could also use the * wildcard,
but
it's safter to restrict matches a bit)
Now, we want to leave everything as it was, and insert a blank and the
stuff
from the second group:
Replace with: ^& \1
^& inserts the whole stuff you matched, \1 puts in what you matched in
your
(first) braced expression.
If that's not what you had in mind, exactly, maybe you can figure out how
to
adapt that wildcard replacement... or post back!
Regards,
Klaus
Write a bit more about what you want the macro to do...
You know you can copy stuff to the clipboard, and then replace some
text
with the (formatted) clipboard content, using ^c in "Edit > Replace >
Replace with"?
I am new to writing macros in Word/VBA. I have recorded a macro
that
I need to repeat through the end of the document. All help is
appreciated.
Jon
Klaus,
Thank you for responding. OK, here is the macro I recorded. What I
need to do via a macro is copy, paste and repeat throughout the entire
document. The format of the document is:
dn: cn=group,ou=org,o=root
changetype:modify
add:equivalentToMe
equivalentToMe:cn=name,o=root
replace:Member
Member:
I need the macro to copy and paste the "cn=" portion from
"equivalentToMe:" to "Member:"
The following macro works but I need it to repeat through the entire
file, or if you have a better way of doing what I need, that would
help as well.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "cn=""*"""
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.copy
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = "newrdn:"
.Replacement.Text = "oldr"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Repeat
End Sub
Thank You,
Jon Klaus,

Sorry about the macro/example. But here is a more accurate example of
what the above script does:
(((Before))))

dn: cn=user1,o=root
changetype:moddn
newrdn:
deletoldrdn:1
newsuperior: ou=NewOU,o=root

dn: cn=user1,o=root
changetype:moddn
newrdn:cn=user1
deletoldrdn:1
newsuperior: ou=NewOU,o=root
What I would like to happen is actually repeat those steps throughout
the entire document because this (see above example) is just one entry
in the document. There are many many more throughout the document.
Now correct me if I misunderstand, but what you are telling to me to
do is place brackets here (cn=user1) and here: newrdn:( ). Then use
'Find/Replace', checking 'Use Wildcards' put in Find what: (*) (*)
and in the Replace with: field type in (\2) (\1). Is this correct?
thank you again,

Klaus,

Thank you for your help and advice, I appreciate it!

sincerely,

Jon
 
K

Klaus Linke

Hi Jon,

Glad if it did help... I wasn't so sure about what your requirements were.
Wildcard replacements are really sweet, if you've gotten beyond the initial
hurdles.
You could do much the same thing with string functions (Instr, Left, Like
....), but it's often more difficult and debug such macros than to write a
wildcard replacement (... if it doesn't work the first time, you just
Undo -- Ctrl+Z -- and try once more). Same with copy/paste, which likely
also has some speed penalty, when you work with large documents.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top