C# 试图将FirebirdDB中的邮件合并到Word中,但无法打开数据源
我正在尝试将firebird数据库中的数据邮件合并到word文档中。我有一个表“USERS”,其中包含字段“NAME”、“ADDRESS1”和“CITY” 但是,我无法让word打开数据源。下面的一行C# 试图将FirebirdDB中的邮件合并到Word中,但无法打开数据源,c#,ms-word,connection-string,firebird,mailmerge,C#,Ms Word,Connection String,Firebird,Mailmerge,我正在尝试将firebird数据库中的数据邮件合并到word文档中。我有一个表“USERS”,其中包含字段“NAME”、“ADDRESS1”和“CITY” 但是,我无法让word打开数据源。下面的一行 wrdDoc.MailMerge.OpenDataSource(myConnection1.Database, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref
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);
正在引发:ProtoLAS.exe中发生类型为“System.Runtime.InteropServices.COMException”的未处理异常
其他信息: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:
如果你能负担得起在第三方产品上花费一些钱,我会推荐两种让你的生活更轻松的产品:
我正在非常有效地使用这两种产品。删除
角色=代码>来自连接字符串(我看到其他驱动程序在指定空角色而不是无角色时出现问题)。如果这不能解决问题,那么请发布异常的完整stacktrace。删除角色,仍然获得相同的结果。Stacktrace已添加到post。