本文介绍了在asp.net中使用邮件合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的邮件合并代码
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false ;
wordApp.ScreenUpdating = false ; // 将加快流程
object filename = path; // 输入
/ / file to
object newFileName = @ 〜\ Quotation\Quotation\document_to_read_tomorrow1.pdf; // 输出
对象 missing = System.Type.Missing;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Add(Template:filename);
foreach (Microsoft /Office.Interop.Word.Field字段在 doc中.Fields)
{
string fieldName = GetFieldName(field.Code.Text);
if (dataToWrite.ContainsKey(fieldName))
{
field.Select();
string value = dataToWrite [fieldName] .ToString();
wordApp.Selection.TypeText( value );
}
}
// formt保存文件,本例PDF
object formatoArquivo = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
// 纸张尺寸变化
doc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;
// 更改orietation文件
doc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;
// 其他更改
// doc.PageSetup.LeftMargin = 20;
// doc.PageSetup.RightMargin = 0;
// 保存文件
doc.SaveAs( ref newFileName, ref formatoArquivo, ref 缺失, ref 缺失, ref 缺失, ref 缺失, ref 缺失,
ref 缺失, ref 缺失, ref missing, ref 缺失, ref missing, ref 缺失, ref 缺失, ref 缺失, ref 缺失);
// doc.Close(ref missing,ref missing,ref missing);
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_ Document)doc)。关闭( ref saveChanges, ref 缺失, ref 缺失);
doc = null ;
// 必须将字词强制转换为_Application类型以便它找到
// 正确的退出方法。
(( _Application)wordApp).Quit( ref 缺失, ref 缺失,参考缺失);
wordApp = null ;
返回 newFileName.ToString();
当我在我的本地系统中运行时很好但是当我在服务器中运行它抛出错误
无法加载文件或程序集 ' Microsoft.Office.Interop.Word,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c'或其依赖项之一。系统找不到指定的文件。
然后我将程序集加载到bin文件夹然后我收到错误
使用CLSID {000209FF-检索组件的类 factory 0000-0000-C000-000000000046}由于以下错误而失败: 80040154 。
已添加预
标签。
[/编辑]
解决方案
My Code for mail merge
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; wordApp.ScreenUpdating = false;//Will speed up the process object filename = path; // input // file to object newFileName = @"~\Quotation\Quotation\document_to_read_tomorrow1.pdf"; // output object missing = System.Type.Missing; Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Add(Template: filename); foreach (Microsoft.Office.Interop.Word.Field field in doc.Fields) { string fieldName =GetFieldName( field.Code.Text); if (dataToWrite.ContainsKey(fieldName)) { field.Select(); string value = dataToWrite[fieldName].ToString(); wordApp.Selection.TypeText(value); } } // formt to save the file, this case PDF object formatoArquivo = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; // changes in paper size doc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4; // changes orietation paper doc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait; // other changes //doc.PageSetup.LeftMargin = 20; //doc.PageSetup.RightMargin = 0; // save file doc.SaveAs(ref newFileName, ref formatoArquivo, 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); //doc.Close(ref missing, ref missing, ref missing); object saveChanges = WdSaveOptions.wdDoNotSaveChanges; ((_Document)doc).Close(ref saveChanges, ref missing, ref missing); doc = null; // word has to be cast to type _Application so that it will find // the correct Quit method. ((_Application)wordApp).Quit(ref missing, ref missing, ref missing); wordApp = null; return newFileName.ToString();
when i run in my local system its fine but when i run in server it throwing error
Could not load file or assembly 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
Then I loaded the assembly to bin folder then I got an error
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154.
[Edit member="Tadit"]
Added pre
tags.
[/Edit]
解决方案
这篇关于在asp.net中使用邮件合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!