Search code examples
delphifontstwebbrowser

How to Programmatically Change Font in TWebBrowser


We are building an email client using a web browser in design mode. Our clients have their own specific font that they want to use. So we would like the ability to set the web browser font using the font they give us. The font they give us is just a string eg "Arial"

How can we set the browser font?

We have tried the following:

HtmlPage.execCommand('FONTNAME', False, 'Arial');

However this only does it for the selection and as the web browser is empty to start with there is no selection. As soon as the user starts typing the font changes back to times new roman.

I have also tried

HtmlPage := ((Sender as TWebBrowser).Document  as IHTMLDocument2);
while (Sender as TWebBrowser).QueryStatusWB( OLECMDID_SELECTALL) <> OLECMDF_SUPPORTED + OLECMDF_ENABLED do
        Application.ProcessMessages;
(Sender as TWebBrowser).ExecWB(OLECMDID_SELECTALL,OLECMDEXECOPT_PROMPTUSER,pvaIn,pvaOut);
HtmlPage.execCommand('FONTNAME', False, 'Arial');

However, this is an infinite loop.

Any Suggestions would be appreciated :) Thanks.


Solution

  • What about this?

    ((Sender as TWebBrowser).Document as IHTMLDocument2).body.style.fontFamily:='Arial';