I am currently attempting to use VBScript to perform batch modifications to HTML files. To do this, I'm using the Microsoft.XMLDOM object. It is failing to load my HTML file as an XML document. After some experimenting, it appears that the following tag being at the first line is the culprit:
<!DOCTYPE html>
If this line is removed, my script will work as expected. If this line is included, it will not load. No particular error message appears, but attempting to get anything out of the XMLDOM object will return nothing, which is the same behavior when the file the object attempts to load doesn't exist.
Does anyone know why this occurs and how to work around this? I cannot remove this tag from my files as they are HTML documents and they are routinely regenerated by another application.
Here is a sample of my code:
strFilePath = WScript.Arguments(0)
strTitlePrefix = WScript.Argument(1)
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.Async = False
objXMLDoc.load(strFilePath)
Set objDoc = objXMLDoc.documentElement
Set objNodes = objDoc.selectNodes("//title")
For Each thisNode in objNodes
OriginalTitle = thisNode.text
NewTitle = TitlePrefix & OriginalTitle
thisNode.text = NewTitle
Next
It fails at this line:
Set objNodes = objDoc.selectNodes("//title")
This is the error message:
Microsoft VBScript runtime error: Object required: 'objDoc'
The code does what I expect it to do if I remove the tag at the top of the document it's trying to read, so I know that the problem is that this tag causes it to think the file is not an XML document.
Microsoft.XMLDOM
is deprecated, useMsxml2.DOMDocument.6.0
instead. For further help with your code: please show your code. – Ansgar Wiechers