Selection.InsertFile C# 错误

Selection.InsertFile C# Error

我正在处理 Office 加载项。我陷入了一个问题。我想使用 c# 和 VSTO 将 word 文档作为链接对象添加到另一个 word 文档中。我深入研究并发现为此目的我必须使用 "INCLUDETEXT" 字段。在 VSTO InsertFile 函数中有一个名为 "Link" 的参数,如果此参数设置为 true 则指定的 word 文档将作为链接插入对象。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using System.Drawing;

namespace WordAddIn1
{
    public partial class MyRibbon
    {
        string txt = "";
        bool hhh = false;
        string file_name = "";
        string file_path = "";
        DataObject o;
        string cmp="";

        private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            checkBox1.Checked = false;
            o = (DataObject)Clipboard.GetDataObject();

        }

        private void checkBox1_Click(object sender, RibbonControlEventArgs e)
        {

            if (checkBox1.Checked == true && (o.ContainsText()||o.ContainsImage()))
            {


                txt = Globals.ThisAddIn.Application.Selection.Text.Trim();
                file_name = Globals.ThisAddIn.Application.ActiveDocument.Name;
                file_path = Globals.ThisAddIn.Application.ActiveDocument.Path;
                cmp = file_path + "\" + file_name;
                hhh = txt.Length > 0;

                if (hhh)
                {

                    Console.Beep();

                }
            }

            else
            {

                if (o.ContainsText() || o.ContainsImage())
                {
                    string FileName = "C:\final.docx";
                    object range = "hashim";

                     object ConfirmConversions = false;
                     object Link = true;
                     object Attachment = false;


                     Globals.ThisAddIn.Application.Selection.InsertFile(FileName, range,ConfirmConversions ,Link,  Attachment);

                     Form1 frm = new Form1(file_name.ToString(),file_path.ToString());
                    frm.Show();

                }

            }

    }



    }
}

Globals.ThisAddIn.Application.Selection.InsertFile(文件名、范围、确认转换、Link、附件);

在编辑器或编译期间没有显示任何错误,但是当我在办公室使用我的加载项时,它在这一行给出错误并说 "Command Failed"

这是错误截图

这是错误的 StackTrace

但是当我简单地使用这一行时它没有给出错误并且文件被插入但不是作为链接对象。

Globals.ThisAddIn.Application.Selection.InsertFile(文件名)

问题出在哪里?如果您有更好的想法将链接对象(Word 文档)插入到其他文档中,也请告诉我。 ?

注:以上代码没有错误。问题在于我的 word 文件中没有要 linked 的书签对象。我创建了一个名称为 "hashim" 的,它非常有用。

解法:

如果我们去 InsertFile

的文档
Summary:
        //     Inserts all or part of the specified file.
        //
        // Parameters:
        //   FileName:
        //     Required String. The path and file name of the file to be inserted. If you don't
        //     specify a path, Microsoft Word assumes the file is in the current folder.
        //
        //   Range:
        //     Optional Object. If the specified file is a Word document, this parameter refers
        //     to a bookmark. If the file is another type (for example, a Microsoft Excel worksheet),
        //     this parameter refers to a named range or a cell range (for example, R1C1:R3C4).
        //
        //   ConfirmConversions:
        //     Optional Object. True to have Word prompt you to confirm conversion when inserting
        //     files in formats other than the Word Document format.
        //
        //   Link:
        //     Optional Object. True to insert the file by using an INCLUDETEXT field.
        //
        //   Attachment:
        //     Optional Object. True to insert the file as an attachment to an e-mail message.

查看参数 Range 如果我们正在 linking 的文件是 "Word Document" 那么应该有是在该文件中创建的带有名称的书签。您必须在 Range 对象中传递此书签名称。喜欢

object Range = "hashim";

此处 hashim 是文件中的书签名称,linked 到其他文档文件中。

如果没有指定书签则一定会报错

现在,如果 linking 对象是 Excel 文档,则范围对象必须包含单元格信息,例如

object Range = "R1C1:R3C4";

此处 "R1C1:R3C4" 是 excel 中的单元格范围,我们希望在文档中 link。

注意:我们可以添加完整或部分excel或word文档。请参阅 Range Object 文档。

希望您的答案很清楚。 谢谢