gpt4 book ai didi

c# - 如何从 winforms .exe 应用程序中打开 Microsoft Word 文档?

转载 作者:行者123 更新时间:2023-11-30 21:59:41 26 4
gpt4 key购买 nike

我有一个 Winforms 应用程序,它利用 dll (docX) 从 StringBuilder 创建 .docx 文档。我正在尝试通过单击按钮使用 Microsoft Word(默认程序)打开该文档。我尝试了以下代码,但我不断收到错误。有人可以指出正确的方向来完成这项工作吗?

private void button3_Click(object sender, EventArgs e)
{
var x = "";
using (DocX document = DocX.Create("Testdocx.docx"))
{
document.MarginTop = 25f;
document.MarginBottom = 25f;
document.MarginLeft = 25f;
document.MarginRight = 25f;
Paragraph p = document.InsertParagraph();
FontFamily fontFamily = new FontFamily("Courier New");

p.Append(sb.ToString()).Font(fontFamily).FontSize(8); //where "sb" is a StringBuilder
document.Save();
x = Environment.CurrentDirectory;
}
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE";
startInfo.Arguments = x + "\\Testdocx.docx";
startInfo.UseShellExecute = true;
Process.Start(startInfo);
}

最佳答案

您的方法硬编码了 WINWORD 的路径。虽然这可能适用于您的情况,但它不够灵活且脆弱。

你可以简单地做

Process.Start(x + "\\Testdocx.docx");

这将找到 .docx 文件的默认文档处理程序(这是 Winword,假设它已安装并且您没有安装任何其他处理 .docx 文件的东西)。

关于c# - 如何从 winforms .exe 应用程序中打开 Microsoft Word 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29091235/

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