How do I disconnect merge fields when saving doc with new name?

F

foxdev

I am using Word 2007. My document was used in Word2000 & 2003 with no
problems. I have 8 merge fields and 1 database field, yes the document uses 2
different datasources with no problem. The first datasource has 1 record fior
the merge fields, the second datasource uses oledb. The document prints like
it is suppose to.

My issue is: I need to print it again. The document use to save and have
static data, another words when I used oDoc.saveas(newDocName), the merge
data became static. Now it is saved as dynamic data. When is the best time to
do the save, before execute or after execute? Is there a difference? How can
I make it static?
 
C

Cindy M.

Hi =?Utf-8?B?Zm94ZGV2?=,
I am using Word 2007. My document was used in Word2000 & 2003 with no
problems. I have 8 merge fields and 1 database field, yes the document uses 2
different datasources with no problem. The first datasource has 1 record fior
the merge fields, the second datasource uses oledb. The document prints like
it is suppose to.

My issue is: I need to print it again. The document use to save and have
static data, another words when I used oDoc.saveas(newDocName), the merge
data became static. Now it is saved as dynamic data. When is the best time to
do the save, before execute or after execute? Is there a difference? How can
I make it static?
I believe I answered this in a forum somewhere, a while back, but...

You should actually execute the merge to a new file, then save that. This file
will have no dynamic merge fields.

Another possibility would be to unlink or lock the fields in the document.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
F

foxdev

Cindy
Just wanted to let you know "I" figured it out. I am sad about all the miss
information on this website. A lot of previous Automation coding capibility
died when Office2007 entered the scene. Not only you but quite a few
"Professionals" are telling people solutions that just aren't right. With
"Compatability Mode" documents, I can do a merge document using 2 different
data sources. One source fills the header/body mergefields the other is a
database mergefield pointing to a different datasource that displays a table
up to 1000 rows. the database mergefield automatically fills when the merge
executes.Here is the code:

oDoc.MailMerge.OpenDataSource(oApp.DbfPath+psConn)
*do not need to add the datasource for the embedded query in the memo_pn.doc
If oDoc.MailMerge.State = 2 && the datasource connected
* always use a table for datasource with 1 record for mergefields
oDoc.MailMerge.DataSource.FirstRecord = 1
oDoc.MailMerge.DataSource.LastRecord = 1
oDoc.MailMerge.Destination = 1 && send doc to printer when executed
oDoc.MailMerge.Execute
wait "" timeout 5 && give doc 5sec to print because of Off2007
else
MESSAGEBOX("",16,"NO Data Attached to Document")

endif
* save memo_pn.doc for future printing
IF LOWER(SUBSTR(oDoc.name,RAT('_',oDoc.name)+1,2)) = "pn"
lsName = oApp.UserPath+psGrpId+"Lst.doc"
oDoc.MailMerge.MainDocumentType = -1 && wdNotAMergeDocument - keep as
static data
oDoc.SaveAs(lsName)
ENDIF

It took many hours to go through the new object model to determine this
solution. It would be nice if the "Professionals" were more informed to give
revelant answers to developers. your answer was partially right to save to
another doc. The trick is the "wdNotAMergeDocument" property. Please quit
telling developers that they cannot use 2 different datasources, I am doing
it.

VFP Developer loving developing
 
C

Cindy M.

Hi =?Utf-8?B?Zm94ZGV2?=,
Please quit
telling developers that they cannot use 2 different datasources, I am doing
it.
I never, ever told you that. Never. I've known it's possible for years - see
my website if you don't believe me :) The database field workaround has been
up there for about ten years...
It would be nice if the "Professionals" were more informed to give
revelant answers to developers. your answer was partially right to save to
another doc. The trick is the "wdNotAMergeDocument" property.
I am informed - you are not. Mail merge was not *designed* to be used in the
manner in which you are using it. It was *designed* to lock down the data on
performing the merge (for you, that means the Execute method - to a new
document). And even if you set the property you mention, that will not remove
or lock the merge fields in the document. Depending on circumstances, the
data they're *displaying* could very well be lost. I speak from fifteen years
experience with mail merge and supporting the same :)

The only *reliable* ways to preserve a document with that data is one of the
three I mentioned: merge to a new document, lock down the merge fields, or
unlink them.

If you decide to stick to what you've got right now, let's just hope you
don't come to regret it the next time something changes in how Word performs
in the UI. (Because that's what "broke" your application, not the object
model.)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

foxdev

I agree that Word2007 may not have been designed to work the way I use it but
it works.
An issue has came up that the process will not save the data as it
did...yes you may be saying "I told you so". One pc works as expected,
another does not (it did). When saving the file as a different name, I see
merge fields, not the data. Why would this process work on 1 pc and not
another? They have the same major/minor version. Could it be a Word
configuration issue?

If you could answer based on my code in a previous reply, I would appreciate
it.

foxdev
 
C

Cindy M.

Hi =?Utf-8?B?Zm94ZGV2?=,
An issue has came up that the process will not save the data as it
did...yes you may be saying "I told you so". One pc works as expected,
another does not (it did). When saving the file as a different name, I see
merge fields, not the data. Why would this process work on 1 pc and not
another?
Actually, I would say: if you'd do it my way you'd never see this...

Doing it your way: what you're seeing are the field codes. Everything a Word
document does dynamically is maintained by field codes. When Word actually
executes the merge to a new document it removes the field codes (making the
result truly static). The display of field codes vs. result can be toggled in
the UI via the "Word Options" or by pressing Alt+F9. This setting is at the
window level - presumably, the user has turned it on for a reason. (Of
course, this is not always the case.) By "window level" I mean that whatever
setting was valid for the most recent active window will apply to the next
document window opened in the application.

And yes, there is an object model equivalent: View.ShowFieldCodes

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
F

foxdev

The execute does not remove the field codes. They are still there when
viewing the document after saving. The document is use multiple times for
different data. I am aware of "How to toggle field codes." <b>The user never
has access to this document</b>, only to generate and print it. Where in
Word2007 Options is it "turned on/off?", I don not see it. How can that be a
difference if I am controlling the doc programmically?

I have tried "wdNotAMergeDocument ", "fields.unlink", and nothing works on
any other pc in my office except mine. There must be a KB installed no one
else has or I changed a configuration in Word unknowingly. Any ideas?

Please look at the code in the previous post and let me know how to
disconnect the fieldcode leaving the doc static.
 
F

foxdev

I may need to clarify what is happening. The process prints 2 documents with
the correct data, its when I save the last document that only the text
"<<fieldcode>>" is printed not the value. This works as expected on my pc but
NOT on any other pc. I do not know what KB or setting can be on my pc and not
the others. Office is suppose to be the same on all.
 
C

Cindy M.

Hi =?Utf-8?B?Zm94ZGV2?=,
The execute does not remove the field codes.
It does if you send the merge to a new document, but not if you send it to the
printer, which is apparently what you're doing:
oDoc.MailMerge.Destination = 1 && send doc to printer when executed

When you send it to a new document Word creates a copy of the main merge
document. That copy does not contain the merge fields.
I may need to clarify what is happening. The process prints 2 documents with
the correct data, its when I save the last document that only the text
"<<fieldcode>>" is printed not the value.
Ah, that's something different. By field code I mean: { Mergefield fieldname }.

What you're seeing is the field placeholder Word shows when "Preview" is not
activated, or when no data is available for the main merge document and the
merge fields have been forced to update.

I can't recall having ever encountered what you describe, but I suppose that's
not surprising, given that I'd not use your approach.

My best guess would be that there is, indeed, no data available at the point
where this is happening, and the fields are being forced to update. You can see
if changing the "Update fields before printing" option in the Display tab of
the Word Options changes what you're seeing.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
F

foxdev

The process works fine for printing, it is the saveas that displays the
"<<fieldcode>>" in the places I have mergefields. Maybe because I use
MainDocumentType = -1 (wdNotAMergeDocument). After printing I want to save
the document with the values and without the data link, like a static doc
without a datasource. I have even tried "datasource.close". Thanks for
sticking with me.

Foxdev
 
C

Cindy M.

Hi =?Utf-8?B?Zm94ZGV2?=,
The process works fine for printing, it is the saveas that displays the
"<<fieldcode>>" in the places I have mergefields. Maybe because I use
MainDocumentType = -1 (wdNotAMergeDocument).
Yes, this would be exactly why. The document doesn't have any data to link
to, so Word has to show the default field content (the placeholders) as
soon as it receives the command to update the fields in the document. I
don't know why SaveAs would be doing that on some machines, but not
others (yours).

You have to leave it linked to the data, lock the fields, or unlink the
fields.

Or do as I advised originally: merge to a new document, print and save
that.

This is how Word's mail merge is designed to work.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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