Visual c++ 在c+;中打印microsoft word中的特定页面范围时出错+;应用程序

Visual c++ 在c+;中打印microsoft word中的特定页面范围时出错+;应用程序,visual-c++,mfc,ms-word,Visual C++,Mfc,Ms Word,我遇到了一个奇怪的问题,当我试图从控制台应用程序打印microsoft word中的特定页面范围时,我看到了奇怪的结果,我假设这是我在指定页面范围时做得不正确 似乎在打印页面范围并获取word文档中的总页数后,该数字在打印特定范围后会发生变化。另一件奇怪的事情是,它可以在调试模式下工作,但不能在发布模式下工作 前。 Word文档由2页组成 打印第1-1页。 获取页数返回2 打印第2-2页 获取页数返回1 下面是打印一系列页面的代码: int CWordComm::PrintAndCloseAct

我遇到了一个奇怪的问题,当我试图从控制台应用程序打印microsoft word中的特定页面范围时,我看到了奇怪的结果,我假设这是我在指定页面范围时做得不正确

似乎在打印页面范围并获取word文档中的总页数后,该数字在打印特定范围后会发生变化。另一件奇怪的事情是,它可以在调试模式下工作,但不能在发布模式下工作

前。 Word文档由2页组成 打印第1-1页。 获取页数返回2

打印第2-2页 获取页数返回1

下面是打印一系列页面的代码:

int CWordComm::PrintAndCloseActiveDocument(int iNumOfCopies, short nTray, int pageNumber)
{
    // Convenient values declared as ColeVariants.
    COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

    _Document oActiveDoc = m_oWord.GetActiveDocument();

    if(!this->m_szOutputFilename.IsEmpty())
    {
        //If we are outputting the document to a file then we need to print a single page at a time
        //This is because of a limitation on the ikonprinter/requisition printer side that can only handle
        //one page at a time
        // Print out to file
        CString szCurrentPage, szPrintRange;
        szCurrentPage.Format("%d", pageNumber);
        szPrintRange.Format("%d", PRINT_FROM_TO);  //PRINT_FROM_TO is #define PRINT_FROM_TO 3

        COleVariant printRange(szPrintRange, VT_BSTR);
        COleVariant currentPage(szCurrentPage, VT_BSTR);

        sprintf(m_szLogMessage, "Printing page %d of requisition", pageNumber);
        LogMessage(m_szLogMessage);

        oActiveDoc.PrintOut(covFalse,          // Background.
            covOptional,           // Append.
            printRange,             // Range.
            COleVariant(szFileName,VT_BSTR),  // OutputFileName.
            currentPage,           // From.
            currentPage,           // To.
            covOptional,           // Item.
            COleVariant((long)1),  // Copies.
            covOptional,           // Pages.
            covOptional,           // PageType.
            covTrue,               // PrintToFile.
            covOptional,           // Collate.
            covOptional,           // ActivePrinterMacGX.
            covOptional            // ManualDuplexPrint.
            );
    }
    else
    {
        // Print out to file
        oActiveDoc.PrintOut(covFalse,          // Background.
            covOptional,           // Append.
            covOptional,           // Range.
            COleVariant(szFileName,VT_BSTR),  // OutputFileName.
            covOptional,           // From.
            covOptional,           // To.
            covOptional,           // Item.
            COleVariant((long)1),  // Copies.
            covOptional,           // Pages.
            covOptional,           // PageType.
            covTrue,               // PrintToFile.
            covOptional,           // Collate.
            covOptional,           // ActivePrinterMacGX.
            covOptional            // ManualDuplexPrint.
            );
    }

    //Get the number of pages in the word document
    iNumPages=GetActiveDocPageCount();

    //omitted code
}
获取页面方法

int CWordComm::GetActiveDocPageCount()
{
    try
    {
        _Document oActiveDoc; 
        //Get the Active Document
        oActiveDoc = m_oWord.GetActiveDocument();

        //Get the BuiltinDocumentProperties collection for the 
        //document
        LPDISPATCH lpdispProps;
        lpdispProps = oActiveDoc.GetBuiltInDocumentProperties();

        //Get the requested Item from the BuiltinDocumentProperties 
        //collection
        //NOTE:  The DISPID of the "Item" property of a 
        //       DocumentProperties object is 0x0
        VARIANT vResult;
        DISPPARAMS dpItem;
        VARIANT vArgs[1];
        vArgs[0].vt = VT_BSTR;

        //property name for the number of pages in the active document
        _bstr_t btVal("Number of pages");

        vArgs[0].bstrVal = btVal;
        dpItem.cArgs=1;
        dpItem.cNamedArgs=0;
        dpItem.rgvarg = vArgs;
        HRESULT hr = lpdispProps->Invoke(0x0, IID_NULL, 
            LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, 
            &dpItem, &vResult, NULL, NULL);

        //Get the Value property of the BuiltinDocumentProperty
        //NOTE:  The DISPID of the "Value" property of a 
        //       DocumentProperty object is 0x0
        DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
        LPDISPATCH lpdispProp;
        lpdispProp = vResult.pdispVal;
        hr = lpdispProp->Invoke(0x0, IID_NULL, LOCALE_USER_DEFAULT, 
            DISPATCH_PROPERTYGET, &dpNoArgs, &vResult, 
            NULL, NULL);

        CString sPropValue = "";
        switch (vResult.vt)
        {
        case VT_BSTR:
            sPropValue = vResult.bstrVal;
            break;
        case VT_I4:
            sPropValue.Format("%d",vResult.lVal);
            break;
        case VT_DATE:
            {
                COleDateTime dt (vResult);
                sPropValue = dt.Format(0, LANG_USER_DEFAULT);
                break;
            }
        default:
//          sPropValue = "<Information for the property you selected is not available>";
            sPropValue = "-4";
            break;
        }

        //Release the no longer needed IDispatch pointers
        lpdispProp->Release();
        lpdispProps->Release();

        return atoi(sPropValue);

    }
    catch(...)
    {
        return FUNC_ERROR;
    }
}
int-CWordComm::GetActiveDocPageCount()
{
尝试
{
_文件OAC;
//获取活动文档
oActiveDoc=m_oWord.GetActiveDocument();
//获取的内置文档属性集合
//文件
lpdispProps;
lpdispProps=oActiveDoc.GetBuiltinoDocumentProperties();
//从内置文档属性获取请求的项
//收藏
//注意:的“Item”属性的DISPID
//DocumentProperties对象是0x0
变异vResult;
disparams dpItem;
变异变形金刚[1];
变量[0]。vt=vt\u BSTR;
//活动文档中页数的属性名称
_bstr_t btVal(“页数”);
vArgs[0].bstrVal=btVal;
dpItem.cArgs=1;
dpItem.cNamedArgs=0;
dpItem.rgvarg=vArgs;
HRESULT hr=lpdispProps->Invoke(0x0,IID_NULL,
区域设置\u用户\u默认值,分派\u属性设置,
&dpItem,&vResult,NULL,NULL);
//获取内置文档属性的Value属性
//注意:的“Value”属性的DISPID
//DocumentProperty对象是0x0
DISPPARAMS dpNoArgs={NULL,NULL,0,0};
lpdispProp;
lpdispProp=vResult.pdispVal;
hr=lpdispProp->Invoke(0x0,IID\u NULL,区域设置\u用户\u默认值,
调度财产组、DPNOARG和vResult,
空,空);
CString sPropValue=“”;
开关(vResult.vt)
{
案例VT\u BSTR:
sPropValue=vResult.bstrVal;
打破
案例VT_I4:
sPropValue.Format(“%d”,vResult.lVal);
打破
案件日期:
{
COleDateTime dt(vResult);
sPropValue=dt.Format(0,语言用户默认);
打破
}
违约:
//sPropValue=“”;
sPropValue=“-4”;
打破
}
//释放不再需要的IDispatch指针
lpdispProp->Release();
lpdispProps->Release();
返回atoi(sPropValue);
}
捕获(…)
{
返回函数错误;
}
}
问题:
这里有什么明显的错误吗?如果我想打印一系列页面,我是否应该以不同的方式与word交互?

如果它在调试模式下工作正常,我建议您将值(例如页数、正在打印的页面等)写入文本文件。比较调试日志输出与发布日志输出。这里有一个可能对你有帮助的方法。链接不是C++的,但是可以用正确的方式来指导你。

,这是一个定时问题。在打印文档之后和计算页数之前,我睡了500毫秒,现在我一直得到正确的结果

当我调试时,它基本上是在做同样的事情