DocVariables - Copy / PasteSpecial

E

erikrthorne

I populate a word doc from an excel spreadsheet using doc variables.
This document will enventually go to a client, so I would like the
DocVariables "removed", while keeping the values themselves. similar
to a copy / pastespecial / values works in excel.

I can accomplish this manually in word by highlighting the variable,
hitting copy / paste special / unformatted text. However, when I try
to do so in VBA, it seems to simply copy back the DocVariable.

Here is the code:

set wrdApp = Word.Application
Dim myField As Field
For Each myField In ActiveDocument.Fields
Debug.Print myField.Code
myField.Result.Select
wrdApp.Selection.Copy
wrdApp.Selection.PasteAndFormat (wdFormatPlainText)
Next

Why would this work manually, but not via VBA? I've tried other
(wdFormat...) options, but with the same result.

Thanks for any help!!!
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
I populate a word doc from an excel spreadsheet using doc variables.
This document will enventually go to a client, so I would like the
DocVariables "removed", while keeping the values themselves. similar
to a copy / pastespecial / values works in excel.

I can accomplish this manually in word by highlighting the variable,
hitting copy / paste special / unformatted text. However, when I try
to do so in VBA, it seems to simply copy back the DocVariable.

You are using a jackhammer to punch a hole through paper! ;-)

It is much easier to simply unlink the field:

To unlink only document variable fields:

'_______________________________________
Sub UnlinkDocVar()

Dim myField As Field

For Each myField In ActiveDocument.Fields
If myField.Type = wdFieldDocVariable Then myField.Unlink
Next

End Sub
'_______________________________________

To unlink all fields:
'_______________________________________
Sub UnlinkAllVar()

ActiveDocument.Fields.Unlink

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

erikrthorne

Holy Cow...you made my whole weekend. I'm a little better on the Excel
side than I am the Word side, you have no idea how long I was trying to
get that jackhammer to work! My first post to the group, and a
solution in under an hour....Awesome, and Thank you!

Erik Thorne
 

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