Make Embedded Word Object function.

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
llandale - Tue Jan 08 16:21:18 EST 2013

Re: Make Embedded Word Object function.
SystemAdmin - Wed Jan 09 03:01:10 EST 2013

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

Re: Make Embedded Word Object function.
SystemAdmin - Wed Jan 09 07:40:13 EST 2013

SystemAdmin - Wed Jan 09 03:01:10 EST 2013
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

What you say about the ROT is 100% true, dxl can initiate VBA that can select alternate documents to achieve desired results.

Re: Make Embedded Word Object function.
Mathias Mamsch - Fri Jan 11 05:16:17 EST 2013

SystemAdmin - Wed Jan 09 07:40:13 EST 2013
What you say about the ROT is 100% true, dxl can initiate VBA that can select alternate documents to achieve desired results.

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

Re: Make Embedded Word Object function.
llandale - Fri Jan 11 14:18:35 EST 2013

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()

Re: Make Embedded Word Object function.
Mathias Mamsch - Fri Jan 11 15:52:32 EST 2013

llandale - Fri Jan 11 14:18:35 EST 2013

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()

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

 

 

Re: Make Embedded Word Object function.
llandale - Mon Jan 14 12:49:09 EST 2013

Mathias Mamsch - Fri Jan 11 15:52:32 EST 2013

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

 

 

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

Re: Make Embedded Word Object function.
SystemAdmin - Tue Jan 15 03:05:08 EST 2013

llandale - Mon Jan 14 12:49:09 EST 2013
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

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

Re: Make Embedded Word Object function.
SystemAdmin - Tue Jan 15 03:12:49 EST 2013

SystemAdmin - Tue Jan 15 03:05:08 EST 2013
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

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).

Re: Make Embedded Word Object function.
llandale - Tue Jan 15 10:43:18 EST 2013

SystemAdmin - Tue Jan 15 03:05:08 EST 2013
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

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))

Re: Make Embedded Word Object function.
adevicq - Tue Jan 15 12:11:38 EST 2013

llandale - Tue Jan 15 10:43:18 EST 2013
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))

Louie,
In my code I get the selection from the Word object, not from the Document...
Alain

Re: Make Embedded Word Object function.
SystemAdmin - Wed Jan 16 02:42:29 EST 2013

adevicq - Tue Jan 15 12:11:38 EST 2013
Louie,
In my code I get the selection from the Word object, not from the Document...
Alain

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



 

 

Re: Make Embedded Word Object function.
SystemAdmin - Wed Jan 16 06:06:08 EST 2013

llandale - Tue Jan 15 10:43:18 EST 2013
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))

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")

 

 

Re: Make Embedded Word Object function.
llandale - Fri Jan 18 13:57:43 EST 2013

SystemAdmin - Wed Jan 16 06:06:08 EST 2013

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")

 

 

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

Re: Make Embedded Word Object function.
SystemAdmin - Mon Jan 21 01:41:20 EST 2013

llandale - Fri Jan 18 13:57:43 EST 2013
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

> 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.

Re: Make Embedded Word Object function.
SystemAdmin - Mon Mar 18 14:23:14 EDT 2013

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.