Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

late binding ref object problem

Status
Not open for further replies.

robobri

Programmer
Oct 14, 2009
1
EU
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 :eek:)



 
robobri,

Could please include the full Exception that is thrown including inner exceptions and stack traces, etc.

The exception message you have posted indicates that the member "Item" you are trying to invoke on the return value of Bookmarks.GetType() is throwing an exception. The actual exception that is being thrown should be stored as the InnerException property of the exception you are catching.

Without this information, it will be difficult if not impossible for me to diagnose.

As a side note: late binding and reflection are not one in the same. Late binding can be accomplished without the use of reflection.

I would recommend that you implement an interface upon which all of your program logic is performed. Then you would write a separate class for each version of Word that you intend to support. When your program starts, discover the version of Word the client is using and instantiate the appropriate class that implements your interface. This approach may be tedious, but it will be much less prone to difficult-to-diagnose errors.

Phyrstorm Technologies, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top