多線程操裏操做webbrowser的 Frames 有這樣一個函數,直接在窗體中運行正常,但放到線程裏一到frames就報錯 procedure TForm1.fillframese1(webbrowser:TWebBrowser;params:TStrings;frameindex:integer); var Tabi:Integer; doc:IHTMLDocument2; Collection:IHTMLElementCollection; Element:IHTMLElement; frames:IDispatch; framew:IHTMLWindow2; i:OleVariant; s:string; ii,divi:Integer; input:IHTMLInputElement; docii:HTMLDocument; iframe:IWebBrowser; begin try doc:=Webbrowser.document as IHTMLDocument2; i:=6; doc.all ; frames:=doc.frames.item(i); framew:=frames as IHTMLWindow2; docii:=framew.document as HTMLDocument; collection:=docii.all.tags('iframe') as IHTMLElementCollection; //ShowMessage(IntToStr(Collection.length)); for ii:= 0 to collection.length -1 do begin iframe:= Collection.item(ii,varEmpty) as IWebBrowser; //在網上查過很多文章,最後本身嘗試成功,關鍵就是這裏。 doc:=iframe.document as IHTMLDocument2; Element:=doc.body as IHTMLElement; end; //總結:獲取Webbrowser的iframe的內容,須要把iframe元索強制轉換爲iwebbrowser接口, //以後,還要強制轉換爲ihtmlelement接口才能夠成功訪問iframe裏面的元素,不然程序運行出錯; except on e:exception do begin showmessage('fillframese1: '+params.Names[i]+' :'+e.Message); end; end; end; 線程外正常,但放到IdTCPServer.Execute裏調用,執行到這句就報錯frames:=doc.frames.item(i); 而且已經加上 CoInitialize(nil); OleInitialize(nil); 不知道你們有沒有遇到過,有什麼解決方法?