Topic
15 replies Latest Post - ‏2013-03-18T18:23:14Z by SystemAdmin
llandale
llandale
2943 Posts
ACCEPTED ANSWER

Pinned topic Make Embedded Word Object function.

‏2013-01-08T21:21:18Z |
Can Someone please write me a DXL function that does this:
  • Open an OLE connection to the 1st of two open MS-Word instances
  • Copy to Clipboard what is currently selected in this instance
  • Open an OLE connection to the 2nd of two open MS-Word instances
  • Paste-Special the contents of the clipboard into an "Miscrosoft Office Word Document Object"
  • Select that Document Object, and "cut" it to the clipboard

I figure to select an Object that needs the Word table; select the Word table and run the code; then paste it into the Object.

-Louie
Updated on 2013-03-18T18:23:14Z at 2013-03-18T18:23:14Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    3180 Posts
    ACCEPTED ANSWER

    Re: Make Embedded Word Object function.

    ‏2013-01-09T08:01:10Z  in response to llandale
    Only one instance of Word will be registered in the ROT (Running Object Table) and available for automation by other processes. This is usually the first instance that was opened. Therefore, you can't automate 2 instances of Word.
    Why not just create a new document paste to that copy and then close without saving? Is it better to just write it in VBA since DOORS is not involved at all?

    Adam
    • SystemAdmin
      SystemAdmin
      3180 Posts
      ACCEPTED ANSWER

      Re: Make Embedded Word Object function.

      ‏2013-01-09T12:40:13Z  in response to SystemAdmin
      What you say about the ROT is 100% true, dxl can initiate VBA that can select alternate documents to achieve desired results.
      • Mathias Mamsch
        Mathias Mamsch
        1921 Posts
        ACCEPTED ANSWER

        Re: Make Embedded Word Object function.

        ‏2013-01-11T10:16:17Z  in response to SystemAdmin
        Well, what you say about the ROT is maybe not entirely true. The ROT (Running Object Table) stores a handle to ALL applications, so if you open two Microsoft Word Processes, then each process can act as a COM server. The bad thing is, that the GetObject function will only return you the handle to the first registered application. So you have no way of accessing two already running processes from DOORS. However you can easily control two different word instances by using 'createObject' twice (Both will be registered in the ROT by the way, since these are both running objects).

        So Louie for you that means you will not get a reliable connection to an open word instance. Period. If the user has another word opened, before he opens you two word instances, you will not get any handle to the OLEs. However the question here would be if you do know the filenames of the files, so you can open one or two word instances yourself and load the data in there? Or why is it so important to use already opened word files?

        Regards, Mathias

        Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
  • llandale
    llandale
    2943 Posts
    ACCEPTED ANSWER

    Re: Make Embedded Word Object function.

    ‏2013-01-11T19:18:35Z  in response to llandale

    I have no idea what I'm doing.
    Functions fWord_OpenFileGet() and fWord_OpenFileNew() seems to work, but I get this error in function clbkCopyPaste()

    • -R-E- DXL: <Temp_l2.dxl:67> null OleAutoObj parameter was passed into argument position 1

    When I use variable g_objDocsSrce at that line I get:

    • Copy: Problem with OLE Argument names

    There seems to be a lot of "OleAutoObj" variables and I'm probably using the wrong one.

    //#include <Includes\Lib-Incs_Level-2.inc>
    //#include <Utils\ole.inc>
     
    string      g_NameSourceFile    = "c:/Doors-Stuff/Reports/ReportsRaw/Attrs_Report-Types-Folder_NVR-2010_2013-Jan-11_000.rtf"
    OleAutoObj      g_objDocsTemp = null, g_objDocsSrce = null
    DB              db
     
    //***************************************************************
    OleAutoObj      fWord_OpenFileNew()
    {     // Create a new MS-Word instance and open a "New" file
            OleAutoObj      objWordTemp,
                            objDocsTemp,
                            objOptsTemp
            OleAutoArgs     AutoArgs
            string          ErrMess
     
            objWordTemp     = oleCreateAutoObject("Word.Application")
                    // Open Temp File
            olePut (objWordTemp, "Visible", true)
            oleGet (objWordTemp, "Documents", objDocsTemp)
                            // Failed: "New", FileNew, 
            ErrMess         = oleMethod (objDocsTemp, "Add")//, AutoArgs)
            print ErrMess
            oleGet(objWordTemp, "Options", objOptsTemp)
            if (!null objOptsTemp)
            {  olePut(objOptsTemp, "CheckSpellingAsYouType", false)
               olePut(objOptsTemp, "CheckGrammarAsYouType",  false)
            }
            oleCloseAutoObject(objWordTemp)
            return(objDocsTemp)
    }     // end fWord_OpenFileNew()
     
                    // Open Source File
     
    //***************************************************************
    OleAutoObj      fWord_OpenFileGet()
    {     // Get a handle on the currently open MS-Word file.  There should be just one open.
            OleAutoObj      objWordTemp,
                            objDocsTemp,
                            objOptsTemp
            OleAutoArgs     AutoArgs
            string          ErrMess
     
            objWordTemp     = oleCreateAutoObject("Word.Application")
            oleGet (objWordTemp, "Documents", objDocsTemp)
            oleCloseAutoObject(objWordTemp)
            return(objDocsTemp)
    }     // end fWord_OpenFileGet()
     
    //***************************************************************
    void    clbkOpenFiles(DBE dbeXX)
    {
            g_objDocsSrce   = fWord_OpenFileGet()
            g_objDocsTemp   = fWord_OpenFileNew()
            print (null g_objDocsTemp) "\t" (null g_objDocsSrce) "\n"
    }     // end clbkOpenFiles()
     
    //***************************************************************
    void    clbkCopyPaste(DBE dbeXX)
    {
            OleAutoObj      objSelected
            OleAutoArgs     AutoArgs = create()
            clear(AutoArgs)
            string  ErrMess
            oleGet(g_objDocsSrce,"Selection",objSelected)
            print "Copy: "  oleMethod(objSelected, "Copy") //, AutoArgs) "\n"
            print "Paste: " oleMethod(objSelected, "Paste") //, AutoArgs) "\n"
            delete(AutoArgs)
    }
     
    //***************************************************************
    void    BuildDialog()
    {    db      = create("OLE Word Test")
            //      button(db, "OpenFiles", clbkOpenFiles)
                    button(db, "CopyPaste", clbkCopyPaste)
     
                    realize(db)
            clbkOpenFiles(null DBE)
                    show(db)
    }    // end BuildDialog()
     
    BuildDialog()
    
    Updated on 2014-01-06T14:11:08Z at 2014-01-06T14:11:08Z by iron-man
    • Mathias Mamsch
      Mathias Mamsch
      1921 Posts
      ACCEPTED ANSWER

      Re: Make Embedded Word Object function.

      ‏2013-01-11T20:52:32Z  in response to llandale

      If I put oleCheck before all oleGet, oleMethod calls,
       

      void oleCheck (string s) { if (!null s) error s }
      

      I get

       

      -R-E- DXL: <Line:8> Problem with OLE Argument names.
      Backtrace:
          <Line:67>   // oleGet(g_objDocsSrce,"Selection",objSelected)
              <Line:84>
      


      The reason seems to be obvious, g_objDocsSrce points to 'Documents' but it should point to a Document. So you need to get ActiveDocument, or Documents(1), and get the Selection from there.

      Regards, Mathias

       

       

       


      Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

       

       

      Updated on 2014-01-06T14:11:44Z at 2014-01-06T14:11:44Z by iron-man
      • llandale
        llandale
        2943 Posts
        ACCEPTED ANSWER

        Re: Make Embedded Word Object function.

        ‏2013-01-14T17:49:09Z  in response to Mathias Mamsch
        I'm completely lost when "outside the box". I have no idea what I'm doing and could get no variation of "ActiveDocument" nor "Document" nor "Documents(1)" to work at all; trying frm "objWordTemp" and "objDocsTemp". What exactly need I do? Do I go from "Documents" to "Document" with a "method"? "Word.Application" to "Document" with a "get"?

        -Louie
        • SystemAdmin
          SystemAdmin
          3180 Posts
          ACCEPTED ANSWER

          Re: Make Embedded Word Object function.

          ‏2013-01-15T08:05:08Z  in response to llandale
          Hello llyndale

          You must not use "activeDocument" because that is an object you only have inside of Excel. Start with Excel.Application, then get "Documents" and then Document.

          Best regards
          Wolfgang
          • SystemAdmin
            SystemAdmin
            3180 Posts
            ACCEPTED ANSWER

            Re: Make Embedded Word Object function.

            ‏2013-01-15T08:12:49Z  in response to SystemAdmin
            Sorry I mean "Word". Actually I've had some problems to post here, because here I've had some strange firewall rules ore else on my pc and that ist the first post, which has had run from here.
            In Word you have the Word Object Modell Overviev (http://msdn.microsoft.com/en-us/library/kw65a0we(v=vs.80).aspx). Do not use Application-Selection, because selection can be changed by a mouse click of the user. In a stable script you shall use Application-Document(s).
          • llandale
            llandale
            2943 Posts
            ACCEPTED ANSWER

            Re: Make Embedded Word Object function.

            ‏2013-01-15T15:43:18Z  in response to SystemAdmin
            For this I am a monkey-at-the-keyboard. After changing "Create" to "Get" I have a "Documents" handle and finally managed to get a "Document" handle using "ActiveDocument"; which is consistent with my procedure of having exactly one Word file open to start. After that, I indeed managed to open an empty Word file by first "Create" and then "Add". Checking the "Name"s I have my correct two "Document" handles.

            I am now stuck on getting the current "Selection" of the original Document; "problem with ole argument names" when doing this:
            • oleCheck(oleGet(g_objDocsSrce, "Selection", objSelected))
            • adevicq
              adevicq
              154 Posts
              ACCEPTED ANSWER

              Re: Make Embedded Word Object function.

              ‏2013-01-15T17:11:38Z  in response to llandale
              Louie,
              In my code I get the selection from the Word object, not from the Document...
              Alain
              • SystemAdmin
                SystemAdmin
                3180 Posts
                ACCEPTED ANSWER

                Re: Make Embedded Word Object function.

                ‏2013-01-16T07:42:29Z  in response to adevicq

                Hello Louie

                First you have to connect to word. Therefore I've three global constants ...

                OleAutoObj oleCompareApplication = null;             // Object to hold a word reference ...
                OleAutoObj oleCompareApplicationDocuments = null;    // Reference to the Documents object ...
                bool bApplicationCreated = false;                    // Marker (false = Word is not availiable on this PC)
                

                 


                To initialize the word connection, you have to use oleGetAutoObject first in order to find a word which is allreaddy running.
                If you do not find this, you have to start a word instance using oleCreateAutoObject.
                If you cannot, you dont have a word installation ...

                 

                 

                 

                /**************************************************************/
                /** Module Initialisation ...
                ***************************************************************/
                void InitUnit() {
                    string sWordApp = "Word.Application";
                        bApplicationCreated = false;
                        // If Word is runnig get an object reference ...
                        oleCompareApplication = oleGetAutoObject(sWordApp);
                        if (oleCompareApplication == null) {
                                // Otherwise start word but do not make it visible (it is faster)
                            oleCompareApplication = oleCreateAutoObject(sWordApp);
                            bApplicationCreated = true;
                        }
                        if (oleCompareApplication == null) {
                            // User does not have a word installation.
                                // some error code?
                        } else {
                                oleCompareArguments = create;
                                oleGet(oleCompareApplication, "Documents", oleCompareApplicationDocuments);   
                        }
                }
                



                Termination is similar:

                /**************************************************************/
                /** Terminate the unit
                If this module has started word, the module shall quit it.
                ***************************************************************/
                void TerminateUnit() {
                if (oleCompareApplication != null) {
                if (bApplicationCreated) {
                string sHelp = oleMethod(oleCompareApplication, "Quit");
                }
                oleCloseAutoObject(oleCompareApplication);
                oleCompareApplication = null;
                clear oleCompareArguments;
                delete oleCompareArguments;
                }
                }

                Between those routines you can use oleCompareApplicationDocuments.

                Best regards
                Wolfgang



                 

                 

                Updated on 2014-01-06T14:12:20Z at 2014-01-06T14:12:20Z by iron-man
            • SystemAdmin
              SystemAdmin
              3180 Posts
              ACCEPTED ANSWER

              Re: Make Embedded Word Object function.

              ‏2013-01-16T11:06:08Z  in response to llandale

              The 'Selection' object is related to the Word GUI. So you can only get the selection of the 'ActiveDocument' because only the 'ActiveDocument' has anything selected. Sure, you can get the 'Selection' from many Objects, but it will always return the selection of the 'ActiveDocument' even if called from a different document object.

              Therefore, it makes sense that the 'Selection' is a property of the Word Application object. It is also why you should use 'Range' objects when there is no user interaction - a 'Range' is tied to a Document.
               

              // Application.Selection
              oleGet(objApplication, "Selection", objSelection)
              

               

              If you want the selection on a specific document, you must first make it the 'ActiveDocument'.

               

              // Document.Activate
              oleMethod(objDocument, "Activate")
              

               

               

              Updated on 2014-01-06T14:12:53Z at 2014-01-06T14:12:53Z by iron-man
              • llandale
                llandale
                2943 Posts
                ACCEPTED ANSWER

                Re: Make Embedded Word Object function.

                ‏2013-01-18T18:57:43Z  in response to SystemAdmin
                I think this may have gotten me over the hump thanks. I see now Application->Documents->Document->Selection Objects here:
                [http://msdn.microsoft.com/en-us/library/bb244515(v=office.12).aspx]
                It appears I need to keep track of several such Objects; unless there is some way to go back up the chain, Selection->Document->Documents->Application.

                -Louie
                • SystemAdmin
                  SystemAdmin
                  3180 Posts
                  ACCEPTED ANSWER

                  Re: Make Embedded Word Object function.

                  ‏2013-01-21T06:41:20Z  in response to llandale
                  > It appears I need to keep track of several such Objects;

                  Yes, you do ...

                  > unless there is some way to go back up the chain, Selection->Document->Documents->Application.

                  You can use "Parent" but

                  a) I do not recommend this, I cannot say anything about this method
                  b) The number you objects you have to track is the same.
                  c) In the internet you finde examples in the top down direction not in the other.
  • SystemAdmin
    SystemAdmin
    3180 Posts
    ACCEPTED ANSWER

    Re: Make Embedded Word Object function.

    ‏2013-03-18T18:23:14Z  in response to llandale
    My customer wants to attach a word document into a lotus notes document and have me code a function to parse data from the word document into fields on the Lotus Notes document. We do know that the data will have key text I can search with to find the data to import into the fields. I was hoping there is an OLE object that I can use like I do for EXCEL in my notes script to parse the data.

    I have been able to open the word document and make it visible using the following:
    Dim myWDoc As Variant
    Dim filename As String
    filename = "c:\\RCATest.docx" ' just for test purposes right now, later i will uses whatever they have attached....
    Set myWDoc = GetObject(filename,"Word.Document")
    myWDoc.Application.Visible = True

    But I don't want to make the document visible, I just want to read it in (using script) and parse it out like any text file. Is this possible? If yes, can someone point me to the calls I would need to read in the data from the word document?

    Thanks for any input.