Tech Support > Microsoft Windows > Development Resources > How to print XSL formatted XML in MFC using IWebBrowser2
How to print XSL formatted XML in MFC using IWebBrowser2
Posted by Paresh on January 12th, 2005


Hello All,

I am having an XML file which uses XSL for formatting while showing in
IE. But now I want to print that XML (With XSL formatting)
programaticaly without opening in IE.

Any Help will be highly appreciated.


Thanks and Regards,

Paresh.

Posted by Alex Blekhman on January 12th, 2005


Paresh wrote:
"Printing Pages with the WebBrowser Control"
http://msdn.microsoft.com/workshop/b...bbrowser.asp?f
rame=true#Print_page



Posted by paresh_chitte2k@rediffmail.com on January 13th, 2005



Alex Blekhman wrote:
Hello Alex,
I did but could not get the result as expected. Following is the code
snippet for this::

CRect client;
RECT rect;
rect.left = rect.top = rect.right = rect.bottom = 0;
GetClientRect(&client);
if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, NULL,
WS_VISIBLE|WS_CHILD, rect, this, AFX_IDW_PANE_FIRST))
{
TRACE("Can't create Web Browser control window.\n");
//return FALSE;
}

IUnknown *pUnk=m_wndBrowser.GetControlUnknown();
ASSERT(pUnk);

HRESULT hr=pUnk->QueryInterface(IID_IWebBrowser2, (void
**)&pBrowser);
if (!SUCCEEDED(hr))
{
TRACE("WebBrowser interface not supported.\n");
//return FALSE;
}

//CString url("C:\\temp\\JP\\MQCReport.xml");
BSTR bUrl=strFilePath.AllocSysString();
hr=pBrowser->Navigate(bUrl, &COleVariant((long)0, VT_I4),
&COleVariant((LPCTSTR)NULL, VT_BSTR), NULL,
&COleVariant((LPCTSTR)NULL, VT_BSTR));
if (!SUCCEEDED(hr))
{
AfxMessageBox("can't browse!!");
//return FALSE;
}

//::Sleep(1000);

READYSTATE state;
hr = pBrowser->get_ReadyState(&state);

VARIANT_BOOL bIsVisible;
pBrowser->get_Visible(&bIsVisible);

// READYSTATE state = READYSTATE_UNINITIALIZED;
/* while(state != READYSTATE_LOADED )
{
pBrowser->get_ReadyState(&state);
}
*/

// HRESULT hr1 = pBrowser->ExecWB(OLECMDID_PRINT,
OLECMDEXECOPT_DONTPROMPTUSER, 0, 0);
/* LPDISPATCH lpDispatch = NULL;
LPOLECOMMANDTARGET lpOleCommandTarget = NULL;

//::Sleep(10000);

HRESULT hr1 = pBrowser->get_Document(&lpDispatch);
ASSERT(lpDispatch);

lpDispatch->QueryInterface(IID_IOleCommandTarget,
(void**)&lpOleCommandTarget);
ASSERT(lpOleCommandTarget);

lpDispatch->Release();

// Print contents of WebBrowser control.
// hr1 = lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, 0,
NULL,NULL);
// hr = pBrowser->get_ReadyState(&state);
hr1 = lpOleCommandTarget->Exec(NULL, OLECMDID_PRINTPREVIEW, 0,
NULL,NULL);
// hr = pBrowser->get_ReadyState(&state);
lpOleCommandTarget->Release();
*/



But READYSTATE_UNINITIALIZED never changes to READYSTATE_LOADED which
is expected. I think page is not loaded completely. How do I resolve
this problem. Help is appreciated.

Regards,

Paresh.


Posted by Alex Blekhman on January 13th, 2005


paresh_chitte2k@rediffmail.com wrote:
Here is small sample that works for me:

////////////////////////////////////////////////////////////////
//
#define STRICT

#define _WIN32_WINNT 0x0500 // Win2000
#define _WIN32_IE 0x0600 // IE6

#include <windows.h>

#include <tchar.h>
#include <stdio.h>
#include <docobj.h>

#import <shdocvw.dll>


int _tmain(int argc, _TCHAR* argv[])
{
::OleInitialize(NULL);

try
{
SHDocVw::IWebBrowserAppPtr ptrWebBrowserApp(
__uuidof(SHDocVw::InternetExplorer));

SHDocVw::IWebBrowser2Ptr ptrWebBrowser =
ptrWebBrowserApp;

_variant_t vtDummy;
HRESULT hr;

hr = ptrWebBrowser->Navigate(
L"V:\\bin_d\\1.xml",
&vtDummy, &vtDummy, &vtDummy, &vtDummy);

while(ptrWebBrowser->Busy == VARIANT_TRUE)
{
::Sleep(3000);
}

IOleCommandTargetPtr ptrOleCmdTarget =
ptrWebBrowser->Document;

hr = ptrOleCmdTarget->Exec(NULL, OLECMDID_PRINT,
OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
}
catch(_com_error& e)
{
_ftprintf(stderr,
_T("Error: 0x%08X (%s); Source: %ls; Desc: %ls"),
e.Error(), e.ErrorMessage(),
LPCWSTR(e.Source()), LPCWSTR(e.Description())
);
}

::OleUninitialize();

return 0;
}

////////////////////////////////////////////////////////////////



Posted by paresh_chitte2k@rediffmail.com on January 28th, 2005


Thanks Alex ! Now it works for me.

Thank you very much.


Similar Posts