0

我正在使用 excel vba。我想使用 excel vba 在 html 中插入图像。但它不显示图像。

PlyrName="Me"
PlyrPicLoc = "C:\EP\Player Image\asdf1234567894.jpg"

HTML = "<!DOCTYPE html>" & _
"<html>" & _
"<head>" & _
"<title>" & PlyrName & "'s Profile" & "</title>" & _
"</head>" & _
"<body>" & _
"<img src=" & PlyrPicLoc & " height='150' width='150'>" & _
"</body>" & _
"</html"> 

Set objIE = CreateObject("InternetExplorer.Application") With objIE
    .Navigate "about:blank"
    Do While .Busy: DoEvents: Loop
    Do While .ReadyState <> 4: DoEvents: Loop
    .Visible = True
    .Document.Write HTML End With
Set objIE = Nothing

2013 年 8 月 22 日更新

如果我要使用来自网络的原始图片或者我从 adobe/snip 制作的图片,它就可以工作,但问题是该图片是否仅从原始图片复制并使用此代码将其保存到 EP\Player 图像文件夹。它没有显示。也许我的复制代码有问题?

Private Sub cmdinsertpic_Click()
Dim fd As FileDialog
Dim objfl As Variant
Dim msg


Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
    .ButtonName = "Select"
    .AllowMultiSelect = False
    .Filters.Add "Image Files", "*.jpg;*.gif;*.bmp", 1
    .Title = "Choose Player's image"
    .InitialView = msoFileDialogViewDetails
    .Show
        For Each objfl In .SelectedItems
            FilNam = objfl
            Image1.Picture = LoadPicture(objfl)
            'Picturebox1.Image = Image.FromFile(OpenFileDalog.Filename)
        Next objfl
    On Error GoTo 0
End With

    'THIS WILL COPY THE PICTURE TO EP\Player Image Folder
    NameFile = Application.ThisWorkbook.Path & "\Player Image\" & Trim(txtnewplayername.Value & txtnewmc.Value) & ".gif"
    Call SavePicture(Image1.Picture, NameFile)

Set fd = Nothing

End Sub

例如,我复制了该原始图片并将其命名为asdf1234567894.gif并将保存到 EP\Player Image Folder

Private Sub LoadPic_Click()
Dim objIE As SHDocVw.InternetExplorer
PlyrPicLoc = "file:///C:/EP/Player%20Image/asdf1234567894.gif"
Const PlyrNames = "Me"
Dim FSObj As Scripting.FileSystemObject
Dim TStream As Scripting.TextStream

sPATH = "C:\EP\sample.html"
sURL = "C:/EP/sample.html"

shtml = "<body>" & _
"<title>" & PlyrNames & "'s Profile" & "</title>" & _
"<img src=" & Chr(34) & PlyrPicLoc & Chr(34) & " height='150' width='150'>" & _
"<body>" & _
"</body>" & _
"</html>"

Set FSObj = New Scripting.FileSystemObject
Set TStream = FSObj.CreateTextFile(sPATH, True)
TStream.WriteLine (shtml)
TStream.Close


Set objIE = CreateObject("InternetExplorer.Application")

With objIE
    .Navigate sURL
    Do While .Busy: DoEvents: Loop
    Do While .ReadyState <> 4: DoEvents: Loop
    .Visible = True
End With

Set objIE = Nothing
Set FSObj = Nothing
Set TStream = Nothing
End Sub
4

2 回答 2

0

嘿,如果您查看生成页面的源代码,您应该会看到 img 源代码周围缺少“。

尝试改变

"<img src=" & PlyrPicLoc & " height='150' width='150'>"

 "<img src=" & Chr(34) & PlyrPicLoc &  Chr(34) & " height='150' width='150'>" 

如果失败,您可以从生成的页面发布您的 html 源代码吗?

使用 PNG 和 JPG 和 IE9 测试和工作正常

PlyrName="Me"
PlyrPicLoc = "PATH TO PICTURE"

HTML = "<!DOCTYPE html>" & _
"<html>" & _
"<head>" & _
"<title>" & PlyrName & "'s Profile" & "</title>" & _
"</head>" & _
"<body>" & _
"<img src=" & PlyrPicLoc & " height='150' width='150'>" & _
"</body>" & _
"</html>" 

Set objIE = CreateObject("InternetExplorer.Application") 
With objIE
    .Navigate "about:blank"
    Do While .Busy: DoEvents: Loop
    Do While .ReadyState <> 4: DoEvents: Loop
    .Visible = True
    .Document.Write HTML 
End With
Set objIE = Nothing
于 2013-08-17T00:55:21.237 回答
0

好的,我已经研究了源代码,我尝试过修改它,但我所做的一切似乎都不起作用。我尝试将其复制到文本文件中并保存为 html,嘿,它工作正常。不幸的是我不知道为什么。我已经比较了文本文件的源代码和 excel 生成的代码,它们是相同的,但由于某种原因,打开文本文件并使用 .document.write 编写它没有。

作为一种解决方法,我使用了文件脚本对象来编写源代码的文本文件版本,然后让 Internet Explorer 对象导航到该版本。我使用的代码如下所示,您唯一需要更改的是图像和文本文件的地址。您不需要更改文本文件地址,因为文件脚本对象应该覆盖它。

要使其工作,您需要添加对 microsoft 脚本运行时和 microsoft Internet 控件的引用。

Sub image()
Dim objIE As SHDocVw.InternetExplorer
PlyrPicLoc = "file:///C:/Documents%20and%20Settings/All%20Users/Documents/My%20Pictures/Sample%20Pictures/Water%20lilies.jpg"
Const PlyrName = "Me"
Dim FSObj As Scripting.FileSystemObject
Dim TStream As Scripting.TextStream

sPATH = "E:\My Documents\StackOverflow\TestC.html"
sURL = "E:/My Documents/StackOverflow/TestC.html"

shtml = "<body>" & _
"<title>" & PlyrName & "'s Profile" & "</title>" & _
"<img src=" & Chr(34) & PlyrPicLoc & Chr(34) & " height='150' width='150'>" & _
"<body>" & _
"</body>" & _
"</html>"

Set FSObj = New Scripting.FileSystemObject
Set TStream = FSObj.CreateTextFile(sPATH, True)
TStream.WriteLine (shtml)
TStream.Close


Set objIE = CreateObject("InternetExplorer.Application")

With objIE
    .Navigate sURL
    Do While .Busy: DoEvents: Loop
    Do While .ReadyState <> 4: DoEvents: Loop
    .Visible = True
End With

Set objIE = Nothing
Set FSObj = Nothing
Set TStream = Nothing 



End Sub
于 2013-08-17T19:12:09.923 回答