【问题标题】:Late binding Excel with .NET使用 .NET 后期绑定 Excel
【发布时间】:2015-02-25 08:52:27
【问题描述】:

我需要在 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();

【问题讨论】:

    标签: c# .net ms-word late-binding


    【解决方案1】:

    Is this what you are looking for?

    此外,您可能还想看看 C# 4 中可用的动态关键字。它将使您的代码更易于编写和阅读。

    Compare the code in figure 4 to the code in figure 5

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多