Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch conversion of XML files to PDF

Posted on 2016-07-14
9
Medium Priority
?
99 Views
Last Modified: 2016-08-24
I receive a number of XML or docxml file to be converted to pdf format. I did research and found this script:
'See http://msdn2.microsoft.com/en-us/library/bb238158.aspx
Const wdFormatPDF = 17  ' PDF format.
Const wdFormatXPS = 18  ' XPS format.
 
Const WdDoNotSaveChanges = 0
 
Dim arguments
Set arguments = WScript.arguments

 
' Make sure that there are one or two arguments
Function CheckUserArguments()
  If arguments.Unnamed.Count < 1 Or arguments.Unnamed.Count > 2 Then
    WScript.Echo "Use:"
    WScript.Echo "<script> input.doc"
    WScript.Echo "<script> input.doc output.pdf"
    WScript.Quit 1
  End If
End Function
 
 
' Transforms a doc to a pdf
Function DocToPdf(docInputFile, pdfOutputFile)
 
  Dim fileSystemObject
  Dim wordApplication
  Dim wordDocument
  Dim wordDocuments
  Dim baseFolder
 
  Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
  Set wordApplication = CreateObject("Word.Application")
  Set wordDocuments = wordApplication.Documents
 
  docInputFile = fileSystemObject.GetAbsolutePathName(docInputFile)
  baseFolder = fileSystemObject.GetParentFolderName(docInputFile)
 
  If Len(pdfOutputFile) = 0 Then
    pdfOutputFile = fileSystemObject.GetBaseName(docInputFile) + ".pdf"
  End If
 
  If Len(fileSystemObject.GetParentFolderName(pdfOutputFile)) = 0 Then
    pdfOutputFile = baseFolder + "\" + pdfOutputFile
  End If
 
  ' Disable any potential macros of the word document.
  wordApplication.WordBasic.DisableAutoMacros
 
  Set wordDocument = wordDocuments.Open(docInputFile)
 
  ' See http://msdn2.microsoft.com/en-us/library/bb221597.aspx
  wordDocument.SaveAs pdfOutputFile, wdFormatPDF
 
  wordDocument.Close WdDoNotSaveChanges
  wordApplication.Quit WdDoNotSaveChanges
   
  Set wordApplication = Nothing
  Set fileSystemObject = Nothing
 
End Function
 
' Execute script
 Call CheckUserArguments
 If arguments.Unnamed.Count = 2 Then
  Call DocToPdf(arguments.Unnamed.Item(0), arguments.Unnamed.Item(1))
 Else
  Call DocToPdf(arguments.Unnamed.Item(0), "")
 End If
'end 
 Set arguments = Nothing

Open in new window


which does fine, except that I can only do one file at a time.

Is there a way to change this so that it can covert a bunch of files in a folder that have the .xml or .docxml extension?

Thanks!
0
Comment
Question by:wasmithpfs
  • 4
  • 3
  • 2
9 Comments
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 1800 total points (awarded by participants)
ID: 41712614
sure.... do you want to do all .xml files ?  how do you want the output ?

add this to the top of your current script...

'-------------multiple XML files ----
Set objFSO = CreateObject("Scripting.FileSystemObject")
sStartFolder = "C:\ee"

Set objFolder = objFSO.GetFolder(sStartFolder)

Set colFiles = objFolder.Files

For Each objFile in colFiles
	If LCase(objFSO.GetExtensionName(objFile.Name)) = "xml" Then
		Wscript.Echo objFile.Name
		DocToPdf objfile.Name,""
    End If
Next
Wscript.Echo
wscript.quit

'----------------------------------------------
'See http://msdn2.microsoft.com/en-us/library/bb238158.aspx
Const wdFormatPDF = 17  ' PDF format.
Const wdFormatXPS = 18  ' XPS format.
 

Open in new window

0
 

Author Comment

by:wasmithpfs
ID: 41712803
Hi Robberbaron (robr),

Thanks for responding. Yes I wan to do all xml or docxml files; the outout needs to be pdf.


wasmithpfs
0
 

Author Comment

by:wasmithpfs
ID: 41713192
Hi Robberbaron (robr),

When I run the updated code, I get  an
ActiveX component can't create object: 'Word.Application'
error. I   have  the Office 365' version of Word  on my Windows 10 machine--might that be causing this issue?

wasmithpfs
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 41713733
could be... I cant test as i have Office2010 installed. (so it works for me)

what happens if you start Word before running the vbs command ?
1
 

Author Comment

by:wasmithpfs
ID: 41719318
Hi--
where are you running the vbs from? the cmd line or from run.exe?  if I run it from  cmd line and reference the target directory like this:
C:\Users\WDMobile2\Desktop\batchconvert.vbs  C:\Users\WDMobile2\Desktop\filestoconvert

I get an "expect end of statement" error that references this line of code:
Set objFolder = objFSO.GetFolder(sStartFolder)

If I run it from the run.exe and enter: C:\Users\WDMobile2\Desktop\batchconvert.vbs,  i get a different error: "object ' required wordDocument'
...which references this line of code (from my original code):
  wordDocument.SaveAs pdfOutputFile, wdFormatPDF.

Please advise. Thanks.
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 1800 total points (awarded by participants)
ID: 41719985
1/ i run from commandline.   use cscript nameofscript.vbs
2/ the target folder is currently hardcoded into script.
sStartFolder = "C:\ee"
2.1/  so try changing that first, then we an add commandline parameter read.
3/ the second error is within the previous code so its the same as your first problem where the Word.Appliciation object could not be created.
1
 
LVL 56

Assisted Solution

by:Joe Winograd, EE MVE 2015&2016
Joe Winograd, EE MVE 2015&2016 earned 200 total points (awarded by participants)
ID: 41719994
Hi wasmithpfs,

To test the theory that the problem may be Office 365, you may download and install Office 2016, which comes with most (although not all) Office 365 subscriptions, as discussed in this 5-minute EE video Micro Tutorial:
How to install the Office 2016 desktop applications that come with the free trial of Office 365 Home

Since robr says it works with Office 2010, it will likely work with Office 2016. Worth a try, assuming your Office 365 subscription includes the desktop apps. And if it doesn't, you could use a trial version that does, and for free (to at least test the theory), as explained in this other 5-minute EE video Micro Tutorial:
How to get a free trial of Office 365 with the Office 2016 desktop applications

Regards, Joe
1
 

Author Comment

by:wasmithpfs
ID: 41744274
Hi Robberbaron,

I followed your suggestion of using cscript in the command line and it worked perfectly!

Thanks so much! :)

--wasmithpfs
0
 
LVL 56

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 41768431
Nearly all the credit (450 points) to Robberbaron for the right answer, as stated by the asker: "it worked perfectly!" A small credit (50 points) to Joe Winograd for a good way to test the theory that the problem may be Office 365.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question