更新“链接到内容” excel自定义属性

编程入门 行业动态 更新时间:2024-10-21 14:41:26
本文介绍了更新“链接到内容” excel自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,

我们有以下代码可以更新Excel文件自定义属性值。此代码适用于所有属性,但标记为"链接到内容"的属性除外

We have following code that updates excel file custom property values. This code works for all the properties except for the properties which are marked "Link To Content"

是否有办法更新标记为"链接到内容"的属性。下图显示了如何完成内容链接。

Is there a way to update the properties marked "Link To Content". The below image shows how link to content is done.

是否可以将此类链接属性更改为不来自单元格的值?

is it possible to change such linked property to a value which does not come from cell?

object objDocProps; Type typeObjDocProps = objDocProps.GetType(); string propName; //Name of the property that needs to be updated to a new value "DOC_DESC" string propVal; // New value of the property d1111 object objPropsCount = typeObjDocProps.InvokeMember("Count", BindingFlags.Default | BindingFlags.GetProperty, null, objDocProps, new object[] { }); int propCount = (int)objPropsCount; if (propCount > 0) { object objProp = null; try { objProp = typeObjDocProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, objDocProps, new object[] { propName }); } catch (Exception ex1) { } try { object propval_obj = propVal; Type typeObjProp = objProp.GetType(); object objpropname = typeObjProp.InvokeMember("Name", BindingFlags.Default | BindingFlags.GetProperty, null, objProp, new object[] { }); LogHelper.Instance.Write("Setting property value..."); // This line throws exception for properties that are marked "Link to Content" object objpropval = typeObjProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.SetProperty, null, objProp, new object[] { propval_obj }); // Exception is /* ERROR: Exception occurred. Exception has been thrown by the target of an invocation. mscorlib at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args) */ } catch (Exception ex2) { LogHelper.Instance.Write("ERROR: Exception occurred."); LogHelper.Instance.LogException(ex2); } }

推荐答案

您好,

更新"链接到内容"的值自定义属性,我们可以获取名称对象,然后获取引用的范围。更新范围值,然后更新属性值。

To update the value for "Link to Content" custom property, we could get the name object and then get the referenced range. Update the range value, then the property value is also updated.

以下是在VBA中使用早期绑定来更新值的示例。

Here is the sample to use early binding in VBA to update the value.

Sub Macro1() Dim p As DocumentProperty For Each p In ActiveWorkbook.CustomDocumentProperties If p.LinkToContent Then ActiveWorkbook.Names(p.LinkSource).RefersToRange.Value = "test" End If Next End Sub

根据您的代码,您正在使用后期绑定。以下是设置自定义属性"link"的值的示例。它与内容相关联。

According to your code, you are using late binding. Here is the example to set the value of the custom property "link" which islinked to content.

object exlApp = Marshal.GetActiveObject("Excel.Application"); object wbs = exlApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, exlApp, null); object wb = wbs.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, wbs, new object[] { 1 }); object propers = wb.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.GetProperty, null, wb, null); object objPropsCount = propers.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, propers, null); int propCount = (int)objPropsCount; if (propCount > 0) { object proper = propers.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, propers, new object[] {"link"}); object getValue = proper.GetType().InvokeMember("Value", BindingFlags.GetProperty, null, proper, null); object getLink = proper.GetType().InvokeMember("LinkToContent", BindingFlags.GetProperty, null, proper, null); if ((Boolean)getLink == true) { object linkSoure= proper.GetType().InvokeMember("LinkSource", BindingFlags.GetProperty, null, proper, null); object names = wb.GetType().InvokeMember("Names", BindingFlags.GetProperty, null, wb, null); object name = names.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, names, new object[] { linkSoure }); object rng =name.GetType().InvokeMember("RefersToRange", BindingFlags.GetProperty, null, name, null); rng.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, rng, new object[] { "newValue" }); } }

问候,

Celeste

更多推荐

更新“链接到内容” excel自定义属性

本文发布于:2023-10-27 03:09:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性   链接   内容   excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!