i am working in excel vba. i want to insert an image in html using excel vba. but it doesn't show the image.
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
UPDATE LAST AUGSUST 22 2013
guyz it's working if im going to use the original picture that came from the web or i made it from adobe/snip but the problem is if that picture is only copied from original one and save it to EP\\Player Image Folder using this code. it's not displaying. maybe there's something wrong wtih my code on copying?
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
For example i copied that orignal picture and name it as asdf1234567894.gif and will save to 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
Hey if you look at the source of generated page you should see the " are missing around your img source.
Try changing
"<img src=" & PlyrPicLoc & " height='150' width='150'>"
to
"<img src=" & Chr(34) & PlyrPicLoc & Chr(34) & " height='150' width='150'>"
If that fails can you post your html source from generated page?
TESTED AND WORKING OK WITH PNG AND JPG AND 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
Ok, I have investigated the source code, I have tried modifying it, but nothing I did seemed to work. I tried copying it into a text file and saving as html and hey presto it worked. Unfortunately I don't know why. I have compared the source code for the text file and code generated by excel and they are identical, but for some reason opening a text file works and writing it using .document.write did not.
As a work around I have used a file scripting object to write a text file version of the source code and then got the internet explorer object to navigate to that. the code I used is shown below the only things you should need to change are the addresses of the image and the text file. You should not need to change the text file adress as the file scripting object should overwrite it.
To make it work you will need to add references to microsoft scripting runtime and microsoft internet controls.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.