Hello,

I'm developing an ActiveX control that automates MS Word. My control is
based on 2 samples from MSDN, the first one employs doc/view model as
described in the following article:

http://www.microsoft.com/mind/0497/mfc.asp

the second one explains how to automate MS Word in a MFC SDI application.
The function that automates Word in the second sample looks as follows:

/************************************************** ******************
* This method encapsulates the process of embedding an Word
* document in a View object and automating that document to add
* some text.
************************************************** ******************/
void CWordView::EmbedAutomateWord()
{
//Change the cursor so that the user knows that something exciting is going
//on.
BeginWaitCursor();

CWordItem* pItem = NULL;

TRY
{
//Get the document that is associated with this view, and be sure that it
is
//valid.
CWordDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

//Create a new item associated with this document, and be sure that it is
//valid.
pItem = new CWordItem(pDoc);
ASSERT_VALID(pItem);

// Get the Class ID for the Word document.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Word.Document",&clsid)))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();

// Create the Word embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();

//Make sure that the new CContainerItem is valid.
ASSERT_VALID(pItem);

// Start the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);

// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // Set selection to the last inserted item.
pDoc->UpdateAllViews(NULL);

//Query for the dispatch pointer for the embedded object. In
//this case, this is the Word document.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();

//Add text to the embedded Word document.
//set CDocument0 wdDoc to use lpDisp, the IDispatch* of the
//actual document.
m_document.AttachDispatch (lpDisp);


//Get a CRange object for the document.
m_range = m_document.Range (COleVariant ((long) DISP_E_PARAMNOTFOUND,
VT_ERROR), COleVariant ((long) DISP_E_PARAMNOTFOUND, VT_ERROR));

//Fill the range with the string "Hello, World!"
m_range.SetText ("Hello, World!");

// get application instance
m_application.AttachDispatch (m_document.GetApplication ());


// hide rulers
Window window;

window.AttachDispatch (m_application.GetActiveWindow ());


Pane pane;

pane.AttachDispatch (window.GetActivePane ());

pane.SetDisplayRulers (FALSE);
}

//Clean up if something went wrong.
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
//AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH

//Set the cursor back to normal so the user knows exciting stuff
//is no longer happening.
EndWaitCursor();
}


The problem is that when I try to call the Open and SaveAs methods of the
_Document class, error messages like "the document is locked by a different
application" and the like appear. So, the question is how to use the Word's
SaveAs and Open automating it in an ActiveX control as described above. Any
help would be appreciated.

Thanks in advance,
Dennis.