Adding page numbers to pdf through VBA and Acrobat IAC - vba

I am trying to do the following thing from Excel vba:
Export certain worksheets to pdf
Take an existing pdf document and insert it in the newly generated pdf at a specific place (not necessarily at the end or at the beginning)
Number the pages of the merged pdf, omitting two title pages
I already figured out the first step. For the second and third step, I have Adobe Acrobat XI Pro at my disposal. Since I want to do this in one go from vba, I have downloaded the Acrobat SDK. From some quick Googling, I think I should be able to figure out the second step now, using the IAC, but the third step (oddly) seems the most difficult. Any suggestions would be welcome.
Best,
NiH

In the meantime, I found a solution for adding page numbers. For anyone who might be interested, here's an example of how it can be done:
Sub addPageNumbers()
Dim acroApp As Acrobat.acroApp
Dim myDocument As Acrobat.AcroPDDoc
Dim jso As Object
Dim strPath As String
Dim strFileName As String
Dim intPages As Integer
Dim i As Integer
Set acroApp = CreateObject("AcroExch.App")
Set myDocument = CreateObject("AcroExch.PDDOc")
strPath = "C:\"
strFileName = "myDoc.pdf"
'Open file and load JSObject
Set myDocument = CreateObject("AcroExch.PDDOc")
myDocument.Open (strPath & strFileName)
Set jso = myDocument.GetJSObject
' get number of pages
intPages = myDocument.GetNumPages
'Write page numbers to all pages
For i = 1 To intPages
jso.addWatermarkFromText _
cText:=Str(i) & " ", _
nTextAlign:=1, _
nHorizAlign:=2, _
nVertAlign:=4, _
nStart:=i - 1, _
nEnd:=i - 1
Next i
'Save document
Call myDocument.Save(1, strPath & strFileName)
'Clean up
Set jso = Nothing
Call acroApp.CloseAllDocs
Set myDocument = Nothing
Call acroApp.Exit
Set acroApp = Nothing
End Sub
Keep in mind that you need to have Acrobat (not only the reader) installed on your computer, and the reference to Acrobat has to be enabled in the vba editor.
I did not add error handling; obviously you should.
More info on the addwatermarkFromText method can be found here
Best regards,
NiH

Here another method to do it. I use the add field method from acrobat js. The "ExecuteThisJavaScript" method has the advantage that you can use js without translation to js-object.
The following example - I published somewhere already - add Date, filename and pageNo as footer to a pdf. It's written in VBS but can also used as vba without changes.
Best regards, Reinhard
File = "D:\Test.pdf"
Set App = CreateObject("Acroexch.app") '//start acrobat
app.show '//show Acrobat or comment out for hidden mode
Set AVDoc = CreateObject("AcroExch.AVDoc")
Set AForm = CreateObject("AFormAut.App") '//get AFormAPI to execute js later
If AVDoc.Open(File,"") Then
'//write JS-Code on a variable
Ex = " // set Date, filename and PageNo as footer "&vbLF _
& " var Box2Width = 50 "&vbLF _
& " for (var p = 0; p < this.numPages; p++) "&vbLF _
& " { "&vbLF _
& " var aRect = this.getPageBox(""Crop"",p); "&vbLF _
& " var TotWidth = aRect[2] - aRect[0] "&vbLF _
& " { var bStart=(TotWidth/2)-(Box2Width/2) "&vbLF _
& " var bEnd=((TotWidth/2)+(Box2Width/2)) "&vbLF _
& " var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]); "&vbLF _
& " fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages; "&vbLF _
& " fp.textSize=6; fp.readonly = true; "&vbLF _
& " fp.alignment=""center""; "&vbLF _
& " } "&vbLF _
& " } "
'//Execute JS-Code
AForm.Fields.ExecuteThisJavaScript Ex
msgBox("Done")
end if
Set AVDoc = Nothing
Set APP = Nothing

Related

Optical Character Recognition from Access via VBA

I wish to OCR a few JPEGs (I can convert on the fly with iview).
I get:
Method 'OCR' of object 'IImage' failed
My code isn't perfect yet as I am focused on getting the .ocr method to function.
The images are photos and contain only a few characters. I could use a barcode reader, but those are hard to find free.
Public Function OCRtest(strTempImg)
pXname = "ocrTest"
On Error GoTo err_hand
Dim miDoc As Object
Dim miWord As MODI.Word
Dim strWordInfo As String
Set miDoc = CreateObject("MODI.Document")
miDoc.Create strTempImg
' Perform OCR.
miDoc.Images(0).ocr
' Retrieve and display word information.
Set miWord = miDoc.Images(0).Layout.Words(2)
strWordInfo = _
"Id: " & miWord.id & vbCrLf & _
"Line Id: " & miWord.LineId & vbCrLf & _
"Region Id: " & miWord.RegionId & vbCrLf & _
"Font Id: " & miWord.FontId & vbCrLf & _
"Recognition confidence: " & _
miWord.RecognitionConfidence & vbCrLf & _
"Text: " & miWord.Text
Set miWord = Nothing
Set miDoc = Nothing
OCRtest = strWordInfo
Return
Exit Function
err_hand:
Call CStatus(Error, 504, Err.Number, Err.description, strTempImg)
End Function
If you use MS Office 2010, you need install MODI firstly.
Then, you need to add reference to: Microsoft Office Document Imaging 1x.0 Type Library and you'll be able to use this code:
Sub OCRReader()
Dim doc1 As MODI.Document
Dim inputFile As String
Dim strRecText As String
Dim imageCounter As Integer
inputFile = Application.GetOpenFilename
strRecText = ""
Set doc1 = New MODI.Document
doc1.Create (inputFile)
doc1.OCR ' this will ocr all pages of a multi-page tiff file
For imageCounter = 0 To (doc1.Images.Count - 1) ' work your way through each page of results
strRecText = strRecText & doc1.Images(imageCounter).Layout.Text ' this puts the ocr results into a string
Next
fnum = FreeFile()
Open "C:\Test\testmodi.txt" For Output As fnum
Print #fnum, strRecText
Close #fnum
doc1.Close
End Sub
Above code comes from: https://www.mrexcel.com/forum/excel-questions/358499-read-data-tiff-file-using-modi-ocr-vba.html

Acrobat cannot read .FDF written with Excel VBA

I exported a PDF with form fields in FDF and wrote a sub to output another FDF verbatim, with cell values for the form field values. If I edit the FDF in a text editor and change the values, Acrobat can read the file just fine, but the file output with VBA throws an error:
Adobe could not open whatever.fdf because it is either not a supported file type or because the file has been damaged
I've tried two different types of line breaks, I've tried a similar sub with xfdf formatting which is slightly different with the same results.
Sub something()
Dim sht As Worksheet
Set sht = Sheets("owssvr")
Dim lastrow As Integer
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Dim x As Integer
For x = 2 To lastrow
Set Fileout = fso.CreateTextFile("C:\Users\blabla\" & x & ".fdf", True, True)
Fileout.Write "%FDF-1.2" & vbCrLf & _
"%âãÏÓ" & vbCrLf & _
"1 0 obj" & vbCrLf & _
"<</FDF<</F(MyDocument.pdf)/Fields[<</T(Adobe Form Field)/V(" & sht.Range("U" & x) & ")>>]/ID[<4ED54800AC4A3D41ABE4F4C7B12A3D23><609E705B7532334B8F914CFF4C09F2A0>]/UF(MyDocument.pdf)>>/Type/Catalog>>" & vbCrLf & _
"endobj" & vbCrLf & _
"trailer" & vbCrLf & _
"<</Root 1 0 R>>" & vbCrLf & _
"%%EOF" & vbCrLf
Fileout.Close
Next x
End Sub
Simply leave out the line: "%âãÏÓ" & vbCrLf & _". /ID and /UF keys not really needed.
Something like this should work:
Fileout.Write "%FDF-1.2" & vbCrLf & _
"1 0 obj<</FDF<<" & vbCrLf & _
"/F(MyDocument.pdf)" & vbCrLf & _
"/Fields" & vbCrLf & _
"[<</T(Adobe Form Field)/V(xyValue)>>]" & vbCrLf & _
">>>>" & vbCrLf & _
"endobj" & vbCrLf & _
"trailer" & vbCrLf & _
"<</Root 1 0 R>>" & vbCrLf & _
"%%EOF" & vbCrLf
You will find that in the Acrobat IAC documentation. Here a quick vbs(vba) example. Where you need only 2 lines from that: jso.getField and f.value =... Good luck.
'//-> Set a value for a form field via JSO
'//-> Settings
FileNm = "d:\TestInput.pdf"
FieldNm= "Input1"
FieldValue= "50"
'//-> let's start
Set App = CreateObject("Acroexch.app")
app.show
Set AVDoc = CreateObject("AcroExch.AVDoc")
'//-> open the file and put the value in
If AVDoc.Open(FileNM,"") Then
Set PDDoc = AVDoc.GetPDDoc()
Set jso = PDDoc.GetJSObject
'//-> Get the field and put a value in
set f = jso.getField(FieldNm)
f.value = FieldValue
end if
As much as I want to leave this open in hopes somebody figures out WHY VBA is screwing up the unicode, my problem can be solved without invoking fso at all and just using FreeFile and string replace on an original FDF
Sub blabla()
Dim objAcroApp As Acrobat.AcroApp
Dim objAcroAVDoc As Acrobat.AcroAVDoc
Dim objAcroPDDoc As Acrobat.AcroPDDoc
Dim jsObj As Object
Dim boResult As Boolean
Dim oldPDF As String
Dim NewFilePath As String
Dim sTemp As String
Dim iFileNum As Integer
Dim oldFDF As String
Dim i As Integer
Dim lastRow As Integer
Dim sht As Worksheet
Set sht = Sheets("owssvr")
With sht
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
For i = 2 To lastRow
oldPDF = "\mydoc.pdf"
oldFDF = "\mydoc_data.fdf"
newPDF = "\" & i & ".pdf"
iFileNum = FreeFile
Open oldFDF For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "<</T(some form field)/V( )>>", "<</T(some form field)/V(" & sht.Range("E" & i) & ")>>")
iFileNum = FreeFile
oldFDF = "\" & i & ".fdf"
Open oldFDF For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
Set objAcroApp = CreateObject("AcroExch.App")
Set objAcroAVDoc = CreateObject("AcroExch.AVDoc")
boResult = objAcroAVDoc.Open(oldPDF, "")
Set objAcroPDDoc = objAcroAVDoc.GetPDDoc
Set jsObj = objAcroPDDoc.GetJSObject
jsObj.ImportAnFDF oldFDF
jsObj.SaveAs newPDF
boResult = objAcroAVDoc.Close(True)
boResult = objAcroApp.Exit
Next i
End Sub

adding page numbers when programatically merging PDFs with acrobat

Hi the code below merges pdfs using adobe acrobat. It works but I am looking to add page numbers to the document so that if I merge 2 documents that are 4 pages each the page numbers go from 1 to 8. How can that be done?
Here is the code:
'http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
Sub MergePDFs()
' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
' Reference required: "VBE - Tools - References - Acrobat"
' --> Settings, change to suit
Const MyPath = "C:\mypath" '"C:\Temp" ' Path where PDF files are stored
Const MyFiles = "file1.pdf,file2.pdf" ' List of PDFs to ne merged
Const DestFile = "MergedFile.pdf" ' The name of the merged file
' <-- End of settings
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
a = Split(MyFiles, ",")
ReDim PartDocs(0 To UBound(a))
On Error GoTo exit_
If Len(Dir(p & DestFile)) Then Kill p & DestFile
For i = 0 To UBound(a)
' Check PDF file presence
If Dir(p & Trim(a(i))) = "" Then
MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
Exit For
End If
' Open PDF document
Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
PartDocs(i).Open p & Trim(a(i))
If i Then
' Merge PDF to PartDocs(0) document
ni = PartDocs(i).GetNumPages()
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
End If
' Calc the number of pages in the merged document
n = n + ni
' Release the memory
PartDocs(i).Close
Set PartDocs(i) = Nothing
Else
' Calc the number of pages in PartDocs(0) document
n = PartDocs(0).GetNumPages()
End If
Next
If i > UBound(a) Then
' Save the merged document to DestFile
If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
End If
End If
exit_:
' Inform about error/success
If Err Then
MsgBox Err.Description, vbCritical, "Error #" & Err.Number
ElseIf i > UBound(a) Then
MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
End If
' Release the memory
If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
Set PartDocs(0) = Nothing
' Quit Acrobat application
AcroApp.Exit
Set AcroApp = Nothing
End Sub
attached a stand-alone VBS/VBA script, which add the page number as footer to your pdf. You may took out the parts you need and write into your script just before you save the pdf or execute it afterwards.
Full Script:
File = "D:\Test.pdf"
Set App = CreateObject("Acroexch.app") '//start acrobat
app.show '//show Acrobat or comment out for hidden mode
Set AVDoc = CreateObject("AcroExch.AVDoc")
Set AForm = CreateObject("AFormAut.App") '//get AFormAPI to execute js later
If AVDoc.Open(File,"") Then
'//write JS-Code on a variable
Ex = " // set Date, filename and PageNo as footer "&vbLF _
& " var Box2Width = 50 "&vbLF _
& " for (var p = 0; p < this.numPages; p++) "&vbLF _
& " { "&vbLF _
& " var aRect = this.getPageBox(""Crop"",p); "&vbLF _
& " var TotWidth = aRect[2] - aRect[0] "&vbLF _
& " { var bStart=(TotWidth/2)-(Box2Width/2) "&vbLF _
& " var bEnd=((TotWidth/2)+(Box2Width/2)) "&vbLF _
& " var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]); "&vbLF _
& " fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages; "&vbLF _
& " fp.textSize=6; fp.readonly = true; "&vbLF _
& " fp.alignment=""center""; "&vbLF _
& " } "&vbLF _
& " } "
'//Execute JS-Code
AForm.Fields.ExecuteThisJavaScript Ex
msgBox("Done")
end if
Set AVDoc = Nothing
Set APP = Nothing
The parts you really need if you only want to take over in your script:
Set AForm = CreateObject("AFormAut.App")
Ex = " // set Date, filename and PageNo as footer "&vbLF _
& " .....
& " .....
& " } "
'//Execute JS-Code
AForm.Fields.ExecuteThisJavaScript Ex
This also demonstrates how you can use/execute AcroJs via VBS/VBA without translating to JSO (Java Script Object).
Good luck, Reinhard

Alternative to URLDownloadtofile when automating IE with VBA

I have been using InternetExplorer.application with Excel VBA for quite a while with few issues. One problem I have is downloading a file from website. I can get as far as having the "Open/Save As" buttons appear but that is where I am stuck.
I've tried using URLDownloadToFile and it does not seem to work through the same session as the InternetExplorer.application objects that I have. It usually returns the HTML text for a webpage stating that authentication is required. If I have multiple browsers open and some of the old ones are already authenticated then it will download the file most of the time.
Is there a way to download the file using the InternetExplorer.application object itself? If not, is there some way I can associate the URLDownloadtofile function with the object that is already authenticated and logged into the website?
EDIT:
The code I've been using is:
IE2.navigate ("https://...")
strURL = "https://..."
strPath = "c:\..."
Ret = URLDownloadToFile(0, strURL, strPath, 0, 0)
I've also tried:
Do While IE2.Readystate <> 4
DoEvents
Loop
SendKeys "%S"
IE2.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
And:
Dim Report As Variant
Report = Application.GetSaveAsFilename("c:\...", "Excel Files (*.xls), *.xls")
No success in any of these, except for the first one which sometimes saves the actual file, but sometimes saves the website that states the authentication error.
Thanks,
Dave
I have managed to solve similar issue with some JavaScript.
The first step is to make JavaScript download the content of the file into a binary array (it doesn't require another authentication once the user is already authenticated).
Then, I needed to pass this binary array back to VBA. I didn't know the other way, so I print the content of this array into a temporary DIV element (with JavaScript) as a string and then read it with VBA and convert it back to binary array.
Finally, I re-created the file from the given binary array by using ADODB.Stream class.
The time required to download a single file grows geometrically with the size of this file. Therefore, this method is not suitable for large files (> 3MB), since it tooks more than 5 minutes then to download a single file.
Below is the code to do that:
'Parameters:
' * ie - reference to the instance of Internet Explorer, where the user is already authenticated.
' * sourceUrl - URL to the file to be downloaded.
' * destinationPath - where the file should be saved.
'Be aware that the extension of the file given in [destinationPath] parameter must be
'consistent with the format of file being downloaded. Otherwise the function below will
'crash on the line: [.SaveToFile destinationPath, 2]
Public Function saveFile(ie As Object, sourceUrl As String, destinationPath As String)
Dim binData() As Byte
Dim stream As Object
'------------------------------------------------------------------------------------
binData = getDataAsBinaryArray(ie, sourceUrl)
Set stream = VBA.CreateObject("ADODB.Stream")
With stream
.Type = 1
.Open
.write binData
.SaveToFile destinationPath, 2
End With
End Function
Private Function getDataAsBinaryArray(Window As Object, Path As String) As Byte()
Const TEMP_DIV_ID As String = "div_binary_transfer"
'---------------------------------------------------------------------------------------------
Dim strArray() As String
Dim resultDiv As Object
Dim binAsString As String
Dim offset As Integer
Dim i As Long
Dim binArray() As Byte
'---------------------------------------------------------------------------------------------
'Execute JavaScript code created automatically by function [createJsScript] in
'the given Internet Explorer window.
Call Window.Document.parentWindow.execScript(createJsScript(TEMP_DIV_ID, Path), "JavaScript")
'Find the DIV with the given id, read its content to variable [binAsString]
'and then convert it to array strArray - it is declared as String()
'in order to make it possible to use function [VBA.Split].
Set resultDiv = Window.Document.GetElementById(TEMP_DIV_ID)
binAsString = VBA.Left(resultDiv.innerhtml, VBA.Len(resultDiv.innerhtml) - 1)
strArray = VBA.Split(binAsString, ";")
'Convert the strings from the [strArray] back to bytes.
offset = LBound(strArray)
ReDim binArray(0 To (UBound(strArray) - LBound(strArray)))
For i = LBound(binArray) To UBound(binArray)
binArray(i) = VBA.CByte(strArray(i + offset))
Next i
getDataAsBinaryArray = binArray
End Function
'Function to generate JavaScript code doing three tasks:
' - downloading the file with given URL into binary array,
' - creating temporary DIV with id equal to [divId] parameter,
' - writing the content of binary array into this DIV.
Private Function createJsScript(divId As String, url As String) As String
createJsScript = "(function saveBinaryData(){" & vbCrLf & _
"//Create div for holding binary array." & vbCrLf & _
"var d = document.createElement('div');" & vbCrLf & _
"d.id = '" & divId & "';" & vbCrLf & _
"d.style.visibility = 'hidden';" & vbCrLf & _
"document.body.appendChild(d);" & vbCrLf & _
"var req = null;" & vbCrLf & _
"try { req = new XMLHttpRequest(); } catch(e) {}" & vbCrLf & _
"if (!req) try { req = new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}" & vbCrLf & _
"if (!req) try { req = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}" & vbCrLf & _
"req.open('GET', '" & url & "', false);" & vbCrLf & _
"req.overrideMimeType('text/plain; charset=x-user-defined');" & vbCrLf & _
"req.send(null);" & vbCrLf & _
"var filestream = req.responseText;" & vbCrLf & _
"var binStream = '';" & vbCrLf & _
"var abyte;" & vbCrLf & _
"for (i = 0; i < filestream.length; i++){" & vbCrLf & _
" abyte = filestream.charCodeAt(i) & 0xff;" & vbCrLf & _
" binStream += (abyte + ';');" & vbCrLf & _
"}" & vbCrLf & _
"d.innerHTML = binStream;" & vbCrLf & _
"})();"
End Function
How about something like this?
Public Sub OpenWebXLS()
' *************************************************
' Define Workbook and Worksheet Variables
' *************************************************
Dim wkbMyWorkbook As Workbook
Dim wkbWebWorkbook As Workbook
Dim wksWebWorkSheet As Worksheet
Set wkbMyWorkbook = ActiveWorkbook
' *************************************************
' Open The Web Workbook
' *************************************************
Workbooks.Open ("http://www.sportsbookreviewsonline.com/scoresoddsarchives/nba/nba%20odds%202015-16.xlsx")
' *************************************************
' Set the Web Workbook and Worksheet Variables
' *************************************************
Set wkbWebWorkbook = ActiveWorkbook
Set wksWebWorkSheet = ActiveSheet
' *************************************************
' Copy The Web Worksheet To My Workbook and Rename
' *************************************************
wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)
wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "MyNewWebSheet"
' *************************************************
' Close the Web Workbook
' *************************************************
wkbMyWorkbook.Activate
wkbWebWorkbook.Close
End Sub

MSXML2.DOMDocument load function fails in VBA

I've been struggling with the below issue for a while now and couldn't find the solution yet.
There is an iShare page with an XML file that I want to download using VBA code, then later process the XML file and save into MS Access database.
I've been using the below code for about 4 years now, it worked perfectly without any issues. But suddenly it stopped working this week.
Any ideas why?
the code:
Private Function GetRequests() As Boolean
On Error GoTo ErrHandler
Dim oDoc As MSXML2.DOMDocument
Dim Url As String
Dim sFileName As String
Set oDoc = New MSXML2.DOMDocument
oDoc.async = False
Url = cUrlDatabase & "/" & cApplicationName & "/In/" & cReqXmlFile
UpdateStatus "Loading " & cReqXmlFile
If Not oDoc.Load(Url) Then
c_sLastError = "Could not load XML " & Url
GoTo EndProc
End If
sFileName = sPath & "\Data\requests.xml"
oDoc.Save sFileName
GetRequests = True
End Function
The code fails at the oDoc.Load(Url) part, it comes back false.
Here's an example of how to gather error details:
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument
If xDoc.Load("C:\My Documents\cds.xml") Then
' The document loaded successfully.
' Now do something intersting.
Else
' The document failed to load.
Dim strErrText As String
Dim xPE As MSXML.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 End If
Set xPE = Nothing
End If
Example taken from here.
For other people finding this post:
The xml parser by now has implemented different error types (see here).
You would have to use the following code
Set objXML = CreateObject("Msxml2.DOMDocument.6.0")
ObjXML.async=true
objXML.load "/path/to/xml"
If objXML.parseError.errorCode <> 0 Then
MsgBox "Error was " + objXML.parseError.reason
End If
This should help you debug your .xml file.
For anyone else struggling with this, I found this error to be caused by text encoded in a format which could not be parsed in VBA (some weird E symbol). The objXML was nothing after the .load. I'm sure there are many possible causes, but I'll share what I found in case this helps someone. Thanks to the guys above for the error handling routines.