下面是TEmbeddedWB实现的Exec方法。
function TEmbeddedWB.Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
const vaIn: OleVariant; var vaOut: OleVariant): HResult;
var
FCancel, FContinueScript, FShowDialog: Boolean;
pEventObj: IHTMLEventObj;
function GetProperty(const PropName: WideString): OLEVariant;
var
Dispparams: TDispParams;
Disp, Status: Integer;
ExcepInfo: TExcepInfo;
PPropName: PWideChar;
begin
Dispparams.rgvarg := nil;
Dispparams.rgdispidNamedArgs := nil;
Dispparams.cArgs := 0;
Dispparams.cNamedArgs := 0;
PPropName := PWideChar(PropName);
Status := pEventObj.GetIDsOfNames(GUID_NULL, @PPropname, 1,
LOCALE_SYSTEM_DEFAULT, @Disp);
if Status = 0 then
begin
Status := pEventObj.Invoke(disp, GUID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
Dispparams, @Result, @ExcepInfo, nil);
if Status <> 0 then DispatchInvokeError(Status, ExcepInfo);
end else
if Status = DISP_E_UNKNOWNNAME then
raise EOleError.CreateFmt('''%s'' not supported.', [PropName])
else
OleCheck(Status);
end;
begin
if (CmdGroup = nil) then
begin
Result := OLECMDERR_E_UNKNOWNGROUP;
exit;
end;
Result := OLECMDERR_E_NOTSUPPORTED;
if (ncmdID = OLECMDID_ONUNLOAD) and IsEqualGuid(cmdGroup^, CGID_EXPLORER) and
Assigned(FOnUnload) then FOnUnload(Self);
if IsEqualGuid(cmdGroup^, CGID_DocHostCommandHandler) then
begin
if Assigned(FOnRefresh) and ((nCmdID = 6041 {F5}) or (nCmdID = 6042 {ContextMenu}) or (nCmdID = 2300)) then
begin
FCancel := False;
FOnRefresh(self, nCmdID, FCancel);
if FCancel then Result := S_OK;
end else
case nCmdID of
OLECMDID_SHOWSCRIPTERROR:
if Assigned(FOnScriptError)
then begin
pEventObj := (Document as IHTMLDocument2).parentWindow.event;
if pEventObj <> nil then
begin
FContinueScript := True;
FShowDialog := True;
FOnScriptError(self,
GetProperty('errorline'),
GetProperty('errorCharacter'),
GetProperty('errorCode'),
GetProperty('errorMessage'),
GetProperty('errorUrl'),
FContinueScript, FShowDialog);
TVariantArg(vaOut).vt := VT_BOOL;
TVariantArg(vaOut).vbool := FContinueScript;
if not FShowDialog then Result := S_OK;
end;
end;
end;
end;
end;
注意其中触发OnScriptError事件的语句,你只需要编写OnScriptError事件处理函数就行了。
procedure TMainForm.EmbeddedWBScriptError(Sender: TObject; ErrorLine, ErrorCharacter, ErrorCode, ErrorMessage, ErrorUrl: string;
var ContinueScript: Boolean; var Showdialog: Boolean);
begin
ContinueScript :=true;//继续执行脚本,和你点按钮一样
Showdialog :=false;//不显示脚本错误对话框
end;
That's it.
Good Luck.