2016-09-28 74 views
0

我試圖從firebird數據庫將數據郵寄到Word文檔。我有一個「USERS」表,包含「NAME」,「ADDRESS1」和「CITY」字段。試圖從FirebirdDB到Word的mailmerge,但無法打開數據源

但是我無法打開數據源的單詞。下面的線,

 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.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不能打開數據源?

編輯:堆棧跟蹤

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

從連接字符串中刪除'Role =;'(在指定空角色而不是** no **角色時,我在其他驅動程序中看到過問題)。如果這不能解決它,那麼請發佈完整的例外情況。 –

+0

刪除角色,仍然相同。 Stacktrace添加到帖子。 – A1raa

回答

-1

如果你能負擔得起的第三方產品花了一些錢,我就推薦兩款產品,這將使你的生活變得更輕鬆:

  1. LLBLGEN Pro是一個ORM爲Firebird(或許多其他數據庫)創建數據訪問層的工具,這使得在C#代碼中處理數據變得非常簡單
  2. Docentric Toolkit是一個工具包,用於合併MS Word模板和數據以創建最終的r以docx,pdf或xps格式進行eports。您可以在MS Word中設計模板並將其與C#應用程序中的數據合併。它也可以在服務器上運行,並且不需要在該服務器上安裝MS Office。

我正在非常有效地使用這兩種產品。