C# VSTO 字 AddIn 抛出值未落在预期错误范围内

发布于 2025-01-13 02:17:35 字数 1194 浏览 5 评论 0原文

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

从此代码开始

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

这会引发错误

值不在范围内错误

将代码修改为创建一个自定义文档属性对象,如下所示,它给了我同样的错误

if(this.Application.ActiveDocument.CustomDocumentProperties["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");
       }
}

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

I am new to VSTO word Addin,the end goal is to check if the custom document property exists.Read all the available articles online with no breakthrough.

Started of with this code

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

This throws the error

Value does not fall within bounds error

Modified the code to create a custom document property object as below and it gives me the same error on

if(this.Application.ActiveDocument.CustomDocumentProperties["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");
       }
}

Requesting you to suggest the steps to fix the issue. Please let me know if more information is required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2025-01-20 02:17:35

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

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;
}

You can iterate over all properties and check their names, compare two approaches:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文