如何使用数据库访问自动化单词?

时间:2011-04-25 15:46:02

标签: winforms automation ms-word

这就是我想要做的事情:

  1. 打开一些表格(也许我用vb或c#或其他东西写),输入一些信息。
  2. 表单然后获取该信息,使用它从数据库中获取更多信息。
  3. 将所有信息放入我在word文档中排列的“字段”中(使用我可以为某些字段指定的字符串比较逻辑)。
  4. 打印word文档。
  5. 获得此类功能的最佳方法是什么?

    答案:以下是@ Smoore的回答,其中包含一些代码模型。

    object fileName = Application.StartupPath + "\\" + fileIdent + ".doc";
    object readOnly = 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;
    

    ....然后插入所有书签后:

    //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);
    

    注意:您必须封送应用和文档,否则winword.exe进程将继续运行。

1 个答案:

答案 0 :(得分:0)

在VB或C#应用程序中,您可以轻松地将数据插入Word书签并使用Microsoft.Office.Interop.Word namespace进行打印。我认为建议使用书签而不是字段。关于字段,MSDN声明:“此属性支持.NET Framework基础结构,不能直接在您的代码中使用。”

<强> 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();