I have successfully created a C# application using early binding to automate the
generation of a report using Microsoft Word.As the customer base has customers
with many different versions of Word,I have discovered that some versions of Word
behave differently. In order to ensure that the correct Word Interop engine is
used I have re-written the code using late binding.
Everything works except PageDelete !
Here is the original code for PageDelete using early binding
public void PageDelete()
{
//Selects the current page ( There is no real concept of "Page" in Word)
object CurrentPage = @"\page";
object missing = System.Reflection.Missing.Value;
WordApp.ActiveDocument.Bookmarks.Item(ref CurrentPage).Select();
WordApp.ActiveDocument.Bookmarks.Item(ref CurrentPage).Range.Delete
(ref missing, ref missing);
}
Note: In the early binding version the object CurrentPage is passed as a ref object.
My attempt at the late binding version fails
Here is my attempt
public void PageDelete(object wordApplication)
{
Type type;
object CurrentPage = @"\page";
object missing = System.Reflection.Missing.Value;
//select the active document
type = wordApplication.GetType();
object activeDocument = type.InvokeMember("ActiveDocument",
BindingFlags.GetProperty,
null, wordApplication, null);
//select bookmarks
type = activeDocument.GetType();
object Bookmarks = type.InvokeMember("Bookmarks",
BindingFlags.GetProperty,
null, activeDocument, null);
//select the Item CurrentPage
type = Bookmarks.GetType();
object Item = type.InvokeMember("Item",
BindingFlags.InvokeMethod,
null, Bookmarks, new object[] { CurrentPage });
// FAILED AT THIS POINT with
// "An unhandled exception of type 'System.Reflection.TargetInvocationException'
// occurred in mscorlib.dll. Additional information: Exception has been thrown
// by the target of an invocation."
type = Item.GetType();
object Range = type.InvokeMember("Range",
BindingFlags.GetProperty,
null,Item, null);
type = Range.GetType();
type.InvokeMember("Select", BindingFlags.InvokeMethod, null,
Range, new object[] { missing });
//Delete the Current Page
type.InvokeMember("Delete", BindingFlags.InvokeMethod, null,
Range, new object[] { missing ,missing });
}
How can I get the page selection working? Its the only time in the automation that
needed a ref object.
Your help would be most appreciated
)
generation of a report using Microsoft Word.As the customer base has customers
with many different versions of Word,I have discovered that some versions of Word
behave differently. In order to ensure that the correct Word Interop engine is
used I have re-written the code using late binding.
Everything works except PageDelete !
Here is the original code for PageDelete using early binding
public void PageDelete()
{
//Selects the current page ( There is no real concept of "Page" in Word)
object CurrentPage = @"\page";
object missing = System.Reflection.Missing.Value;
WordApp.ActiveDocument.Bookmarks.Item(ref CurrentPage).Select();
WordApp.ActiveDocument.Bookmarks.Item(ref CurrentPage).Range.Delete
(ref missing, ref missing);
}
Note: In the early binding version the object CurrentPage is passed as a ref object.
My attempt at the late binding version fails
Here is my attempt
public void PageDelete(object wordApplication)
{
Type type;
object CurrentPage = @"\page";
object missing = System.Reflection.Missing.Value;
//select the active document
type = wordApplication.GetType();
object activeDocument = type.InvokeMember("ActiveDocument",
BindingFlags.GetProperty,
null, wordApplication, null);
//select bookmarks
type = activeDocument.GetType();
object Bookmarks = type.InvokeMember("Bookmarks",
BindingFlags.GetProperty,
null, activeDocument, null);
//select the Item CurrentPage
type = Bookmarks.GetType();
object Item = type.InvokeMember("Item",
BindingFlags.InvokeMethod,
null, Bookmarks, new object[] { CurrentPage });
// FAILED AT THIS POINT with
// "An unhandled exception of type 'System.Reflection.TargetInvocationException'
// occurred in mscorlib.dll. Additional information: Exception has been thrown
// by the target of an invocation."
type = Item.GetType();
object Range = type.InvokeMember("Range",
BindingFlags.GetProperty,
null,Item, null);
type = Range.GetType();
type.InvokeMember("Select", BindingFlags.InvokeMethod, null,
Range, new object[] { missing });
//Delete the Current Page
type.InvokeMember("Delete", BindingFlags.InvokeMethod, null,
Range, new object[] { missing ,missing });
}
How can I get the page selection working? Its the only time in the automation that
needed a ref object.
Your help would be most appreciated