Solved

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

Posted on 2008-10-05
16
397 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now