C# with MS Word 2000 - Object Library - Not Saving Template Data

G

Guest

==================
Problem Description
==================
Following code is for
1. Load a Word Template called CustomerInfo1.dot
2. CustomerInfo1.dot is a normal Word Template with Form fields
3. After opening a document on that template, I put "2" in all the fields
for test

Problem is,
4. When I try to sav it, calling SaveAs, it just saves the "2"s and all the
other template contents, mainly text are gone.

==================
Platform
==================
-Win XP Pro
- Office 2000/ Word 2000
- Added Reference to "Microsoft Word 9.0 Object Library" in a C# Windows
Application

==================
Others
==================
Most of the MSDN samples seems to use Bookmarks. That's the way I will go
if there is no resolution

Please guide.

Appreciate that

Thanks

// =========== CODE BLOCK FOR YOUR REFERENCE STARTS ===========
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Word;

// !!! Other ASPX.CS codes !!!!

[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form1());
}


Word.Application wrdApp;
Word._Document wrdDoc;
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
Object oTrue = true;
Object oTemplate = "C:\\Projects\\AutoWord\\CustomerInfo1.dot";
Object oFileName = "C:\\Projects\\AutoWord\\CustomerInfo.doc";

private void button1_Click(object sender, System.EventArgs e)
{
try{
System.IO.File.Delete("C:\\Projects\\AutoWord\\CustomerInfo.doc");
System.IO.File.Create("C:\\Projects\\AutoWord\\CustomerInfo.doc");
}
catch(Exception ex){}

wrdApp = new Word.Application( );
wrdApp.Visible = true;
wrdDoc = wrdApp.Documents.Add(ref oTemplate, ref oMissing,ref
oMissing,ref oMissing);

foreach(Word.FormField frmField in wrdDoc.FormFields){
// Just for test I am Setting all form fields to 2
frmField.Result = "2";
}
// This save as saves only the 2s and wipes out the template content
wrdDoc.SaveAs(ref oFileName ,ref oMissing ,ref oTrue ,ref oMissing,ref
oMissing,ref oMissing,ref oMissing,ref oMissing,ref oTru,ref oTrue,ref
oMissing);
wrdDoc.Close(ref oTrue,ref oFalse,ref oMissing);
wrdDoc = null;
wrdApp = null;
}
}
// =========== CODE BLOCK FOR YOUR REFERENCE ENDS=============
 

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