2 out of 4 rated this helpful - Rate this topic

write method

[This documentation is preliminary and is subject to change.]

Writes one or more HTML expressions to a document in the specified window.

Document Object Model (DOM) Level 2 HTML Specification, Section 1.5

Syntax


HRESULT retVal = object.write(psarray);

Standards information

Parameters

psarray [in]

Type: SAFEARRAY

A SAFEARRAY of BSTR that specifies the text and HTML tags to write.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

Note  When document.IHTMLDocument2::write or document.IHTMLDocument2::writeln is used in an event handler, you must also use document.IHTMLDocument2::close.

Examples

This example shows how to write a string to the document.


    IHTMLDocument2 *document; // Declared earlier in the code
    BSTR bstr = SysAllocString(OLESTR("Written by IHTMLDocument2::write()."));
    // Creates a new one-dimensional array
    SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
    if (psaStrings == NULL) {
        goto cleanup;
    }
    VARIANT *param;
    HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
    param->vt = VT_BSTR;
    param->bstrVal = bstr;
    hr = SafeArrayUnaccessData(psaStrings);
    hr = document->write(psaStrings);
cleanup:
    // SafeArrayDestroy calls SysFreeString for each BSTR
    if (psaStrings != NULL) {
        SafeArrayDestroy(psaStrings);
    }

See also

Reference
IHTMLDocument2::writeln
IHTMLDocument2::open

 

 

Build date: 3/8/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
It replaces existing text...
Sample code replaces existing text ... I wonder why.
write method problem
although it accept SafeArray parameter, but in fact, it can only take BSTR as element, so, you always write unicode string to the document. therefor you lost original charset when you call charset method.
How to use write and close method

I use write method to write HTML(string) to IHTMLDocument2 , after that ,when I want to close application but I can't for that the IHTMLDocument2 don't Close. But When I use Close method I can't get the document

Sample code is wrong

The BSTR allocated by SysAllocString is transferred into the SafeArray. So it is owned by the SafreArray itself and should not be freed separately.

The complete contents of the SafeArray incl. all Variants is freed when SafeArrayDestroy is called.