#1
|
|||
|
|||
VBA Script that can convert XML Files to Word Doc.
Hello everyone,
I'm pretty new at VBA and I was hoping someone can help me or point me in the right direction. I currently have XML files that I need to be able to convert so I can view them on word document. I have to write a script on VBA, but I have no idea on how to start. If anyone could help I would greatly appreciate it. |
#2
|
||||
|
||||
Why do you need a macro? How many files are there? One of the file types supported by Word is the xml type. See under File|Open.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Quote:
I have multiple XML files (133). They are xml files showing how to build different types of bicycle. I need Word to open it with all the diagrams and charts that it has. Do you know how to do this. It says use VBA on my instructions. |
#4
|
||||
|
||||
Try the following macro:
Code:
Sub ConvertXMLDocuments() Application.ScreenUpdating = False Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document strDocNm = ActiveDocument.FullName strFolder = GetFolder If strFolder = "" Then Exit Sub strFile = Dir(strFolder & "\*.xml", vbNormal) While strFile <> "" If strFolder & "\" & strFile <> strDocNm Then Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) With wdDoc .SaveAs2 FileName:=Split(.FullName, ".xml")(0) & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False .Close SaveChanges:=False End With End If strFile = Dir() Wend Set wdDoc = Nothing Application.ScreenUpdating = True End Sub Function GetFolder() As String Dim oFolder As Object GetFolder = "" Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path Set oFolder = Nothing End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thank you so much for your help. I was able to create the Macro, but when i go and try to select an XML file its not showing up so that I can select it. Any idea why?
|
#6
|
|||
|
|||
Thank you for your help thus far, I'm having an issue when I go and run the macro it does not read or should I say, I can't find the XML file.
|
#7
|
||||
|
||||
The error message indicates it's not an xml file Word can open, not that it can't find the file.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Quote:
Do you know anything I can do? I downloaded another sample XML and that isn't working either. When I try to look for the file its not showing up as a selection but clearly its an xml file. |
#9
|
||||
|
||||
XML files contain a Document Type Descriptor (DTD) which defines its type. Evidently, the DTD for your xml file says it's type isn't one Word can open. One could open it as a plain text file, but all you'd see is the xml code - which I don't suppose would be very useful. You need to use whatever application this xml file type is supposed to be opened with.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Quote:
Okay got it. Thank you again for all your help. |
#11
|
|||
|
|||
Quote:
One last question regarding my other XML files that I had and weren't working do you know of this code within an xml. When I delete the !DOCTYPE ddn the macro works, not 100% because it doesn't display it neatly but it does work. Do you know of a work around from the !doctype command that these xmls file have. "<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ddn> " EDIT* Now I'm noticing a trend. It always displays an error as shown above the picture I posted yesterday when it has the " !DOCTYPE " But when I delete it off the code on the XML it converts it into a word document but it doesn't convert it perfectly. Any idea of how to get the macro working with this type of XML code? XML file.txt If it helps I attached the code of the XML file you just have to convert it back to XML format. Thank you |
#12
|
||||
|
||||
The only way of circumventing that would be to first open the file as a plain text file so the offending line can be deleted and the updated file saved, before opening it as an xml file in Word.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
open custon files, convert to Word, print/save to PDF | mgl_8 | Word VBA | 6 | 04-13-2018 03:38 PM |
How can convert MSOffice .xml files to .doc or .pdf? | FelipeGar | Word VBA | 5 | 06-05-2015 04:51 AM |
Convert old PowerPC file to Intel-based files in Word? | walmar | Word | 0 | 06-12-2013 07:07 AM |
Convert word files to html including hyperlink file types | MikS | Word | 3 | 06-12-2012 05:39 AM |
Convert JML files | Lundberg | Word | 5 | 09-11-2010 11:05 AM |