后期将 Excel 与 .NET 绑定
我需要在 Word 文档中添加“书签”的值。我确实需要使用后期绑定来实现这一点。
我已经提取了最多书签,但是如何更改该值?
object bookMark = @"OfferRef";
Type applicationType = Type.GetTypeFromProgID("Word.Application");
object applicationObject = Activator.CreateInstance(applicationType);
object documentsObject = applicationType.InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty,
null, applicationObject, null);
applicationType.InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, applicationObject,
new object[] { true });
Type documentsType = documentsObject.GetType();
object documentObject = documentsType.InvokeMember("Add", BindingFlags.InvokeMethod, null, documentsObject,
new Object[] { @"e:\offer.doc"});
Type documentType = documentObject.GetType();
object fieldsBookMarks = documentType.InvokeMember("BookMarks", BindingFlags.GetProperty, null, documentObject, null);
Type typeBookMarks = fieldsBookMarks.GetType();
object bookMark = typeBookMarks.InvokeMember("Item", BindingFlags.InvokeMethod, null, fieldsBookMarks, new object[] { bookMark });
Type type = bookMark.GetType();
object Range = type.InvokeMember("Range", BindingFlags.GetProperty, null, bookMark, null);
type = Range.GetType();
I need to add values for my "BookMarks" in a Word document. There I do need to achieve this using late binding.
I have extracted upto book mark, but how do I change the value?
object bookMark = @"OfferRef";
Type applicationType = Type.GetTypeFromProgID("Word.Application");
object applicationObject = Activator.CreateInstance(applicationType);
object documentsObject = applicationType.InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty,
null, applicationObject, null);
applicationType.InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, applicationObject,
new object[] { true });
Type documentsType = documentsObject.GetType();
object documentObject = documentsType.InvokeMember("Add", BindingFlags.InvokeMethod, null, documentsObject,
new Object[] { @"e:\offer.doc"});
Type documentType = documentObject.GetType();
object fieldsBookMarks = documentType.InvokeMember("BookMarks", BindingFlags.GetProperty, null, documentObject, null);
Type typeBookMarks = fieldsBookMarks.GetType();
object bookMark = typeBookMarks.InvokeMember("Item", BindingFlags.InvokeMethod, null, fieldsBookMarks, new object[] { bookMark });
Type type = bookMark.GetType();
object Range = type.InvokeMember("Range", BindingFlags.GetProperty, null, bookMark, null);
type = Range.GetType();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您正在寻找的内容吗?
此外,您可能还想获取看一下 C# 4 中可用的动态关键字。它将使您的代码更易于编写和阅读。
将图 4 中的代码与图 5 中的代码进行比较
Is this what you are looking for?
In addition you might want to take a look at the dynamic keyword that is available in C# 4. It will make your code easier to write and read.
Compare the code in figure 4 to the code in figure 5