excel 工作簿的自定义文档属性设置异常
Exception on settings Custom document properties of excel workbook
我正在开发 excel VSTO 加载项,在该加载项中,我构建了一些工作表以便稍后处理它们。我想稍后知道当用户打开 excel 工作簿时,该工作簿是否使用我的加载项构建或不对其进行一些处理。为此我尝试使用 CustomDocumentProperties.
我面临两个问题:
- visual studio 调试器不计算 DocumentProperties 变量的子项,我无法检查它们。
- 当我尝试通过调用 DocumentProperties.Add 创建一个新的 DocumentPropertiy 时,我收到一个 ArgumentException,指出该值不在范围内。
构造工作簿调用的函数:
public void InitWorkbook()
{
workingBook = Globals.Factory.GetVstoObject(Application.ActiveWorkbook);
var dps = (DocumentProperties)workingBook.CustomDocumentProperties;
if (!customDocumentPropertyExist("validctcwb", dps))
{
// some sheet creation and listobject creation
dps.Add("validctcwb", false);
}
}
bool customDocumentPropertyExist(string name, DocumentProperties dps)
{
foreach (DocumentProperty p in dps)
{
if (p.Name == name)
{
return true;
}
}
return false;
}
提前感谢您的宝贵帮助。
我在我的插件中做同样的事情来生成一个唯一的 ID。
var propertyName = "validctcwb";
var propertyValue = false;
var propertyType = MsoDocProperties.msoPropertyTypeBoolean;
dps.Add(propertyName, false, propertyType, propertyValue);
如果想在调试的时候看到数值,可以引入以下方法:
public static IEnumerable<CustomProperty> GetCustomDocumentProperties(Workbook workbook)
{
foreach (CustomProperty property in workbook.CustomDocumentProperties)
{
yield return property;
}
}
瞧
我正在开发 excel VSTO 加载项,在该加载项中,我构建了一些工作表以便稍后处理它们。我想稍后知道当用户打开 excel 工作簿时,该工作簿是否使用我的加载项构建或不对其进行一些处理。为此我尝试使用 CustomDocumentProperties.
我面临两个问题:
- visual studio 调试器不计算 DocumentProperties 变量的子项,我无法检查它们。
- 当我尝试通过调用 DocumentProperties.Add 创建一个新的 DocumentPropertiy 时,我收到一个 ArgumentException,指出该值不在范围内。
构造工作簿调用的函数:
public void InitWorkbook()
{
workingBook = Globals.Factory.GetVstoObject(Application.ActiveWorkbook);
var dps = (DocumentProperties)workingBook.CustomDocumentProperties;
if (!customDocumentPropertyExist("validctcwb", dps))
{
// some sheet creation and listobject creation
dps.Add("validctcwb", false);
}
}
bool customDocumentPropertyExist(string name, DocumentProperties dps)
{
foreach (DocumentProperty p in dps)
{
if (p.Name == name)
{
return true;
}
}
return false;
}
提前感谢您的宝贵帮助。
我在我的插件中做同样的事情来生成一个唯一的 ID。
var propertyName = "validctcwb";
var propertyValue = false;
var propertyType = MsoDocProperties.msoPropertyTypeBoolean;
dps.Add(propertyName, false, propertyType, propertyValue);
如果想在调试的时候看到数值,可以引入以下方法:
public static IEnumerable<CustomProperty> GetCustomDocumentProperties(Workbook workbook)
{
foreach (CustomProperty property in workbook.CustomDocumentProperties)
{
yield return property;
}
}
瞧