Programming Delphi How to add alternative text to an image in TWebBrowser in Delphi

This example shows how to use the execCommand and pasteHTML method to put an alternative text to an image in TWebBrowser control in Delphi.

procedure TForm1.Button1Click(Sender: TObject);
  HtmlDoc: IHTMLDocument2;
  HtmlSelObj: IHTMLSelectionObject;
  HtmlTxtRng: IHTMLTxtRange;
  HtmlDoc := WebBrowser1.Document as IHTMLDocument2;
  HtmlDoc.execCommand('InsertImage', False, '');
  HtmlSelObj := HtmlDoc.selection;
  if HtmlSelObj.type_ = 'Control' then
  HtmlTxtRng := HtmlSelObj.createRange as IHTMLTxtRange;
  HtmlTxtRng.pasteHTML('This is the alternative text ');

Note that the function HtmlDoc.execCommand('InsertImage', False, ''), when the second parameter is set to true, the TWebBrowser document will prompt a dialog for select an image to insert.

Add comment

Security code

Programming - Delphi