后期将 Excel 与 .NET 绑定

发布于 2024-10-25 02:31:53 字数 1302 浏览 2 评论 0原文

我需要在 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 技术交流群。

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

发布评论

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

评论(1

半夏半凉 2024-11-01 02:31:53

这是您正在寻找的内容吗?

此外,您可能还想获取看一下 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

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