title: Read and manipulate HTML with Excel VBA
body: <p>Let's say I have a page as follows, saved at c:\temp\html_page.html:</p>
  
  <pre><code>&lt;html&gt;
     &lt;head&gt;
        &lt;link rel="stylesheet" href="styles.css"&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;div id="xxx1"&gt;
           &lt;img src="test.png"&gt;
        &lt;/div&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I'd like to programatically adjust the src attribute of the img, based on Excel data &amp; VBA. Basically a way to find the div with Xpath, and adjust the (single) img tag that is contained in it.</p>
  
  <p>I found an example for manipulating XML with VBA through the XML library <a href="http://excel-macro.tutorialhorizon.com/vba-excel-update-xml-file/" rel="nofollow noreferrer">here</a>, but I've been crunching my head around making this work with the HTML object library; can't find any examples and/or documentation.</p>
  
  <pre><code>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" &amp; _
           "due the following error." &amp; vbCrLf &amp; _
           "Error #: " &amp; .ErrorCode &amp; ": " &amp; xPE.reason &amp; _
           "Line #: " &amp; .Line &amp; vbCrLf &amp; _
           "Line Position: " &amp; .linepos &amp; vbCrLf &amp; _
           "Position In File: " &amp; .filepos &amp; vbCrLf &amp; _
           "Source Text: " &amp; .srcText &amp; vbCrLf &amp; _
           "Document URL: " &amp; .URL
      End With
      MsgBox strErrText, vbExclamation
  </code></pre>
  
  <p>All I want to do is:</p>
  
  <pre><code>'...
  Set outer_div = XDoc.SelectFirstNode("//div[id='xxx1'")
  ... edit the img attribute
  </code></pre>
  
  <p>But I can't load the HTML page, because it's not proper XML (img tag isn't closed). </p>
  
  <p>Any help is greatly appreciated. Oh, and I can't use other languages such as Python, bummer. </p>