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();
4

1 回答 1

2

这是你想要的?

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

将图 4 中的代码与图 5 中的代码进行比较

于 2011-03-22T11:39:15.017 回答