C#VSTO字AddIn Throws值不在预期的错误范围内



我是VSTO单词Addin的新手,最终目标是检查自定义文档属性是否存在。在线阅读所有可用的文章,没有突破。

开始于此代码

public void chk()
{
if (this.Application.ActiveDocument.CustomDocumentProperties["ObjectType"].Value = 0)
{
MessageBox.Show("Please select an object");
}
}

这抛出错误

值不在的范围内

修改代码以创建如下自定义文档属性对象,它在上给了我相同的错误

if(this.Application.ActiveDocument.CustomDocumentProperties[quot;ObjectType"].Value=0(

public void chk()
{
dynamic properties = null;
properties = this.Application.ActiveDocument.CustomDocumentProperties;
properties.Add("Name",false,Office.MsoDocProperties.msoPropertyTypeString, "ObjectType");
properties.Add("LinkToContent",false, Office.MsoDocProperties.msoPropertyTypeBoolean,false);
properties.Add("Type",false,Office.MsoDocProperties.msoPropertyTypeNumber, 0);

if(this.Application.ActiveDocument.CustomDocumentProperties["ObjectType"].Value = 0)
{
MessageBox.Show("Please select an object");
}
}

请求您建议解决问题的步骤。如果需要更多信息,请告诉我。

您可以迭代所有属性并检查它们的名称,比较两种方法:

void TestProperties()
{
Microsoft.Office.Core.DocumentProperties properties;
properties = (Office.DocumentProperties)this.CustomDocumentProperties;
if (ReadDocumentProperty("Project Name") != null)
{
properties["Project Name"].Delete();
}
properties.Add("Project Name", false,
Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
"White Papers");
}
private string ReadDocumentProperty(string propertyName)
{
Office.DocumentProperties properties;
properties = (Office.DocumentProperties)this.CustomDocumentProperties;
foreach (Office.DocumentProperty prop in properties)
{
if (prop.Name == propertyName)
{
return prop.Value.ToString();
}
}
return null;
}

相关内容

  • 没有找到相关文章

最新更新