我正在使用 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