开发者

How to automate word with database access?

Here's what I want to do:

  1. Open some form (maybe I have write in vb or c# or something), type in some information.
  2. Form then takes that information, uses it to get more info from database.
  3. Drops all that information into "fields" that I've arranged in a word document (Uses string comparison logics that I can specify for some fields).
  4. Prints the word document.

What is the best way to go about getting this sort of functionality?

ANSWER: What worked was @Smoore's answer below, with some code mods.

object fileName = Application.StartupPath + "\\" + fileIdent + ".doc";
object read开发者_StackOverflowOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;

// create instance of Word
Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
oWordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

// create instance of Word document
Microsoft.Office.Interop.Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref readOnly,
                                                                            ref missing, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
                                                                            ref missing, ref missing, ref missing, ref missing, ref missing);

oWordDoc.Activate();

//Set all the fields in sheet.TopLabel
object oBookMark = "TopDescription";
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = sheet.TopLabel.JobDescription;

.... Then after all bookmarks are inserted:

//Print the sheet
oWordDoc.PrintOut(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing
                        , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

System.Threading.Thread.Sleep(1000);

//Close the file
//oWordDoc.Close(ref missing, ref missing, ref missing);
                    Object doNotSave = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
oWordDoc.Close(ref doNotSave, ref missing, ref missing);
oWordApp.Quit(ref doNotSave, ref missing, ref missing);

Marshal.ReleaseComObject(oWordDoc);
Marshal.ReleaseComObject(oWordApp);

Note: You have to marshal the app and doc, otherwise winword.exe process stays running.


In a VB or C# app you can easily insert data into Word bookmarks and print it using the Microsoft.Office.Interop.Word namespace. I think it's recommended to use bookmarks instead of fields. Regarding fields, MSDN states: "This property supports the .NET Framework infrastructure and is not intended to be used directly from your code."

VB

Dim oWord As Word.Application
Dim oDoc As Word.Document
oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add(_fileName)

oDoc.Bookmarks("Bookmark1").Range.Text = _bookmark1Vals
oDoc.Bookmarks("Bookmark2").Range.Text = _bookmark2Vals
oDoc.Bookmarks("Bookmark3").Range.Text = _bookmark3Vals

oDoc.PrintOut()
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
oWord.Application.Quit()

C#

Word.Application oWord = default(Word.Application);
Word.Document oDoc = default(Word.Document);
oWord = Interaction.CreateObject("Word.Application");
oWord.Visible = false;
oDoc = oWord.Documents.Add(_fileName);

oDoc.Bookmarks("Bookmark1").Range.Text = _bookmark1Vals;
oDoc.Bookmarks("Bookmark2").Range.Text = _bookmark2Vals;
oDoc.Bookmarks("Bookmark3").Range.Text = _bookmark3Vals;

oDoc.PrintOut();
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
oWord.Application.Quit();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜