c# Interop.Word 在不保存到磁盘的情况下连接两个 WordDocument

标签 c# merge office-interop mailmerge

我已经可以从文件路径加入两个或多个 word 文档。

如何在不将它们保存到磁盘的情况下加入它们?

string TemporaryPath = "";
string TemplatePath = "";
Object MissingValue;
object OFalse = false;
object OTrue = true;

public WordWorker()
{
    TemporaryPath = AppDomain.CurrentDomain.BaseDirectory + "Temporary";
    MissingValue = Missing.Value;
    TemplatePath = @"C:\TemplateTest.dotx";
}
public void Merge()
{
    object wdPageBreak = 7;
    object wdStory = 6;


    Application WordApplication = new Application();
    Document WordDocument = WordApplication.Documents.Add(ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue);

    for (int i = 0; i < 100; i++)
    {
        string Path = CreateWordFromTemplate(WordApplication, TemplatePath, new Dictionary<string, string>() { { "Name", "MyNewValue" + i } });
        WordDocument.Application.Selection.Range.InsertFile(Path, ref MissingValue, ref MissingValue, ref MissingValue, ref OFalse);
    }

    object WordSaveFormat = WdSaveFormat.wdFormatDocument;
    object MergedDocumentPath = TemporaryPath + "Merged.dox";
    WordDocument.SaveAs(ref MergedDocumentPath, ref WordSaveFormat, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue);
    WordApplication.Application.Quit();
}
public string CreateWordFromTemplate(Application WordApplication, Object oTemplatePath, Dictionary<string, string> dictionary)
{
    Guid DocumentID = Guid.NewGuid();

    Document WordDocument = new Document();

    WordDocument = WordApplication.Documents.Add(ref oTemplatePath, ref MissingValue, ref MissingValue, ref MissingValue);

    foreach (Field FieldToMerge in WordDocument.Fields)
    {
        Range FieldRange = FieldToMerge.Code;
        String FieldText = FieldRange.Text;

        if (FieldText.StartsWith(" MERGEFIELD"))
        {
            Int32 EndMerge = FieldText.IndexOf("\\");
            Int32 FieldNameLength = FieldText.Length - EndMerge;
            String FieldName = FieldText.Substring(11, EndMerge - 11).Trim();

            if (dictionary.ContainsKey(FieldName))
            {
                FieldToMerge.Select();
                WordApplication.Selection.TypeText(dictionary[FieldName]);
            }
        }
    }
    string Path = @"{0}\{1}.doc".SFormat(TemporaryPath, DocumentID);

    WordDocument.SaveAs(Path);
    WordDocument.Close();
    return Path;
}

最佳答案

使用这些不需要 Office COM 的库之一,结果直接与内存或其他地方关联的二进制数据一起工作。

https://www.codeplex.com/site/search?tagname=office&projectsearchtext=%22office%22

然后假设,使用 MemoryStream,您将能够以您想要的格式创建您想要的文档,然后用您想要的 Stream 做任何事情。

v/

关于c# Interop.Word 在不保存到磁盘的情况下连接两个 WordDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23597341/

相关文章:

c# - Interop 字进程不会立即关闭

c# - BigInteger 不能表示无穷大

基于第一列合并行的 Python 脚本

git - 使用git日志显示 merge 期间更改的文件

python - 使用 Python 对 jpeg 图像进行排序和合并?

c# - 将互操作颜色转换为 System.Drawing.Color

c# - 在 ASP.NET MVC4 中从登录 View 重定向到 ChangePassword View

c# - 如何确定有害队列消息的原因

c# - 显示器特定区域的像素搜索

.net - 在 VS2012 中找不到 Microsoft.Office.Interop 程序集