Link to home
Start Free TrialLog in
Avatar of jaba
jaba

asked on

Doc file creation

I am going to write some code to create empty WinWord document. I do this:

    CString ProgID = "Word.Document";
    CLSID wordID;
    HRESULT hRes = CLSIDFromProgID(ProgID.AllocSysString() , &wordID);

    ILockBytes* pLock = NULL;
    hRes = CreateILockBytesOnHGlobal(NULL, TRUE , &pLock);

    IStorage* pTmpStor;
    hRes = StgCreateDocfileOnILockBytes(pLock,  
        STGM_DIRECT | STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &pTmpStor);


    IPersistStorage* pPersist = NULL;
    hRes = CoCreateInstance(wordID , NULL , CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER ,
    IID_IPersistStorage, (void**)&pPersist);

    IStorage* pStor = NULL;
    CString FileName ("aaa.doc");
    hRes = StgCreateDocfile(FileName.AllocSysString() ,
        STGM_DIRECT | STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &pStor);

    hRes = pPersist->InitNew(pStor);
    hRes = pPersist->Save(pStor, TRUE);
    hRes = pPersist->SaveCompleted(pStor);

    pPersist->Release();
    pStor->Release();
    pTmpStor->Release();
    pLock->Release();

Really its a full circle if "Save as" operation for OLE document. Because i am creating new object it must be empty. All working ok (each hRes testing to FAILED after every operation). Why after all this actions i am opening aaa.doc buy MS WinWord and see a lot of strange symbols in text ? Why document isnt empty ?

ASKER CERTIFIED SOLUTION
Avatar of dr_funfrock
dr_funfrock

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial