谁知道 知道在VC中调用WORD,怎么样才能得到打开文档共有多少页,在WORD的哪个类中可能得到呢?

解决方案 »

  1.   

    好像有个long PageNumbers::GetCount();
      

  2.   

    //generate class from type library
    //remember to call AfxOleInit
    //get page numbers of a document
    //theory:insert pages field to header, read the value, then delete the field 
    //by masterz
    _Application wordapp;
    BOOL bret=wordapp.CreateDispatch("Word.Application");
    wordapp.SetVisible(TRUE);

    COleVariant covTrue((short)TRUE),
    covFalse((short)FALSE),
    covOptional((long)DISP_E_PARAMNOTFOUND,
    VT_ERROR);
    Documents oDocs(wordapp.GetDocuments());  // Use the Constructor
    // for Documents.
    _Document oDoc;

    oDoc.AttachDispatch(oDocs.Open(COleVariant("C:\\Test.doc",VT_BSTR),
    covFalse, //Confirm Conversions.
    covFalse, // Not read only.
    covTrue,  // Add to recent documents.
    covOptional,  // PassWordDocument.
    covOptional,  // PassWordTemplate.
    covFalse, // Revert.
    covOptional,  // WritePasswordDocument.
    covOptional,  // WritePasswordTemplate.
    covOptional,  // Format, e.g., WordOpenFormat.
    covOptional,  // New with Word 9 - Encoding
    covOptional,  // Visible - Ditto
    //   covOptional,  // New with Word 10 - OpenConflictDocument
    covOptional,  // Ditto - OpenAndRepair
    covOptional,  //(long) 0,     // Ditto - DocumentDirection - Left to Right
    covOptional   // Ditto - NoEncodingDialog
    )
    );
    LPDISPATCH lpdisp;//=oDoc.GetSections();
    Sections oSecs;
    lpdisp=oDoc.GetSections();
    oSecs.AttachDispatch(lpdisp);
    lpdisp=oSecs.GetFirst();
    Section oSec;
    oSec.AttachDispatch(lpdisp);
    lpdisp=oSec.GetFooters();
    HeadersFooters hfs;
    hfs.AttachDispatch(lpdisp);
    lpdisp=hfs.Item(1);
    HeaderFooter hf;
    hf.AttachDispatch(lpdisp);

    Range rg=hf.GetRange();

    Fields fds;
    lpdisp=oDoc.GetFields();
    fds.AttachDispatch(lpdisp);
    lpdisp=fds.Add(rg,COleVariant(long(26)),covOptional,covOptional);
    Field fd;
    fd.AttachDispatch(lpdisp);
    fd.Select();
    Selection oSelection = wordapp.GetSelection();
    rg=oSelection.GetRange();
    CString msg;
    rg=oSelection.GetRange();
    msg=rg.GetText();
    rg.Delete(covOptional,covOptional);
    AfxMessageBox(msg);
    wordapp.Quit(covFalse,covOptional,covOptional);