关于Word邮件合并的问题,

athlon128 2008-06-18 09:43:31
Microsoft.Office.Interop.Word.Application doc = new Microsoft.Office.Interop.Word.Application();
doc.Visible = true;
Microsoft.Office.Interop.Word._Document document;
object path = @"c:\word\Main.doc";
document = doc.Documents.Open(ref path,
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, ref oMissing, ref oMissing);
document.MailMerge.OpenDataSource("c:\\word\\data.txt", 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, ref oMissing, ref oMissing);
document.MailMerge.Execute(ref oMissing);
document.Save();
object False=false;
document.Close(ref False, ref oMissing, ref oMissing);

我的目的是想合并之后保存为新的文档。上面我写的代码会改变模板,这不是我想要的。这方面的知识我很缺乏,
知道的朋友能告诉我下嘛?
...全文
244 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
athlon128 2008-06-18
  • 打赏
  • 举报
回复
还没弄出来。人工置顶下~~
ericzhangbo1982111 2008-06-18
  • 打赏
  • 举报
回复
我明白你的替换域的问题,以前我也开发过
我以前的办法就是做一个莫板也就是.dot文件
然后里面是我替换的域。
我读取以后,然后替换,生成新的文件。

Word.ApplicationClass WordApp = new Word.ApplicationClass();

// give any file name of your choice.
object fileName = "D:\\aa.dot";
object readOnly = false;
object isVisible = false;

// the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
WordApp.Visible = false;
// Make word visible, so you can see what's happening
//WordApp.Visible = true;
// Open the document that was chosen by the dialog
Word.Document aDoc = WordApp.Documents.Open(ref fileName,
ref missing, ref readOnly, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
int num = aDoc.ComputeStatistics(stat, ref missing);
System.Console.WriteLine("The number of pages in doc is {0}",
num);

foreach (Word.Field f in aDoc.Fields)
{
string code = f.Code.Text;
if (code.IndexOf("replaceFile1") > -1)//域的名字
{
f.Select();
WordApp.Selection.TypeText("asdad");//替换

}
}
//foreach (Word.Range r in aDoc.Words)
//{
// Debug.WriteLine("value:" + r.Text);

//}

System.Console.ReadLine();
object o = false;

aDoc.Close(ref o, ref missing, ref missing);
WordApp.Quit(ref o, ref missing, ref missing);

大约就是这个样子。saveas我就不写了,参数太多了
athlon128 2008-06-18
  • 打赏
  • 举报
回复
我的Main.doc有几个邮件合并的域了。
我是替换这些域
你说的。dot文件是什么?
ericzhangbo1982111 2008-06-18
  • 打赏
  • 举报
回复
这样的情况你要打开的事.dot文件的
不是.doc的文档
athlon128 2008-06-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ericzhangbo1982111 的回复:]
Microsoft.Office.Interop.Word.Application doc = new Microsoft.Office.Interop.Word.Application();
doc.Visible = true;
Microsoft.Office.Interop.Word._Document document;
object path = @"c:\word\Main.doc";
object savepath = @"c:\word\Main1.doc";
document = doc.Documents.Open(ref path,
ref oMissing, ref oMissing, ref oMissing…
[/Quote]

这样是另存为了一个文件,不过这个文件打开的时候会提示我选择是否打开数据源,把最后的
//document.Close(ref False, ref oMissing, ref oMissing);
//doc.Quit(ref False, ref oMissing, ref oMissing);//最后关闭一下,别忘记
这两句先屏蔽的话。会看到有个新的文档出来。里面没有域。仅仅是个普通的文档。我想保存的,就是这个文档。
而不是模板的拷贝。用您的方法得到的是模板的拷贝。
ericzhangbo1982111 2008-06-18
  • 打赏
  • 举报
回复
Microsoft.Office.Interop.Word.Application doc = new Microsoft.Office.Interop.Word.Application();
doc.Visible = true;
Microsoft.Office.Interop.Word._Document document;
object path = @"c:\word\Main.doc";
object savepath = @"c:\word\Main1.doc";
document = doc.Documents.Open(ref path,
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, ref oMissing, ref oMissing);
document.MailMerge.OpenDataSource("c:\\word\\data.txt", 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, ref oMissing, ref oMissing);
document.MailMerge.Execute(ref oMissing);
document.SaveAs(ref path......); //后面的参数我就不写了,太多了
object False=false;
document.Close(ref False, ref oMissing, ref oMissing);
doc.Quit(ref False, ref oMissing, ref oMissing);//最后关闭一下,别忘记
yagebu1983 2008-06-18
  • 打赏
  • 举报
回复
看不懂!!
只能帮你顶了!!
tnt3d 2008-06-18
  • 打赏
  • 举报
回复
不会,帮顶,希望牛人解决
athlon128 2008-06-18
  • 打赏
  • 举报
回复
document.MailMerge.Execute(ref oMissing);
执行完这句代码后会新建一个文档。
我需要保存这个文档,
怎么指定一个文件名保存?
athlon128 2008-06-18
  • 打赏
  • 举报
回复
WordApp.Quit(ref True, ref oMissing, ref oMissing);
把退出方法中的第一个参数用true,推出的时候就能提示让你保存合并后的文档了。
能不能在代码中就指定一个文件名让他保存呢?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧