Cut and paste merge field codes

D

dixie

Is there a simple way of copying a set of merge field conditions oout of a
mail merge document and then pasting the codes in a way someone can read the
code, rather than the results of the code? It'd be really good for posting
code in a newsgroup. It can be very tedious typing a long sequence of code
you have already typed in Word.
 
M

macropod

Hi Dixie,

If you select the field that you want to copy, then run the following macro,
a 'plain text' version of the field will be placed in the clipboard for
pasting.

Cheers

Sub FieldCodeToString()
Dim Fieldstring As String, NewString As String, CurrChar As String
Dim CurrSetting As Boolean, fcDisplay As Object
Dim MyData As DataObject, X As Integer
NewString = ""
Set fcDisplay = ActiveWindow.View
Application.ScreenUpdating = False
CurrSetting = fcDisplay.ShowFieldCodes
If CurrSetting <> True Then fcDisplay.ShowFieldCodes = True
Fieldstring = Selection.Text
For X = 1 To Len(Fieldstring)
CurrChar = Mid(Fieldstring, X, 1)
Select Case CurrChar
Case Chr(19)
CurrChar = "{"
Case Chr(21)
CurrChar = "}"
Case Else
End Select
NewString = NewString + CurrChar
Next X
Set MyData = New DataObject
MyData.SetText NewString
MyData.PutInClipboard
fcDisplay.ShowFieldCodes = CurrSetting
End Sub
 
D

dixie

Hi macropod - nice name :). Bear with me here, I've never done this before.
I usually only use Word to do some mail merge from Access. I entered the
code in as a macro and called it FieldCodeToString. I then called up that
macro and ran it. It stopped at the line:
Dim MyData As DataObject, X As Integer
with aUser-defined type not defined. Now if this was in Access, I would
think I didn't perhaps have the right dll files installed. Is that what is
causing this to happen?

dixie
 
M

macropod

Hi Dixie,

Since this is a macro that I use from time to time, I know it works. You
should be able to simply copy and paste it into a Word vba module (called up
via Alt-F11), without having to name it.

Alternatively, perhaps you don't have all the required vba library
references defined. If you press Alt-F11, you can see them listed under
Tools|References. In my case, the checked ones are:
.. visual basic for applications
.. Microsoft Word # object library (where # is your current Word version)
.. OLE automation
.. Microsoft Office # object library (where # is your current Word version)
.. Microsoft Forms 2.0 object library
If you make sure that you have at least these checked, things should work
OK.

Cheers
 
D

dixie

Followed that to a 'T'. But still same result - I don't have the reference
Microsoft Forms 2.0 - it seems to be missing from the list. That might be
it!

dixie
 
D

dixie

The only other thing that might be different I guess is that this is a
template, not a .doc file.

dixie
 
M

macropod

Hi Dixie,

In that case, scroll down through the available list of vba references and
add the 'Microsoft Forms 2.0 object' by clicking on it.

Cheers
 
D

dixie

But it is not in the list of references!!

dixie

macropod said:
Hi Dixie,

In that case, scroll down through the available list of vba references and
add the 'Microsoft Forms 2.0 object' by clicking on it.

Cheers
 
G

Graham Mayor

I had high hopes for this macro, but it doesn't work for me either and I do
have all the quoted references checked. I get no error messages, but no
conversion either.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

macropod

Hi Graham,

To use the macro, you simply select the field to be converted, then run the
macro to put the 'plain text' version into the clipboard. Then simply select
wherever you want the output to go, be it Word, notepad, your email client,
or whatever, and paste the results (eg Ctrl-V) into it.

In Dixie's case, the lack of a 'Microsoft Forms 2.0 object' in the available
references suggests that some of the Word/Office 2000 functionality may not
have been installed. Any idea which components install that library?

Cheers

Graham Mayor said:
I had high hopes for this macro, but it doesn't work for me either and I do
have all the quoted references checked. I get no error messages, but no
conversion either.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

dixie

YES!! I got it to work. I did a browse for the Microsoft Forms 2.0
reference and I found a C:\WINDOWS\system32\FM20.dll and just selected it.
Voila, the reference Microsoft Forms 2.0 Object Library was showing as
ticked. Then I had a go at highlighting the Field codes and running the
macro. I then went to another empty word document and did a CTRL V to paste
it and there were all the field codes using the normal {} keys instead of
the proper CTRL F9 ones. Macropod, you're a genius.

dixie

macropod said:
Hi Graham,

To use the macro, you simply select the field to be converted, then run
the
macro to put the 'plain text' version into the clipboard. Then simply
select
wherever you want the output to go, be it Word, notepad, your email
client,
or whatever, and paste the results (eg Ctrl-V) into it.

In Dixie's case, the lack of a 'Microsoft Forms 2.0 object' in the
available
references suggests that some of the Word/Office 2000 functionality may
not
have been installed. Any idea which components install that library?

Cheers
 
D

dixie

When I finally found the right dll and got the forms 2.0 reference in place,
I tried to use it and the first time it didn't work for me either. It was
then I realised that when macropod said select the field, he meant totally
selected as in highlighted by sweeping the mouse cursor through it. If you
just click in it, it does all go grey, but it doesn't work.

dixie

Graham Mayor said:
I had high hopes for this macro, but it doesn't work for me either and I do
have all the quoted references checked. I get no error messages, but no
conversion either.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

dixie

Right, got it working fine now, but it only works on the one template file.
Can I install the macro code so that it works universally on any mailmerge
document on my computer. If so, where do I put the code?

dixie

macropod said:
Hi Graham,

To use the macro, you simply select the field to be converted, then run
the
macro to put the 'plain text' version into the clipboard. Then simply
select
wherever you want the output to go, be it Word, notepad, your email
client,
or whatever, and paste the results (eg Ctrl-V) into it.

In Dixie's case, the lack of a 'Microsoft Forms 2.0 object' in the
available
references suggests that some of the Word/Office 2000 functionality may
not
have been installed. Any idea which components install that library?

Cheers
 
D

dixie

Another question comes to mind now that this is working. Can you sort of do
that in reverse, such that your could publish the code in an email or text
message and it could be pasted into a mail merge document and then have the
ordinary brackets changed to proper field code brackets again, meaning that
the person would not have to manually copy the code into the mail merge
document.

dixie

macropod said:
Hi Graham,

To use the macro, you simply select the field to be converted, then run
the
macro to put the 'plain text' version into the clipboard. Then simply
select
wherever you want the output to go, be it Word, notepad, your email
client,
or whatever, and paste the results (eg Ctrl-V) into it.

In Dixie's case, the lack of a 'Microsoft Forms 2.0 object' in the
available
references suggests that some of the Word/Office 2000 functionality may
not
have been installed. Any idea which components install that library?

Cheers
 
G

Graham Mayor

No who feels stupid? :(
Good one!
It shouldn't take much effort to add the code to paste the result in the
document now I know what it is supposed to be doing ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Hi Graham,

To use the macro, you simply select the field to be converted, then
run the macro to put the 'plain text' version into the clipboard.
Then simply select wherever you want the output to go, be it Word,
notepad, your email client, or whatever, and paste the results (eg
Ctrl-V) into it.

In Dixie's case, the lack of a 'Microsoft Forms 2.0 object' in the
available references suggests that some of the Word/Office 2000
functionality may not have been installed. Any idea which components
install that library?

Cheers
 
G

Graham Mayor

Put it in normal.dot

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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