I need to set the value of a Select field of a page that is loaded inside the TWebBrowser component in Delphi 7. This procedure I can do in other fields where the values are the same as the text of the options, but in another field as there are hundreds of values and the Select option text is different from the value, so I can not. The procedure I'm needing is like this below:
function SelectOptionByValue(const ADocument: IDispatch; const AElementID,
AOptionValue: WideString): Integer;
var
HTMLDocument: IHTMLDocument3;
HTMLElement: IHTMLSelectElement;
function IndexOfValue(const AHTMLElement: IHTMLSelectElement;
const AValue: WideString): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to AHTMLElement.length - 1 do
if (AHTMLElement.item(I, I) as IHTMLOptionElement).value = AValue then
begin
Result := I;
Break;
end;
end;
begin
Result := -1;
if Supports(ADocument, IID_IHTMLDocument3, HTMLDocument) then
begin
if Supports(HTMLDocument.getElementById(AElementID), IID_IHTMLSelectElement,
HTMLElement) then
begin
Result := IndexOfValue(HTMLElement, AOptionValue);
HTMLElement.selectedIndex := Result;
end;
end;
end;
The above procedure only works when the option text of the Select field is equal to the value of the option. For example:
SelectOptionByValue(web.Document, 'fNaturalidade', DMCadastros.cdsClientesUF.AsString);