IHTMLDocument2::execCommand Method--------------------------------------------------------------------------------Executes a command on the current document, current selection, or the given range.SyntaxHRESULT execCommand( BSTR cmdID, VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet );令cmdID = "SaveAs"就可以了,msdn里有详细的用法
{
/*
要保存HTML文档,通过使用ExecWB是不行的,原因可能是由于我们所操作的HTML文档是
由about:blank 创建的。因此,要打印文档,只有调用IHTMLDocument2的接口execCommand
来实现。
*/
IHTMLDocument2 * pIHtml;
LPDISPATCH lpDisp = GetHtmlDocument();
if (lpDisp == NULL || FAILED(lpDisp->QueryInterface(IID_IHTMLDocument2, (void **)&pIHtml)))
return;
try
{
VARIANT_BOOL vbRet;
VARIANT v;
v.vt = VT_BSTR;
v.bstrVal = SysAllocString(L"报表");
pIHtml->execCommand(SysAllocString(L"SaveAs"), VARIANT_TRUE, v, &vbRet);
}
catch(COleException * e)
{
e->ReportError();
e->Delete();
}
pIHtml->Release();
}
IHTMLDocument2::execCommand Method--------------------------------------------------------------------------------Executes a command on the current document, current selection, or the given range.SyntaxHRESULT execCommand( BSTR cmdID,
VARIANT_BOOL showUI,
VARIANT value,
VARIANT_BOOL *pfRet
);令cmdID = "SaveAs"就可以了,msdn里有详细的用法
{
CInternetSession m_Session("DigitalTitan");
CHttpFile* pFile=NULL;
CException* e; TCHAR szTempPath[MAX_PATH],szTempFile[MAX_PATH];
DWORD dwResult=::GetTempPath(MAX_PATH,szTempPath);
CString strURLPath;
GetTempFileName(szTempPath,_T("DigitalTitan_"),0,szTempFile);
strURLPath=szTempFile;
TRY
{
pFile=(CHttpFile*)m_Session.OpenURL(strURL);
}
CATCH_ALL(e)
{
pFile=NULL;
AfxMessageBox("URL地址不合法",MB_ICONINFORMATION);
return "";
}
END_CATCH_ALL if(pFile)
{
DWORD dwStatus;
DWORD dwBufLen=sizeof(dwStatus);
BOOL bSuccess=pFile->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,&dwStatus,&dwBufLen);
if(bSuccess&&dwStatus>=200&&dwStatus<300)
{
CStdioFile m_File;
if(m_File.Open(strURLPath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary))
{
BYTE pBuf[1024];
DWORD dwRead;
do
{
dwRead=pFile->Read(pBuf,1024);
m_File.Write(pBuf,dwRead);
}
while(dwRead>0);
m_File.Close();
}
}
pFile->Close();
delete pFile;
}
else
{
m_Session.Close();
}
return strURLPath;
}