尝试从FirebirdDB邮寄到Word但无法打开数据源

时间:2016-09-28 10:35:28

标签: c# ms-word connection-string firebird mailmerge

我试图将来自firebird数据库的数据邮寄到word文档中。我有一张桌子" USERS"使用字段," NAME"," ADDRESS1"和" CITY"。

但是我无法打开数据源。下面一行,

        wrdDoc.MailMerge.OpenDataSource(myConnection1.Database, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);

抛出:未处理的类型&System; Run.Runtime.InteropServices.COMException'发生在ProtoLAS.exe

其他信息:Word无法打开数据源。

我觉得我已经尝试了一切。您可以在下面找到完整的表单代码。

using FirebirdSql.Data.FirebirdClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace ProtoLAS
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {
        string connectionString =
            "User=SYSDBA;" +
            "Password=masterkey;" +
            "Database=E:\\data\\LAS.FDB;" +
            "DataSource=localhost;" +
            "Port=3050;" +
            "Dialect=3;" +
            "Charset=NONE;" +
            "Role=;" +
            "Connection lifetime=15;" +
            "Pooling=true;" +
            "MinPoolSize=0;" +
            "MaxPoolSize=50;" +
            "Packet Size=8192;" +
            "ServerType=0";
        FbConnection myConnection1 = new FbConnection(connectionString);
        try
        {
            // Open connection.
            myConnection1.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Word.Application wrdApp;
        Word._Document wrdDoc;
        Object oTemplate = "E:\\Template.docx";
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
        Object oTrue = true;
        Word.MailMerge wrdMailMerge;

        // Create an instance of Word  and make it visible.
        wrdApp = new Word.Application();
        wrdApp.Visible = true;

        // Create MailMerge Data.                        
        wrdDoc = wrdApp.Documents.Open(ref oTemplate, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        wrdDoc.Select();
        wrdMailMerge = wrdDoc.MailMerge;
        object oQuery = "SELECT NAME,ADDRESS1,CITY from USERS";
        wrdDoc.MailMerge.OpenDataSource(myConnection1.Database, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);
        wrdMailMerge.SuppressBlankLines = true;

        // Perform mail merge.
        wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
        wrdMailMerge.Execute(ref oFalse);

        // Close the Template document.
        wrdDoc.Saved = true;
        wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        // Release References.            
        wrdMailMerge = null;
        wrdDoc = null;
        wrdApp = null;
        myConnection1.Close();
    }

为什么Word不会打开数据源?

编辑:Stacktrace

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2146822366
  HResult=-2146822366
  HelpLink=wdmain11.chm#25402
  Message=Word was unable to open the data source.
  Source=Microsoft Word
  StackTrace:
       at Microsoft.Office.Interop.Word.MailMerge.OpenDataSource(String Name, Object& Format, Object& ConfirmConversions, Object& ReadOnly, Object& LinkToSource, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Connection, Object& SQLStatement, Object& SQLStatement1, Object& OpenExclusive, Object& SubType)
       at ProtoLAS.Main.button1_Click(Object sender, EventArgs e) in C:\Users\nathan\Source\Repos\PrototypeLAS\ProtoLAS\ProtoLAS\Main.cs:line 64
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at ProtoLAS.Program.Main() in C:\Users\nathan\Source\Repos\PrototypeLAS\ProtoLAS\ProtoLAS\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

1 个答案:

答案 0 :(得分:-2)

如果你能负担得起在第三方产品上花一些钱的话,我会推荐两款让你的生活更轻松的产品:

  1. LLBLGen Pro是一个ORM工具,用于为Firebird(或许多其他数据库)创建数据访问层,这使得在C#代码中操作数据非常容易。
  2. Docentric Toolkit是一个工具包,用于将MS Word模板与数据合并,以docx,pdf或xps格式创建最终报告。您在MS Word中设计模板并将其与C#应用程序中的数据合并。它也可以在服务器上运行,并且不需要在该服务器上安装MS Office。
  3. 我非常有效地使用这两种产品。