我正在使用Office加载项。我遇到了问题。我想使用c#和VSTO将word文档作为链接对象添加到另一个word文档中。我深入研究并发现,为此我必须使用" INCLUDETEXT" 字段。在VSTO中,InsertFile函数有一个名为" Link" 的参数,如果此参数设置为 true ,则指定的word文档将作为链接对象插入。 / p>
这是我的代码:
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(FileName,range,ConfirmConversions,Link,Attachment);
在编辑器或编译过程中没有显示任何错误,但是当我在办公室中使用我的加载项时,它会在此行中出错,并说"命令失败"
这是错误截图
这是错误的StackTrace
但是,当我只是使用这一行时,它不会出错,而是插入文件但不作为链接对象。
Globals.ThisAddIn.Application.Selection.InsertFile(文件名)
问题出在哪里?还告诉我你是否有更好的想法将链接对象(Word文档)插入到其他文档中。 ?
答案 0 :(得分:0)
注意:上述代码中没有错误。问题在于我的word文件中没有要链接的书签对象。我创建了一个名为“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.
请参阅参数范围如果我们要链接的文件是“Word文档”,那么应该在该文件中创建一个带有名称的书签。您必须在Range对象中传递此书签名称。喜欢
object Range = "hashim";
此处 hashim 是链接到其他文档文件的文件中的书签名称。
如果未指定书签,则必须提供错误。
现在,如果链接对象是 Excel文档,则范围对象必须包含像
这样的单元格信息object Range = "R1C1:R3C4";
此处“R1C1:R3C4”是我们要在文档中链接的excel中的单元格范围。
注意:我们可以添加完整或部分excel或word文档。请参阅Range Object文档。
希望你的回答很清楚。 谢谢