[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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

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
wademi
Asked:
wademi
  • 6
  • 6
1 Solution
 
GrahamSkanCommented:
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
 
wademiAuthor Commented:
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
 
GrahamSkanCommented:
I am not clear about what happens when you execute the mailmerge after opening the document. Is the data still wrong?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
wademiAuthor Commented:
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
 
wademiAuthor Commented:
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
 
GrahamSkanCommented:
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
 
wademiAuthor Commented:
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
 
GrahamSkanCommented:
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
 
wademiAuthor Commented:
Leave it opened
0
 
GrahamSkanCommented:
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
 
wademiAuthor Commented:
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
 
GrahamSkanCommented:
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now