下記のように C1データ というテーブルの 商品画像URL 1 というフィールドの値を
URL として受け取りフォーム上で表示したいのですが、レコードを移動しても最初の画像が表示され更新されません。
更新された URL の値を WebBrowser に渡すにはどうしたらよいのでしょうか。お願いいたします。
Private Sub Form_Current()
Dim oRS As DAO.Recordset
Dim URL As Variant
With Application.CurrentDb
Set oRS = .OpenRecordset("C1 データ", dbOpenDynaset)
URL = oRS.Fields("商品画像URL 1").Value
oRS.Close
Set oRS = Nothing
End With
End Sub
Private Sub WebBrowser4_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If URL <> "about:blank" Then
pDisp.Document.write "<head>" & _
(中略)
"<img src='" & URL & "'>" & _
"</body>"
End If
End Sub
WebBrowser コントロールの Document プロパティから IMG 要素を取得して src 属性を変更します。
例えば、IMG に id 属性を設定して、getElementById メソッドで設定する場合は、こんな感じになると思います。
URL = oRS.Fields("商品画像URL 1").Value oRS.Close Set oRS = Nothing Set Img = WebBrowser4.Document.getElementById("goods_image") Img.setAttribute("src", URL) ... pDisp.Document.write "<head>" & _ (中略) "<img id='goods_image' src='" & URL & "'>" & _
【参考URL】
インターフェースの方が取られるのかなあ。
2016/01/02 17:46:08HtmlElement ではなく、IHtmlElement だとどうなりますか?
Dim Img As IHTMLElement
2016/01/02 22:56:20にするとコンパイルエラーはなくなりますが
Img.setAttribute("src", URL)
のところで構文エラーになるようです。
Private Sub Form_Current()
Dim oRS As DAO.Recordset
Dim URL As Variant
Dim Img As IHTMLElement
With Application.CurrentDb
Set oRS = .OpenRecordset("C1 カラデータ", dbOpenDynaset)
URL = oRS.Fields("商品画像URL 1").Value
oRS.Close
Set oRS = Nothing
Set Img = WebBrowser4.Document.getElementById("goods_image")
Img.setAttribute("src", URL)
End With
End Sub
Private Sub WebBrowser4_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If URL <> "about:blank" Then
pDisp.Document.write "<head>" & _
"" & _
"</head>" & _
"<body>" & _
"" & _
"</body>"
WebBrowser4.Document.body.Scroll = "no"
End If
End Sub