8 Replies Latest reply on Nov 7, 2011 7:55 PM by RobertReno

    csharp and Indesign CS5

    IndesignerCoder20

      Hello

       


      I am using visual studio 2008 (c sharp) under windows 7 to createe a indesign document (CS5).

      i basically created a Interop.Indesign.dll via command prompt  using Resources for Visual Basic.tlb ie

       


      TlbImp.exe "C:\ProgramData\Adobe\InDesign\Version 7.0\en_GB\Scripting Support

      \7.0\Resources for Visual Basic.tlb" /out:Interop.InDesign.dll

       

      I imported the resulting dll into my csharp project and tried to uee the code but get an error message ...

       

      Unable to cast COM object of type 'System.__ComObject' to interface type

       

      'InDesign.Application'. This operation failed because the QueryInterface call on the COM

       

      component for the interface with IID '{ABD4CBB2-0CFE-11D1-801D-0060B03C02E4}' failed

       

      due to the following error: No such interface supported (Exception from HRESULT:

       

      0x80004002 (E_NOINTERFACE)).

       

      I am creafing an instance of application obj in the following mann

       


          Type t = Type.GetTypeFromProgID("InDesign.Application");
          InDesign.Application application = (InDesign.Application)Activator.CreateInstance(t);

       


      I have refefrence the indesign dll in the project.Basically I want to use csharp to create a

      indesign document with images and text,

       

      Any suggestions why this doesn;t work?

       

      thanks

        • 1. Re: csharp and Indesign CS5
          menzel7281

          Close down visual studio, close down indesign. 

           

          Open InDesign by right clicking and select run as administrator.  You just have to do this once, all subsequent loads can be under normal permission set.  This lets InDesign create the applicable type libraries.

           

          Now load your project, then select Add Reference, under the COM tab, there should now be a working Adobe InDesign CS5 type library.

           

          Then it's as simple as:

          InDesign.ApplicationClass App = new InDesign.ApplicationClass();

          App.DoScript(_ScriptToProcess.ToString(), InDesign.idScriptLanguage.idJavascript, new object[] { "" }); //sends script to indesign for processing (not assyncronous)

           

          Hope this helps.

          • 2. Re: csharp and Indesign CS5
            IndesignerCoder20 Level 1

            Hello

             

            Great thanks very much. That sorted that issue. Not managed to successfully create a document under windows Indesign cs5 yet.

             

            I'm using the code below ...... bascially I'm getting a run-time error below. Any suggestions ? In the meantime will try and look into fixing this issue.

             

            Thanks in advance.

            A

             

            {


            Unable to cast COM object of type  'InDesign.ApplicationClass' to interface type 'InDesign._Application'.  This operation failed because the QueryInterface call on the COM  component for the interface with IID  '{ABD4CBB2-0CFE-11D1-801D-0060B03C02E4}' failed due to the following  error: No such interface supported (Exception from HRESULT: 0x80004002  (E_NOINTERFACE)).

            }

             

             

             

                          InDesign.ApplicationClass application = new InDesign.ApplicationClass();

             

                        // Set unit type
                        application.ViewPreferences.HorizontalMeasurementUnits = InDesign.idMeasurementUnits.idMillimeters;
                         application.ViewPreferences.VerticalMeasurementUnits = InDesign.idMeasurementUnits.idMillimeters;

             

                        // Create new document
                        application.Documents.Add(true,null);

             

                        // Get active document and change some settings
                        InDesign.Document document =  application.ActiveDocument;
                        document.DocumentPreferences.FacingPages = false;
                        document.DocumentPreferences.PageWidth = 210;
                        document.DocumentPreferences.PageHeight = 297;

             

                        // Get first page (already created) and set margins
                        InDesign.Page page = (InDesign.Page)document.Pages[1];

             

                        page.MarginPreferences.Top = 10;
                        page.MarginPreferences.Bottom = 10;
                        page.MarginPreferences.Left = 20;
                        page.MarginPreferences.Right = 10;

            • 3. Re: csharp and Indesign CS5
              menzel7281 Level 1

              C# is strongly typed, Adobe is not, this causes a lot of issues when programming with C#.  It can be done, I have a distributed network of 20 machines all churning out InDesign documents.  Does about 10,000 documents an hour.

               

              You basically have 2 choices, use the var declaration to allow the compiler to worry about all the typing. (var application = new InDesign.Application();)

              or build all your instructions as javascript into a stringbuilder, then send it using the application.DoScript() function.

               

              I do the latter (and recommend it) and it works out very nicely because all typing considerations can be ignored.  I send 400-500 line scripts this way.

               

              If you go the the latter, the very first line of your javascript should always be:

              app.scriptPreferences.userInteractionLevel = 1699640946;

              This disables all modal dialog boxes that will cause you issues.

               

              edit:

              your first line should also be

              InDesign.Application App = new InDesign.Application();

              instead of ApplicationClass

               

              Message was edited by: menzel7281

              1 person found this helpful
              • 4. Re: csharp and Indesign CS5
                IndesignerCoder20 Level 1

                Hello

                 

                Thanks will do some research using javascript.

                 

                Thanks very for your suggestions.

                A

                • 5. Re: csharp and Indesign CS5
                  RobertReno

                  Hello Menzel,

                   

                   

                  I know this is a very old thread, but I am hoping you or someone else can help me solve my problems scripting InDesgin from Visual Studio 2010 and C#.

                   

                   

                  I was able to view the InDesign Type Library by running InDesign as Administrator and adding the reference in Visual Studio but I get the following results:

                   

                   

                  // Add Referecne to following COM Type Library (Adobe InDesign CS5 Type Library 1.0)

                  // C:\ProgramData\Adobe\InDesign\Version 7.0\en_US\Scripting Support\7.0\Resources for Visual Basic.tlb

                   

                  // Attempt to create an InDesign instance

                  InDesign.Application app1 = (InDesign.Application)COMCreateObject("InDesign.Application");

                  // Results: Starts InDesign Application and then trhows following error.

                  // Error: Unable to cast COM object of type 'System.__ComObject' to interface type 'InDesign.Application'.

                  // This operation failed because the QueryInterface call on the COM component for the interface with IID '{ABD4CBB2-0CFE-11D1-801D-0060B03C02E4}'

                  // failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

                   

                  InDesign.Application app2 = new InDesign.Application();

                  // Resutls: Throws error.

                  // Error: Retrieving the COM class factory for component with CLSID {296CAEB5-C99C-4B3E-9359-6E7D6EAE71FC} failed due to

                  // the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

                   

                  Type t = Type.GetTypeFromProgID("InDesign.Application");

                  InDesign.Application application = (InDesign.Application)Activator.CreateInstance(t);

                  // Resutls: Starts InDesign Application and then trhows following error.

                  // Error: Unable to cast COM object of type 'System.__ComObject' to interface type 'InDesign.Application'.

                  // This operation failed because the QueryInterface call on the COM component for the interface with IID '{ABD4CBB2-0CFE-11D1-801D-0060B03C02E4}'

                  // failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

                   

                  var app3 = new InDesign.Application();

                  // Starts InDesign Application and then trhows following error:

                  // Retrieving the COM class factory for component with CLSID {296CAEB5-C99C-4B3E-9359-6E7D6EAE71FC}

                  // failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

                   

                  Yet, I was able to get the folowing code to work:

                  Type type = Type.GetTypeFromProgID("InDesign.Application.CS5", true);

                  dynamic app = System.Activator.CreateInstance(type, true);

                  var doc = app.Documents.Add();

                   

                  This started the InDesign application and added a document to the window.

                   

                  • 6. Re: csharp and Indesign CS5
                    menzel7281 Level 1

                    Should be as simple as:

                     

                    InDesign.Application App = new InDesign.Application();

                     

                    App.DoScript(_ScriptToProcess.ToString(), idScriptLanguage.idJavascript, new object[] { "" });

                     

                    App = null;

                     

                    (copied directly from my working code)

                     

                    If you're getting class not registered errors, make sure you open InDesign with administrator rights at least once. Refer to the corrected answer part of this thread.

                    • 7. Re: csharp and Indesign CS5
                      RobertReno Level 1

                      Hi Menzel,

                       

                      Thanks for replying. I am a real newbie to trying to automate InDesign from C#, but I have benefited from your notes above and now have access to the InDesign type library from my VS 2010 C# project. I am trying to hack my way through converting all the Javascript in "InDesign CS5 Automation Using XML & JavaScript" to try and learn how to automate InDesign from C#.

                       

                      I could really use some help with a couple tasks that would get me onto the right path. I have been able to start InDesign and add docment and set its preferences, etc. But no luck opening an existing document or using the App.DoScript functionality.

                       

                       

                      object file = @"F:\CSharpScriptingInDesign\Files\000.paper.en.indd";

                       

                      dynamic app = System.Activator.CreateInstance(type, true);

                       

                      string _ScriptToProcess = "var doc = app.documents.Add()";

                       

                       

                      app.DoScript(_ScriptToProcess, InDesign. idScriptLanguage.idJavascript, new object[] { ""});

                       

                      I know I probably doing something really stupid, but if you could point me in the right direction I would be very appreciative.

                      Rob

                      • 8. Re: csharp and Indesign CS5
                        RobertReno Level 1

                        Are there any recources where those who are trying to manipulate InDesign through scripting and IDML can share examples code and help each other learn.  That would be very nice.