在asp.net中使用邮件合并 [英] Using mail merge in asp.net

查看:107
本文介绍了在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 









已添加标签。

[/编辑]

解决方案

1.在Visual Studio中检查项目的目标平台为 Win32

Win64 基于您的测试系统!



2.因此您必须修改项目属性 - >构建tab - >平台目标与您的测试机器(在您的Win64中)相同,然后重建并重新安装。

对于此错误

由于以下错误,检索具有CLSID {000209FF-0000-0000-C000-000000000046}的组件的COM类工厂失败:80040154。



似乎你的COM组件没有安装,你需要使用Regsvr-32 -i文件路径注册它(在RUN程序中 - Window + R)或服务器的命令提示符。


您必须在运行应用程序的服务器上安装 Microsoft Word

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]

解决方案

1.Check in Visual Studio the target platform of your project to be Win32 or
Win64 based on your testing system!

2.So you have to modify the "project properties -> Build tab -> Platform target" to be the same like your test machine (in your case Win64), then rebuild and reinstall.


For this Error
"Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154."

Seems your COM component is not installed and you need to register it using Regsvr-32 -i "path of the file" (in the RUN program - Window + R) or command prompt of the server.


You have to have Microsoft Word installed on the Server running your Application.


这篇关于在asp.net中使用邮件合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆