8 Replies Latest reply on Jul 15, 2008 11:03 AM by (Olav_Kvern)

    windows.add

    Level 1
      Hello everyone!

      I have a question, if someone know pls help

      I'm trying to have a new window of a document that I have created.
      The code is this:

      .......
      Dim DocumentPreviewWindow As InDesign.Window
      DocumentPreviewWindow = DocumentPreview.Windows.Add
      .......

      the window gets created, but after that the debuger says this mesage:

      When casting from a number, the value must be a number less than infinity

      Does anyone know what is hapening?

      I'm scripting for InDesign CS2 with Microsoft Visial Basic 2005 EE

      Thank you in advence!

      Nice coding!
        • 1. Re: windows.add
          Level 1
          Hi Ervin,

          Documents create windows, so try something like (VBScript form):

          Rem Given an InDesign document "myDocument"...
          Set myWindow = myDocument.Windows.Add

          Thanks,

          Ole
          • 2. Re: windows.add
            Level 1
            Hi Olav,

            Thank you for the replay.

            Well it seams more complicated.

            I'm using Microsoft Visial Basic 2005 EE and..
            set statement is no longer suported.

            I made a search in the internet:

            http://support.microsoft.com/kb/311328

            and I found that
            Microsoft Visual Basic 2005 Express Edition
            Microsoft Visual Basic .NET 2003 Standard Edition
            Microsoft Visual Basic .NET 2002 Standard Edition
            are related to the same esue.

            So is there any other way to write the same code:
            Set myWindow = myDocument.Windows.Add
            but without the set statement??

            I used Dim insted of Set. In fact it works but the debuger gets me the eror mesage after the window is created??

            I you know any solution please help me solve the problem, if nothing can be done then thank you anyway:)

            Nice coding!
            • 3. Re: windows.add
              Level 1
              Hi Ervin,

              In Microsoft Visual Basic 2005, you just omit the Set statement. Dim is something entirely different. The point remains the same--Documents create Windows. In your first post, you are trying to have a Window create a Window. That won't work.

              Thanks,

              Ole
              • 4. Re: windows.add
                Level 1
                Hi Olav!

                I'm sorry for the truble and thank you for your patience.

                If I omit the set:
                myDocumentWindow = myDocument.Windows.Add
                then e get "Name 'myDocumentWindow' is not declared"

                I tried:
                myDocument.Windows.Add
                It worked (the window was created) BUT.. I have the same error:
                Invalid CastException was unhandled

                Here is my code I'm trying to run:
                ......
                Dim InDesignApplication As InDesign.Application
                InDesignApplication = CreateObject("InDesign.Application.CS2")
                Dim myDocument As InDesign.Document
                myDocument = InDesignApplication.Documents.Add(False)

                myDocumentWindow = myDocument.Windows.Add '????????
                ......

                I can set the Documents.Add(False) to true and I get the window
                created. My point is that I want to show and hide the window,
                without closing the document.

                Thank you!

                Nice coding!
                • 5. Re: windows.add
                  Level 1
                  Hi Ervin,

                  Don't use Dim, and add the line:

                  Option Explicit Off

                  Before any other lines in your VB code.

                  Thanks,

                  Ole
                  • 6. Re: windows.add
                    Level 1
                    Hi Olav,

                    Thank you for the  line.
                    I can omit the Set and use myDocumentWindow without Dim.

                    -1--
                    Here is the code:

                    Option Explicit Off 'Before any other lines in your VB code
                    ......
                    Dim InDesignApplication As InDesign.Application
                    InDesignApplication = CreateObject("InDesign.Application.CS2")
                    Dim myDocument As InDesign.Document
                    myDocument = InDesignApplication.Documents.Add(False)

                    myDocumentWindow = myDocument.Windows.Add
                    .......

                    It compiles without problem, but the same error
                    when I run that comes from debuger:
                    =========================================================
                    InvalidCastException was unhandled

                    Return argument has as invalid type.

                    troubleshooting tips:
                    Whwn casting from a number, the value must be e number less than infinity.
                    Make shure the source type is convertible to the destination type.
                    ============================================================

                    This error hapend to be when i use .Add

                    I tried this:
                    myDocumentWindow = myDocument.Windows.Item(myDocument.Index)
                    and it works.

                    -2--
                    Another thing,
                    ....
                    myDocumentWindow = myDocument.Windows.Item(myDocument.Index)
                    myDocumentWindow.Close()
                    ....
                    It gave me this error:??
                    Can't close last document window. Close the document instead.

                    All I want is to make the document window visible or not, without closing the document.

                    -3--
                    Olav, are this lines the same?

                    -3.1---
                    Option Explicit Off 'Before any other lines in your VB code
                    .....
                    myDocumentWindow = myDocument.Windows.Item(myDocument.Index)
                    MsgBox(myDocumentWindow.Name)
                    .....
                    ------------------------------------------

                    -3.2---
                    Option Explicit On 'Before any other lines in your VB code
                    ....
                    Dim myDocumentWindow As Object
                    myDocumentWindow = myDocument.Windows.Item(myDocument.Index)
                    MsgBox(myDocumentWindow.Name)
                    ------------------------------------------

                    They are doing the same thing and working ok as I run them.

                    Thank you for your time and patience!
                    • 7. Re: windows.add
                      Level 1
                      Hi Olav,

                      I fixed my problem thanks to your tips.
                      The problem was that I used Dim to create documents, Indesign.Application and so.., then used:
                      myDocumentWindow = myDocument.Windows.Add (omitting Dim)

                      I did not understand you well about Dim thing.

                      So silly of me.

                      Well things are working now.

                      Thank you!

                      Nice Coding!
                      • 8. Re: windows.add
                        Level 1
                        Hi Ervin,

                        Option Explicit On means that you must use Dim to declare all of your variables as a specific type. While that's a good thing in other uses of VB, it's not a good thing for InDesign scripting. (Note: Dim does not create documents--that's what myInDesign.Documents.Add does.)

                        InDesign has two types of windows--layout windows and story windows. When you tell InDesign to create a window, and have used Dim to declare the variable as a Window object, you'll get a type mismatch error, because InDesign returns either a LayoutWindow object or a StoryWindow object--never a generic Window object. When you use Dim to declare a variable as an Object, you can get away with it, because Object is a generic object variable type. This can cause problems, however, so it's better to just avoid Dim altogether.

                        Thanks,

                        Ole