开发者

How to Print the particular contents in the entire document using DHTML

In these example, I want to print the contents in the ID tab_form_1.While trying these sample to print the tab_form_1 contents,lpOleCommandTarget become NULL while query interface using CComptr .

How to solve this issue?

SAMMPLE

    IHTMLDocument2 *pDoc;
GetDHtmlDocument(&pDoc);


CComPtr<IHTMLElement2> spControl;       
if(GetElementInterface(_T("tab_form_1") ,&spControl) != S_OK)
    return; 

//ole command target
LPOLECOMMANDTARGET lpOleCommandTarget = NULL;
spControl->QueryInterface(IID_IOleCommandTarget,(void**)&lpOleCommandTarget);ASSERT(lpOleCommandTarget);
//lpDispatch->Release();

if(lpOleCommandTarget == NULL) return;

//prepare header
CString header   = _T("Project Path: &w");

VARIANT     header_variant;
VariantInit(&header_variant);
V_VT(&header_variant)   = VT_BSTR;
V_BSTR(&header_variant) = CString(header).AllocSysString();

//prepare footer
CString footer   = _T("&d &t&b Page&p of &P");

VARIANT     footer_variant;
VariantInit(&footer_variant);
V_VT(&footer_variant)   = VT_BSTR;
V_BSTR(&footer_variant) = CString(footer).AllocSysString();

//prepare header footer safe arrray
SAFEARRAYBOUND  parameter_array_bound[1];
SAFEARRAY       *parameter_array = NULL;

parameter_array_bound[0].cElements = 2;
parameter_array_bound[0].lLbound   = 0;

parameter_array = SafeArrayCreate(VT_VARIANT,1,parameter_array_bound)开发者_如何学Go;

//HRESULT hr;
long index;

index = 0;
HRESULT hr    = SafeArrayPutElement(parameter_array,&index,&header_variant);

index = 1;
hr    = SafeArrayPutElement(parameter_array,&index,&footer_variant);

VARIANT parameter;
VariantInit(&parameter);
V_VT(&parameter)    = VT_ARRAY | VT_BYREF;
V_ARRAY(&parameter) = parameter_array;

// print contents of web browser control
lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, &parameter,NULL);

//clear all variants
VariantClear(&header_variant);
VariantClear(&footer_variant);
if (parameter_array != NULL) {
    SafeArrayDestroy(parameter_array);
}

lpOleCommandTarget->Release();


Try this sample.This will eradicate your problem.This code will print the particular contents into the newly created document.

    // create DHtmlDocument for Newly Created Dialog
HRESULT hDispatch = printdlg->m_pBrowserApp->get_Document((IDispatch**)&pDisp);
if(hDispatch != S_OK) return;
HRESULT hResult = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pNewDoc);
if(hResult != S_OK) return;

//GetDHtmlDocument for new document
GetDHtmlDocument(&pDoc);

BSTR strSummaryText = GetElementHtml(L"tab_form_1");

// Creates a new one-dimensional array
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (psaStrings == NULL) {

    return;
}

VARIANT *param;
HRESULT hresult = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
param->vt = VT_BSTR;
param->bstrVal = strSummaryText;

hresult = SafeArrayUnaccessData(psaStrings);
//it will write the div part of tab_form_1 into new document to pass the summary contents alone to the printer
hresult = pNewDoc->write(psaStrings);


// SafeArrayDestroy calls SysFreeString for each strtemp
if (psaStrings != NULL) {
    SafeArrayDestroy(psaStrings);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜