Steps to Reproduce Behavior
1. | In Microsoft Visual Basic, create a Standard EXE project. |
2. | Add references to the Microsoft HTML Object Library and the Microsoft Internet Controls. |
3. | In the Load event of Form1, paste the following code to start Internet Explorer and navigate to a test page:
Dim WithEvents ie As InternetExplorer
Dim doc As IHTMLDocument
Private Sub Form_Load()
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "http://myserver/test.htm"
End Sub
|
4. | To replace the HTML code on the current page, add a button to the form, and then add the following code to the Click event:
Private Sub Command1_Click()
Dim strHTML As String
Set doc = ie.document
strHTML = "<html><body><DIV>hi<br>" & _
"<iframe src=testiframe.htm" & _
" width=200 height=70>" & _
"</iframe><DIV></body></html>"
doc.open
doc.write strHTML
doc.Close
End Sub
|
5. | Build the application. |
6. | Create a new file named Test.htm and paste the following code:
<html>
<body>
hi
</body>
</html>
|
7. | Create a new file named TestIFrame.htm and paste the following code:
<html>
<body>
This is my iframe.
</body>
</html>
|
8. | Execute the Visual Basic project and then click the button that you added. The address bar in Internet Explorer changes to about:blank, and the IFrame within it does not show the TestIFrame.htm file. The source of the IFrame is
<HTML>blanktestiframe.htm</HTML>
|
To work around this problem, insert script code into the HTML code and call it from the Automation client. You can use the following code in place of the
Command1_Click() procedure:
Private Sub Command1_Click()
Dim strScript As String
Set doc = ie.document
strScript = "<span style=" & Chr(34) & "display:none" & Chr$(34) & _
">h</span>" & _
"<script defer=true language=" & Chr(34) & _
"javascript" & Chr(34) & ">" & _
"function insertIFrame()" & "{" & _
" var alltext = " & Chr(34) & _
"<html><body>Written from VB" & _
"Automation<br>" & _
"<iframe src=\" & Chr(34) & "testiframe.htm\" & Chr(34) & _
" width=200 height=70>" & _
"</iframe></body></html>" & Chr(34) & ";" & _
" document.open();" & _
" document.write(alltext);" & _
" document.close();" & "}" & _
"</script>"
doc.documentElement.insertAdjacentHTML "beforeEnd", strScript
doc.parentWindow.execScript "insertIFrame()", "jscript"
End Sub