gpt4 book ai didi

c# - 程序在 oWord.Documents.Open(oTemplatePath) 处卡住?

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:30 26 4
gpt4 key购买 nike

我整理了一个简单的 C# Winforms 应用程序,它根据用户条件查询 AS400 数据并将结果返回到 ListView 控件中。在 Button_Click() 上,然后我将标题和数据存储在 .txt 文件中。下面我尝试使用该 .txt 文件作为邮件合并 word 文档的数据源。

当代码到达 oWrdDoc = oWord.Documents.Open(oTemplatePath); 时,程序似乎只是卡住了。什么都没有发生,我无法进入下一行。有人知道我做错了什么吗?

public void Print(string docLoc, string docSource)
{
try
{
Word.Application oWord = new Word.Application();
Word.Document oWrdDoc = new Word.Document();
oWord.Visible = true;
Object oTemplatePath = "C:\\Users\NAME\\Desktop\\B-AIAddChgDual10-06-NEW.doc";
oWrdDoc = oWord.Documents.Open(oTemplatePath);
Object oMissing = System.Reflection.Missing.Value;
oWrdDoc.MailMerge.OpenDataSource("C:\\Users\\NAME\\Desktop\\Test2.txt", oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
oWrdDoc.MailMerge.Execute();
}
catch (Exception ex)
{
MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data);
}
finally
{
//
}
}

编辑:原来我没有将新的 Word 实例设置为 Visible = true 所以当实例弹出一个对话框说文件被锁定以供编辑时 ( 由我自己???)它提示我打开一个只读版本,之前我看不到它,看起来一切都在处理中被卡住。我修改了上面的代码以反射(reflect)更改。

关于为什么我被锁定在我自己的文件之外以及如何防止这种情况的任何想法?

这些是我接受打开只读文档后收到的对话框(按顺序):

Reult Dialog1 Dialog2 Dialog3 Dialog4 Dialog5 Dialog6 Dialog7

选择如何替换上面的字段后:

Merge Errors

原始邮件合并字段:

enter image description here

个人选择之后:

enter image description here

如何让 Word 应用程序使用“!”字符作为我的 C# 代码中的字段分隔符?

此外,我该如何继续对话?由于我的数据源不包含与列为邮件合并字段的字段相匹配的字段,我假设我收到了每一个?

这是我的邮件合并字段:

-fuldate-系统-内存号-姓名-地址1-地址2-地址3-萨尔

这是我的 .txt 数据源文件中的分隔字段:

memno!name!addr1!addr2!city!state!zip!old_addr1!old_addr2!old_city!old_state!old_zip

最佳答案

您可以将OpenAndRepair 模式设置为True 然后打开文档

替换这个:

oWrdDoc = oWord.Documents.Open(oTemplatePath);

具有以下内容:

oWrdDoc =  word.Documents.Open(oTemplatePath, OpenAndRepair: true);

关于c# - 程序在 oWord.Documents.Open(oTemplatePath) 处卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19984708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com