Solved

Microsoft Word merge fields do not refresh when launched from a C#.net application

Posted on 2008-10-05
16
401 Views
Last Modified: 2013-12-17
I have a Microsoft word document with some merge fields from an Access database.
When I launch the word document from my C#.net application using:
Process.Start(@"C:\Ama\Templates\NILM.doc");
The Word document opens but the fields shows the information from an old merged document.
The fields in the word document does not show the data from the Access database.

The table I am pulling the data from only has one row of record at a time.
However if I manualy open the document by clicking on it by going to C:\Ama\Templates\NILM.doc
The document opens populating the merge fields with the most current data from the access database.
It appears that when I launch the document from my application it's openining a document stored in memory

PLEASE HELP
0
Comment
Question by:wademi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
16 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22646630
The user interface sometimes does in one step things that take several programme instructions. Does the old information still show after you have previewed or executed the merge?
0
 

Author Comment

by:wademi
ID: 22651555
Yes. If I open the merge document by clicking on the actual docuemnt it opens with the correct data from the database. If I go back in the application then launch the merge document again it still shows me old information that is not in my database.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22652132
I am not clear about what happens when you execute the mailmerge after opening the document. Is the data still wrong?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:wademi
ID: 22652850
Yes the data is still wrong afer I open the document from the application. But its correct 100% of the time when I open the document outside of the application
0
 

Author Comment

by:wademi
ID: 22653089
Just to make things clearer. The document automatically merge when I open It. So if I open it manually the docuemnt shows the merged data from the database. If I launch it from the application it shows me the merged result from a random merge I performed a long time ago. There is only one merge Doc and source file. It seems like the docuemnts are stored in memory.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22654369
That doesn't fit in with the way that I understand mail merge to work. You create a mail merge main document which links to a datasource and has Mail Merge fields at the places where the variable data is to be displayed. As a help in setting up you can preview the data directly in the main document. When the design is finished, you then run the merge to produce an output to a new result document - or to email, fax or a printer. If the choice is to produce a new document, the result document has no bookmarks and most of the fields, including the merge fileds have not been transfered. Only the resulting text is in the new document.

If you save the main document, you can reopen it at any later time and run the merge again, even if the data in the datasource has changed.
0
 

Author Comment

by:wademi
ID: 22656123
When I created the document I included th merge fields then ran the merge to produce the new result document. I then saved the new result document. So if I change the data in the database then open the new result document it automatically shows the current data that is beign pulled from the database. Its this new result document that I launch from the aplication
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22689925
The result document has no reference back to the datasource. There are no merge fields.
If you want to use the new data, you will have to run the mail merge again to produce a new result document. Programatically you will have to use the Execute method of the main document's MailMerge property.
0
 

Author Comment

by:wademi
ID: 22891588
Leave it opened
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22911567
wademi,

This question is now a long way down the list of unanswered questions, and it is unlikely that anyone else will stumble across it and attempt an answer. I believe that there are links around open questions that are visible to the question owner that allow the moderators to post pointer questions requesting further help.

Meanwhile, I am not sure how we have got. Did you take on board what I was saying in my last comment? If I have misunderstood what you are are expecting to happen, can you put me right, please?

Cheers, Graham

0
 

Author Comment

by:wademi
ID: 22916971
I am a C#.net beginner so I just needed help how to Programmatically use the Execute method of the main document's MailMerge property. No one seem to be able to explain to me whats the difference between launching the word document from the application and opening it manually.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 22917192
I am not even a C#beginner, but I do understand the Word Object model. It wasn't clear to me that that was what you needed.

The snippet shows the VB code for opening a main document that has already been set up, so needs no modification.

There should be no substantive difference between manual and programmatic opening, but if you may not be able to respond to any warning messages.




Sub DoMerge()
    Dim wdApp As Word.Application
    Dim wdMainDoc As Word.Document
    Dim wdResultDoc As Word.Document
    
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = False
    Set wdMainDoc = wdApp.Documents.Open("C:\MyFolder\MyMain.doc")
    wdMainDoc.MailMerge.Destination = wdSendToNewDocument
    wdMainDoc.MailMerge.Execute
    Set wdResultDoc = wdApp.ActiveDocument
    wdMainDoc.Close wdDoNotSaveChanges
    wdResultDoc.SaveAs "C:\MyFolder\MyResult.doc"
    wdMainDoc.Close wdDoNotSaveChanges
    wdApp.Quit
End Sub

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question