在InternetExplorer.Application中显示本地图片

标签: , , , ,

很久以前的问题了,一直没解决,今天无意中搜到了答案。

在InternetExplorer.Application对象中显示远程服务器上的图片是没有问题的:

Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "about:blank"
IE.Document.write "<img src='http://demon.tw/demon.gif' />"
IE.Visible = True

然而,在IE默认的设置下,却无法显示本地图片:

Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "about:blank"
IE.Document.write "<img src='D:/demon.gif' />"
IE.Visible = True

有人说要使用相对路径,有人说路径要用反斜杠

IE.Document.write "<img src='D:\demon.gif' />"

也有人说要加上协议名称

IE.Document.write "<img src='file:///D:/demon.gif' />"

还有人说加上协议名称并使用反斜杠

IE.Document.write "<img src='file:///D:\demon.gif' />"

我都试过了,没用,百思不得其解。

今天却无意中搜索到一段微软网站上的代码,终于豁然开朗:

Option Explicit
Dim objWshShell, objFSO, strVariableName, objExplorer, strScriptFileDirectory

Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strVariableName = "WhateverYouWant"
strScriptFileDirectory = objFSO.GetParentFolderName(wscript.ScriptFullName)

' Displays the Internet Explorer informational message window.
DisplayIEwindow

wscript.Sleep 4000

CloseIEwindow
Wscript.Quit


' ~$~----------------------------------------~$~
'            FUNCTIONS & SUBROUTINES
' ~$~----------------------------------------~$~
Sub DisplayIEwindow
' Executes an Internet Explorer window.
Dim strIEregistryKey, strCheckAssociationsKey
strIEregistryKey = "NotExist"

On Error Resume Next

' The following registry entry will enable local files such as .GIFs to be displayed using IE 7.0, but this setting will not take effect if the web browser is already open (there will be no error messages displayed if this is the case).
strIEregistryKey = objWshShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\blank\about")

wscript.echo strIEregistryKey

If strIEregistryKey = "NotExist" Then
    objWshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\blank\about", 2, "REG_DWORD"
 wscript.echo "escreve"
End If

' Attempts to temporarily prevent Internet Explorer checking to see if it is the default browser (storing the existing value to be restored after disaplying the script's IE window).
strCheckAssociationsKey = objWshShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\Main\Check_Associations")
objWshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Check_Associations", "no", "REG_SZ"

Set objExplorer = CreateObject ("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Left = 200
objExplorer.Top = 100
objExplorer.Width = 450
objExplorer.Height = 220
objExplorer.Visible = 1
objExplorer.Document.Body.Scroll = "no"
objExplorer.Document.Body.Style.Cursor = "wait"
objExplorer.Document.Title = "Window Title Name Goes Here" & String(50, ".")

objExplorer.Document.Body.InnerHTML = "<img src='file:///" & strScriptFileDirectory & "\pro.gif' align='left'> " & "<b><font size='4'><font color='#008000'>" & strVariableName & " . . .</font></b><br>This application is currently being installed, and may take up to 5 minutes to complete.<p align='center'><font size='3'><b>Installation Start Time:</b> " & Time & "</p></p><p align='center'><font size='2'>*closing this window will <u>not</u> abort the installation<font color='#808080'><br> (this window will close automatically after the process completes)</font></font>"

' Attempts to make the open IE window active.
WScript.Sleep 1000
objWshShell.AppActivate "Window Title Name Goes Here"
WScript.Sleep 1001

' Removes the added registry key setting used with IE 7.0 if it did not already exist.
If strIEregistryKey = "NotExist" Then
    objWshShell.RegDelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\blank\"
End If

' Restores the original Check_Associations registry setting from the script's stored value.
objWshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Check_Associations", strCheckAssociationsKey, "REG_SZ"
End Sub

' ~$~----------------------------------------~$~
Sub CloseIEwindow
' Attemps to close the Internet Explorer window if it was opened by this script.
On Error Resume Next
objExplorer.Quit
End Sub

代码里的注释写得很详细,我就不解释了。什么?看不懂英文?那你点参考链接进去看看原文吧,代码以外的部分都是葡萄牙语,作者不用葡萄牙写注释已经很不错了。

图片显示的问题解决后,VBS也可以写出漂亮的WEB界面了。

参考链接:Script InternetExplorer.Application + img scr

随机文章:

  1. OpenWrt实现WOL(Wake-on-LAN)网络唤醒
  2. .htaccess文件初探
  3. VBS中的Mod运算符与负数
  4. VBS Scripting.Dictionary字典对象按键名Key进行冒泡排序
  5. VBS中存在两种不同类型的数组?

6 条评论 发表在“在InternetExplorer.Application中显示本地图片”上

  1. PopEye说道:

    Demo,你提到的几个无法通过的代码中最后一个其实是可以通过的,不信你可以试试。不过我用的是InnerHTML赋值的方式,也许是这个原因?objIE.Document.Body.InnerHTML = ” “

  2. PopEye说道:

    XP+IE6,我的系统太古老,见笑了。

  3. 枫中残雪说道:

    确实如此,刚才试了一下,在IE8中只是显示了图片的图标,不显示原图。而将其保存为HTML再打开,就可以看见了,暂时有点乱:
    Set ie=CreateObject(“internetexplorer.application”)
    ie.Visible=True
    ie.Navigate “about:blank”
    With ie.Document
    .write “”
    .write “”
    .write “”
    End with
    Set ie=nothing

  4. 枫中残雪说道:

    不好意思,标记出了点错误,不过就是HTML,BODY,HEAD而以

留下回复