IWebBrowser2::Document PropertyRetrieves the automation object of the active document, if any.SyntaxHRESULT IWebBrowser2::get_Document(IDispatch **ppDisp); When the active document is an HTML page, this property provides access to the contents of the HTML Document Object Model (DOM). Specifically, it returns an IDispatch interface pointer to the HTMLDocument component object class (coclass). The HTMLDocument coclass is functionally equivalent to the Dynamic HTML (DHTML) document object used in HTML script. It supports all the properties and methods necessary to access the entire contents of the active HTML document.C++ programs can retrieve the Component Object Model (COM) interfaces IHTMLDocument, IHTMLDocument2, and IHTMLDocument3 by calling QueryInterface on the IDispatch received from this property.When other document types are active, such as a Microsoft® Word document, this property returns the default IDispatch dispatch interface (dispinterface) pointer for the hosted document object. For Word documents, this would be functionally equivalent to the Document object in the Word object model. For more information on the Microsoft® Office DOM, refer to the Microsoft Office Developer Center . now u can work with this document,for example extern IDispatch *pWordDoc;//a Microsoft Word document // Get BuiltinDocumentProperties collection IDispatch *pProps; { VARIANT result; VariantInit(&result); AutoWrap(DISPATCH_PROPERTYGET, &result, pDoc, L"BuiltinDocumentProperties", 0); pProps = result.pdispVal; } // Get "Subject" from BuiltInDocumentProperties.Item("Subject") IDispatch *pPropSubject; { VARIANT result; VariantInit(&result); VARIANT x; x.vt = VT_BSTR; x.bstrVal = ::SysAllocString(L"Subject"); AutoWrap(DISPATCH_PROPERTYGET, &result, pProps, L"Item", 1, x); pPropSubject = result.pdispVal; SysFreeString(x.bstrVal); } // Get the Value of the Subject property and display it { VARIANT result; VariantInit(&result); AutoWrap(DISPATCH_PROPERTYGET, &result, pPropSubject, L"Value", 0); char buf[512]; wcstombs(buf, result.bstrVal, 512); ::MessageBox(NULL, buf, "Subject", 0x10000);
// Close the document without saving changes and quit Word { VARIANT x; x.vt = VT_BOOL; x.boolVal = false; AutoWrap(DISPATCH_METHOD, NULL, pDoc, L"Close", 1, x); AutoWrap(DISPATCH_METHOD, NULL, pWordApp, L"Quit", 0); } see alsoKB articlesHOWTO: Catch Microsoft Word97 Application Events Using VC++ Q183599 HOWTO: Add and Run a VBA Macro Using Automation from MFC Q194906 HOWTO: Use MFC to Retrieve a List of Macro Names in an Office Document Q274680 HOWTO: Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, or Word 2002 Q252719Technical ArticlesAutomating Microsoft Office 97 and Microsoft Office 2000 Lori Turner Microsoft Corporation March 2000
When the active document is an HTML page, this property provides access to the contents of the HTML Document Object Model (DOM). Specifically, it returns an IDispatch interface pointer to the HTMLDocument component object class (coclass). The HTMLDocument coclass is functionally equivalent to the Dynamic HTML (DHTML) document object used in HTML script. It supports all the properties and methods necessary to access the entire contents of the active HTML document.C++ programs can retrieve the Component Object Model (COM) interfaces IHTMLDocument, IHTMLDocument2, and IHTMLDocument3 by calling QueryInterface on the IDispatch received from this property.When other document types are active, such as a Microsoft® Word document, this property returns the default IDispatch dispatch interface (dispinterface) pointer for the hosted document object. For Word documents, this would be functionally equivalent to the Document object in the Word object model. For more information on the Microsoft® Office DOM, refer to the Microsoft Office Developer Center .
now u can work with this document,for example
extern IDispatch *pWordDoc;//a Microsoft Word document // Get BuiltinDocumentProperties collection
IDispatch *pProps;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pDoc,
L"BuiltinDocumentProperties", 0);
pProps = result.pdispVal;
} // Get "Subject" from BuiltInDocumentProperties.Item("Subject")
IDispatch *pPropSubject;
{
VARIANT result;
VariantInit(&result);
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"Subject");
AutoWrap(DISPATCH_PROPERTYGET, &result, pProps, L"Item", 1, x);
pPropSubject = result.pdispVal;
SysFreeString(x.bstrVal);
} // Get the Value of the Subject property and display it
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pPropSubject, L"Value",
0);
char buf[512];
wcstombs(buf, result.bstrVal, 512);
::MessageBox(NULL, buf, "Subject", 0x10000);
} // Set the Value of the Subject DocumentProperty
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"This is my subject");
AutoWrap(DISPATCH_PROPERTYPUT, NULL, pPropSubject, L"Value", 1,
x);
::MessageBox(NULL,
"Subject property changed, examine document.",
"Subject", 0x10000);
SysFreeString(x.bstrVal);
} // Get CustomDocumentProperties collection
IDispatch *pCustomProps;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pDoc,
L"CustomDocumentProperties", 0);
pCustomProps = result.pdispVal;
} // Add a new property named "CurrentYear"
{
VARIANT parm1, parm2, parm3, parm4;
parm1.vt = VT_BSTR;
parm1.bstrVal = SysAllocString(L"CurrentYear");
parm2.vt = VT_BOOL;
parm2.boolVal = false;
parm3.vt = VT_I4;
parm3.lVal = 1; //msoPropertyTypeNumber = 1
parm4.vt = VT_I4;
parm4.lVal = 1999; AutoWrap(DISPATCH_METHOD, NULL, pCustomProps, L"Add", 4, parm4,
parm3, parm2, parm1);
::MessageBox(NULL, "Custom property added, examine document.",
"Custom Property", 0x10000);
SysFreeString(parm1.bstrVal);
} // Get the custom property "CurrentYear" and delete it
IDispatch *pCustomProp;
{
VARIANT result;
VariantInit(&result);
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"CurrentYear");
AutoWrap(DISPATCH_PROPERTYGET, &result, pCustomProps, L"Item",
1, x);
pCustomProp = result.pdispVal;
SysFreeString(x.bstrVal);
AutoWrap(DISPATCH_METHOD, NULL, pCustomProp, L"Delete", 0);
::MessageBox(NULL,
"Custom property removed, examine document.",
"Custom Property", 0x10000);
}
// Close the document without saving changes and quit Word
{
VARIANT x;
x.vt = VT_BOOL;
x.boolVal = false;
AutoWrap(DISPATCH_METHOD, NULL, pDoc, L"Close", 1, x);
AutoWrap(DISPATCH_METHOD, NULL, pWordApp, L"Quit", 0);
}
see alsoKB articlesHOWTO: Catch Microsoft Word97 Application Events Using VC++
Q183599
HOWTO: Add and Run a VBA Macro Using Automation from MFC
Q194906
HOWTO: Use MFC to Retrieve a List of Macro Names in an Office Document
Q274680
HOWTO: Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, or Word 2002
Q252719Technical ArticlesAutomating Microsoft Office 97 and Microsoft Office 2000
Lori Turner
Microsoft Corporation
March 2000