簡體   English   中英

嘗試使用excel vba從html顯示圖像

[英]Try to display image from html using excel vba

我正在使用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制作的,guyz可以正常工作,但問題是,如果僅從原始圖片復制該圖片並將其保存到使用此代碼的EP \\ Player Image Folder中。 它沒有顯示。 也許我的復制代碼有問題嗎?

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

嘿,如果您查看生成頁面的來源,您應該會在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

好的,我已經研究了源代碼,嘗試對它進行修改,但是似乎沒有做任何事情。 我嘗試將其復制到文本文件中並另存為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM