Figure 1: Partial listing of WordStream.cpp

#include <afxwin.h>
#import "mso97.dll" no_namespace rename\
    ("DocumentProperties", "DocumentPropertiesXL")
#import "vbeext1.olb" no_namespace
#import "ven2232.olb"  
#import "msword8.olb" rename\
    ("ExitWindows","ExitWindowsWRD")
using namespace Word;
#include "WordStream.hpp"

//...

CWordStream::CWordStream()
{
    m_nUnderLine = WSTR_UL_NONE;
    m_nJustification = WSTR_JU_CENTER;
    m_bBold = FALSE;
    m_bItalic = FALSE;
    INT nLastSize = 0;
    INT nCurrentSize = 0;

    _Document *pDoc=NULL;
    REFCLSID clsid = __uuidof(Document);
    REFIID iid = __uuidof(_Document);
    
    // Create an empty Word document; the
    // result is an interface of type _Document.
    HRESULT res = CoCreateInstance(
                    clsid, 
                    NULL,
                    CLSCTX_LOCAL_SERVER,
                    iid,
                    (void**)&pDoc);

    if (SUCCEEDED(res)) {
        m_pUnknown = NULL;
        REFIID iid2 = __uuidof(IUnknown);
        // Retrieve an IUnknown pointer to the object.
        res = pDoc->QueryInterface(iid2,(void**)&m_pUnknown);
        if (!SUCCEEDED(res)) {
            m_bError = TRUE;
            m_szError = _T("Cannot get IUnknown interface.");
        }
        m_fFontSize = pDoc->GetContent()->GetFont()->GetSize();
    } else {
        m_bError = TRUE;
        m_szError = _T("Cannot create document object.");
    }
}

//...

UINT CWordStream::AddText( const CString &str )
{
    _Document *pDoc=NULL;
    REFIID iid = __uuidof(_Document);
    HRESULT res;
    res = m_pUnknown->QueryInterface(iid,(void**)&pDoc);
    if (!SUCCEEDED(res)) {
        m_bError = TRUE;
        m_szError = _T("Error getting _Document interface.");
        return WSTR_FAIL;
    }
                
    // Define a range containing the entire document
    RangePtr range = pDoc->GetContent();
    bstr_t bstr = str;
    LONG nLastSize = range->GetStoryLength()-1;

    // Add the string to the end of the document.
    range->InsertAfter(bstr);
    LONG nCurrentSize = range->GetStoryLength()-1;
    _variant_t vbegin( nLastSize,    VT_I4 );
    _variant_t vend  ( nCurrentSize, VT_I4 );

    // Define a range containing the text just inserted.
    range = pDoc->Range(&vbegin,&vend);

    // Set attributes on the range.
    range->PutItalic(m_bItalic);
    range->PutBold(m_bBold);
    range->PutUnderline(mapUnderLineCodes[m_nUnderLine]);
    range->GetFont()->PutSize(m_fFontSize);

    // We need a ParagraphFormat object to define the text
    // alignment.
    _ParagraphFormatPtr pformat = range->GetParagraphFormat();
    pformat->PutAlignment(
        mapJustificationCodes[m_nJustification]);

    return WSTR_OKAY;
}

//...

UINT CWordStream::PrintOut()
{
    _Document *pDoc=NULL;
    REFIID iid = __uuidof(_Document);
    HRESULT res;
    res = m_pUnknown->QueryInterface(iid,(void**)&pDoc);
    if (!SUCCEEDED(res)) {
        m_bError = TRUE;
        m_szError = _T("Error getting _Document interface.");
        return WSTR_FAIL;
    }
    _variant_t doNotBackgroundPrint(long(0),VT_I4);
    pDoc->PrintOut(&doNotBackgroundPrint);
    return WSTR_OKAY;
}

//...

//End of File