通过使用 VSTO 创建的 MS Word 加载项在内存中打开 Word 文档

本文关键字:Word 加载项 内存 文档 MS VSTO 创建 | 更新日期: 2023-09-27 18:32:55

我一直在为Word编程加载项。我将文档作为byte[]加载到内存中。我需要在 Word 中打开它。

通过使用 VSTO 创建的 MS Word 加载项在内存中打开 Word 文档

这是不可能的。Word 无法从内存流中打开文档。您必须使用(临时)文件。

using System.IO;
public void MyWordFileReaderMethod()
{
   string filePath = @"c:'example.docx";
   var file = File.ReadAllBytes(filePath);
}

对象file将包含您需要的内容。

编辑 1

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "WINWORD.EXE";
    startInfo.Arguments = @"c:'tempfile.docx";
    Process.Start(startInfo);

如果您需要使用 word 启动内存文件,则只能将其放入临时文件中并使用上面的代码。 如果我没记错的话,2 个进程无法跨进程边界共享数据。

如果您有一个存储在HDD上的文件,并且想在MS Word中打开它,那么为什么要将其放入内存中。您可以使用进程类直接打开它

Process wordDoc = new Process();
wordDoc.FileName = @"c:'example.docx";
wordDoc.Start();