Board index » cppbuilder » Saving a Web Page from TCcpbrowser without a dialog

Saving a Web Page from TCcpbrowser without a dialog


2005-03-24 11:24:44 PM
cppbuilder84
I am trying to save a Web page from tccpbrowser. If I use the code below, it
works fine, but explorer handles it and pops up a dialog box. I also have
code below that which will display the source, and then can be saved. My
question is how do I save the complete page to include images, without
showing a dialog box to the user?
Thanks,
ba
PROMPTS USER WITH DIALOG BOX:
AnsiString FileName = ExtractFilePath ( ParamStr (0) )
+ "ExecWB.OLECMDID_SAVEAS.html";
char filename [MAXPATH];
strcpy ( filename, FileName.c_str() );
TVariant Variant = filename;
CppWebBrowser1->ExecWB(
Shdocvw_tlb::OLECMDID_SAVEAS,
Shdocvw_tlb::OLECMDEXECOPT_DODEFAULT, &Variant );
DISPLAYS HTML SOURCE:
AnsiString Source = "";
IHTMLDocument2 *htm = NULL; // #include <mshtml.h>
if(CppWebBrowser1->Document&&SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&htm)))
{IPersistStreamInit *spPsi = NULL; // ocidl.h
if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit,(LPVOID*)&spPsi))
&& spPsi)
{IStream *spStream = NULL; // objidl.h
OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
if(spStream)
{__int64 nSize = 0;
STATSTG ss;
LARGE_INTEGER nMove;
nMove.QuadPart = 0;
OleCheck(spPsi->Save(spStream, true));
OleCheck(spStream->Seek(nMove,STREAM_SEEK_SET,(ULARGE_INTEGER
*)&nSize));
OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
nSize = ss.cbSize.QuadPart;
Source.SetLength(nSize);
OleCheck(spStream->Read((void *)Source.data(),
nSize,(ULONG*)&nSize));
OleCheck(spStream->Release());
}
spPsi->Release();
}
htm->Release();
}
 
 

Re:Saving a Web Page from TCcpbrowser without a dialog

The following code is what I want to do, but I don't know enough about
Pascal to convert it to C++. Any sharp Delphi coders willing to help out?
Thanks
uses CDO_TLB, ADODB_TLB;
...
procedure WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName);
var
Msg: IMessage;
Conf: IConfiguration;
Stream: _Stream;
URL : widestring;
begin
if not Assigned(WB.Document) then Exit;
URL := WB.LocationURL;
Msg := CoMessage.Create;
Conf := CoConfiguration.Create;
try
Msg.Configuration := Conf;
Msg.CreateMHTMLBody(URL, cdoSuppressAll, '', '');
Stream := Msg.GetStream;
Stream.SaveToFile(FileName, adSaveCreateOverWrite);
finally
Msg := nil;
Conf := nil;
Stream := nil;
end;
end; (* WB_SaveAs_MHT *)
"murone" < XXXX@XXXXX.COM >wrote in message
Quote
I am trying to save a Web page from tccpbrowser. If I use the code below,
it works fine, but explorer handles it and pops up a dialog box. I also
have code below that which will display the source, and then can be saved.
My question is how do I save the complete page to include images, without
showing a dialog box to the user?

Thanks,

ba

PROMPTS USER WITH DIALOG BOX:
AnsiString FileName = ExtractFilePath ( ParamStr (0) )
+ "ExecWB.OLECMDID_SAVEAS.html";

char filename [MAXPATH];

strcpy ( filename, FileName.c_str() );

TVariant Variant = filename;

CppWebBrowser1->ExecWB(
Shdocvw_tlb::OLECMDID_SAVEAS,
Shdocvw_tlb::OLECMDEXECOPT_DODEFAULT, &Variant );



DISPLAYS HTML SOURCE:
AnsiString Source = "";

IHTMLDocument2 *htm = NULL; // #include <mshtml.h>



if(CppWebBrowser1->Document&&SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&htm)))
{IPersistStreamInit *spPsi = NULL; // ocidl.h

if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit,(LPVOID*)&spPsi))
&& spPsi)
{IStream *spStream = NULL; // objidl.h
OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
if(spStream)
{__int64 nSize = 0;
STATSTG ss;
LARGE_INTEGER nMove;
nMove.QuadPart = 0;
OleCheck(spPsi->Save(spStream, true));
OleCheck(spStream->Seek(nMove,STREAM_SEEK_SET,(ULARGE_INTEGER
*)&nSize));
OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
nSize = ss.cbSize.QuadPart;

Source.SetLength(nSize);
OleCheck(spStream->Read((void *)Source.data(),
nSize,(ULONG*)&nSize));
OleCheck(spStream->Release());
}

spPsi->Release();
}
htm->Release();
}


 

Re:Saving a Web Page from TCcpbrowser without a dialog

For anyone interested, here is the solution...
文档标题:如何把一个网页存为一个单独的mht文件  作者: ccrun
关 键 字:网页,mht,outlook,COM,CDO,接口,CreateMHTMLBody,ccrun
本文转自 C++Builder 研究 -
www.ccrun.com/article/go.asp
#include "SHDocVw_OCX.h"
#include "cdoex.h"
#include "msado15.h"
using namespace CDO;
// 本文是ccrun(老妖)根据相关资料改的,在BCB 6.0 下成功编译并测试通过.
// 如果有不能正常运行的,可能需要在机器上安装有outlook.
void __fastcall SaveWholePage(BSTR bstrUrl, BSTR bstrFileName)
{
CoInitialize(NULL);
IMessage *pMsg;
IConfiguration *pConfig;
HRESULT hr = CoCreateInstance(__uuidof(Message), NULL,
CLSCTX_INPROC_SERVER, __uuidof(IMessage), (void**)&pMsg);
hr = CoCreateInstance(
__uuidof(Configuration),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IConfiguration),
(void**)&pConfig);
pMsg->put_Configuration(pConfig);
try<br>
{
pMsg->CreateMHTMLBody(
bstrUrl,
cdoSuppressNone,
WideString(""),
WideString(""));
}
catch(...)
{
ShowMessage("有错误发生!");
return;
}
ADOStream *pStream;
pMsg->GetStream(&pStream);
pStream->SaveToFile(bstrFileName, adSaveCreateOverWrite);
pMsg->Release();
pStream->Release();
CoUninitialize();
}
// 调用举例:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SaveWholePage(WideString("www.ccrun.com"),
WideString("C:\\123.mht"));
}
--------------------------------------------------------------------------------
Quote
>上篇文章: 利用Shell对象控制Windows系统
>下篇文章: Windows文件保护的应用
"murone" < XXXX@XXXXX.COM >wrote in message
Quote
The following code is what I want to do, but I don't know enough about
Pascal to convert it to C++. Any sharp Delphi coders willing to help out?
Thanks

uses CDO_TLB, ADODB_TLB;
...
procedure WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName);
var
Msg: IMessage;
Conf: IConfiguration;
Stream: _Stream;
URL : widestring;
begin
if not Assigned(WB.Document) then Exit;
URL := WB.LocationURL;

Msg := CoMessage.Create;
Conf := CoConfiguration.Create;
try
Msg.Configuration := Conf;
Msg.CreateMHTMLBody(URL, cdoSuppressAll, '', '');
Stream := Msg.GetStream;
Stream.SaveToFile(FileName, adSaveCreateOverWrite);
finally
Msg := nil;
Conf := nil;
Stream := nil;
end;
end; (* WB_SaveAs_MHT *)
"murone" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>I am trying to save a Web page from tccpbrowser. If I use the code below,
>it works fine, but explorer handles it and pops up a dialog box. I also
>have code below that which will display the source, and then can be saved.
>My question is how do I save the complete page to include images, without
>showing a dialog box to the user?
>
>Thanks,
>
>ba
>
>PROMPTS USER WITH DIALOG BOX:
>AnsiString FileName = ExtractFilePath ( ParamStr (0) )
>+ "ExecWB.OLECMDID_SAVEAS.html";
>
>char filename [MAXPATH];
>
>strcpy ( filename, FileName.c_str() );
>
>TVariant Variant = filename;
>
>CppWebBrowser1->ExecWB(
>Shdocvw_tlb::OLECMDID_SAVEAS,
>Shdocvw_tlb::OLECMDEXECOPT_DODEFAULT, &Variant );
>
>
>
>DISPLAYS HTML SOURCE:
>AnsiString Source = "";
>
>IHTMLDocument2 *htm = NULL; // #include <mshtml.h>
>
>
>
>if(CppWebBrowser1->Document&&SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&htm)))
>{IPersistStreamInit *spPsi = NULL; // ocidl.h
>
>
>if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit,(LPVOID*)&spPsi))
>&& spPsi)
>{IStream *spStream = NULL; // objidl.h
>OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
>if(spStream)
>{__int64 nSize = 0;
>STATSTG ss;
>LARGE_INTEGER nMove;
>nMove.QuadPart = 0;
>OleCheck(spPsi->Save(spStream, true));
>OleCheck(spStream->Seek(nMove,STREAM_SEEK_SET,(ULARGE_INTEGER
>*)&nSize));
>OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
>nSize = ss.cbSize.QuadPart;
>
>Source.SetLength(nSize);
>OleCheck(spStream->Read((void *)Source.data(),
>nSize,(ULONG*)&nSize));
>OleCheck(spStream->Release());
>}
>
>spPsi->Release();
>}
>htm->Release();
>}
>
>


 

{smallsort}