【问题标题】:Producing Word document with windows form doesn't work for all PCs: System.Runtime.InteropServices.COMException (0x800A16CA)使用 windows 窗体生成 Word 文档不适用于所有 PC:System.Runtime.InteropServices.COMException (0x800A16CA)
【发布时间】:2020-11-24 12:50:52
【问题描述】:

我开发了一个 Windows 窗体应用程序来生成 Word 报告。一开始,它在所有生成出色文档的 PC 上运行良好,在我添加以下代码块后,它在某些 PC 上执行时停止生成报告。

object end = 0;
object start = 0;
string title = Regex.Replace(tab.Cell(1, 1).Range.Text, pattern, dati[7]);
Range rng = doc.Range(ref start, ref end);
rng.Text = title;
Object styleHeading2 = "Heading 2";
rng.set_Style(ref styleHeading2);
rng.Paragraphs.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel2;
rng.Paragraphs.SpaceAfter = 10;

我发现,在它不起作用的 PC 上,代码会引发以下异常: System.Runtime.InteropServices.COMException (0x800A16CA):具有指定名称的项目不存在。 在 Microsoft.office.Interop.Word.Range.set_style(Object& prop)

你有什么办法解决吗?

谢谢

【问题讨论】:

  • 你有async方法吗?
  • @karritos 不,我没有

标签: c# runtime production-environment word winforms-interop


【解决方案1】:

问题在于 Word 的语言。在我的电脑上 Word 是英文的,但我的同事有母语 Word,因此他们不懂命令:

 Object styleHeading2 = "Heading 2";

解决方法是使用 Word.WdBuiltinStyle 枚举设置样式,本例中:

Object styleHeading2 = WdBuiltinStyle.wdStyleHeading2;

【讨论】:

  • 太好了,也许这会在不久的将来帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
相关资源
最近更新 更多