Let's say I have a page as follows saved in c: \ temp \ html_page.html:
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="xxx1">
<img src="test.png">
</div>
</body>
</html>
I would like to programmatically configure the src attribute for img based on Excel and VBA data. Basically, a way to find a div with Xpath and configure the (single) img tag that it contains.
I found an example for manipulating XML using VBA through the XML library here , but I crunched my head around creating this work with an HTML object library; cannot find any examples and / or documentation.
Dim XDoc As Object, root As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
If XDoc.Load(html_path) Then
Debug.Print "Document loaded"
Else
Dim strErrText As String
Dim xPE As MSXML2.IXMLDOMParseError
' Obtain the ParseError object
Set xPE = XDoc.parseError
With xPE
strErrText = "Your XML Document failed to load" & _
"due the following error." & vbCrLf & _
"Error #: " & .ErrorCode & ": " & xPE.reason & _
"Line #: " & .Line & vbCrLf & _
"Line Position: " & .linepos & vbCrLf & _
"Position In File: " & .filepos & vbCrLf & _
"Source Text: " & .srcText & vbCrLf & _
"Document URL: " & .URL
End With
MsgBox strErrText, vbExclamation
All I want to do is:
'...
Set outer_div = XDoc.SelectFirstNode("//div[id='xxx1'")
... edit the img attribute
But I can not load the HTML page because it is not valid XML (the img tag is not closed).
Any help is appreciated. Oh, and I can't use other languages ββlike Python, bummer.