Get graphic object from InDesign using COM object in C#

CatsSolutions
CatsSolutions used Ask the Experts™
on
I am looking at automating some functions in InDesign using a WPF app in C# using the COM library. I have managed to open a document, get text frames and update text within the document.

I would like to swap out images within graphic frames but I am having trouble doing so, I can't even get the graphic object.

I am using the below code to try and get graphic objects from the document:

Type type = Type.GetTypeFromProgID("InDesign.Application");
Application app = (Application)Activator.CreateInstance(type, true);

 //Set units to mm
app.ViewPreferences.HorizontalMeasurementUnits = idMeasurementUnits.idMillimeters;
app.ViewPreferences.VerticalMeasurementUnits = idMeasurementUnits.idMillimeters;

//Open Document
app.Open(@"C:\document.indd", true, idOpenOptions.idOpenOriginal);
Document doc = app.ActiveDocument;

Page pg = doc.Pages[1];
Graphics gs = (Graphics)pg.AllGraphics;

Open in new window


but I keep getting this error:

An unhandled exception of type 'System.InvalidCastException' occurred in InDesignTest.exe

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'InDesign.Graphics'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C85A4AB3-9492-4C40-8A7B-F8305706C8B8}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Has anyone used InDesign successfully with C#, any help would be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle AbrahamsSenior .Net Developer

Commented:
What type is pg.AllGraphics?
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you added a COM reference to the project, then I am curious why you are using Type.GetTypeFromProgID?

Type type = Type.GetTypeFromProgID("InDesign.Application");
Application app = (Application)Activator.CreateInstance(type, true);

You should be able to create an instance of the InDesign.Application with early-binding, instead of late-binding.

Author

Commented:
I will admit that I copied most of this code from the internet to give me a start as I have only just started working with the InDesign COM and every sample I have seen uses the late-binding.

I have just tried creating a new instance of InDesign.Application and I get a class not registered error, Using late binding is working up until trying to get the graphics.


The pg.Allgraphics returns a COM object.
Most Valuable Expert 2012
Top Expert 2008
Commented:
You can get the type name for the COM object by using Microsoft.VisualBasic.Information.TypeName(object)

You just need to add a project reference to Microsoft.VisualBasic.

Author

Commented:
This has helped a lot, thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial