Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-30-2020, 02:44 PM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 03-30-2020, 03:51 PM
macropod's Avatar
macropod macropod is offline VBA Script that can convert XML Files to Word Doc. Windows 7 64bit VBA Script that can convert XML Files to Word Doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,344
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

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
[MS MVP - Word]
Reply With Quote
  #3  
Old 03-30-2020, 03:58 PM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.

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.
Reply With Quote
  #4  
Old 03-30-2020, 04:04 PM
macropod's Avatar
macropod macropod is offline VBA Script that can convert XML Files to Word Doc. Windows 7 64bit VBA Script that can convert XML Files to Word Doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,344
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

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
For PC macro installation & usage instructions, see: Installing Macros
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 03-30-2020, 04:24 PM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Try the following macro:
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?
Attached Images
File Type: png Untitled.png (237.8 KB, 41 views)
Reply With Quote
  #6  
Old 03-30-2020, 04:50 PM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 03-30-2020, 05:15 PM
macropod's Avatar
macropod macropod is offline VBA Script that can convert XML Files to Word Doc. Windows 7 64bit VBA Script that can convert XML Files to Word Doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,344
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

The error message indicates it's not an xml file Word can open, not that it can't find the file.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #8  
Old 03-30-2020, 05:21 PM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
The error message indicates it's not an xml file Word can open, not that it can't find the file.

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.
Reply With Quote
  #9  
Old 03-30-2020, 05:26 PM
macropod's Avatar
macropod macropod is offline VBA Script that can convert XML Files to Word Doc. Windows 7 64bit VBA Script that can convert XML Files to Word Doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,344
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

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
[MS MVP - Word]
Reply With Quote
  #10  
Old 03-30-2020, 05:28 PM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.

Okay got it. Thank you again for all your help.
Reply With Quote
  #11  
Old 03-31-2020, 09:30 AM
Rolo18 Rolo18 is offline VBA Script that can convert XML Files to Word Doc. Windows 10 VBA Script that can convert XML Files to Word Doc. Office 2016
Novice
VBA Script that can convert XML Files to Word Doc.
 
Join Date: Mar 2020
Posts: 8
Rolo18 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
So I download different XMLs from online examples and they were working.

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
Reply With Quote
  #12  
Old 03-31-2020, 02:16 PM
macropod's Avatar
macropod macropod is offline VBA Script that can convert XML Files to Word Doc. Windows 7 64bit VBA Script that can convert XML Files to Word Doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,344
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

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
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


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
VBA Script that can convert XML Files to Word Doc. 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

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 07:29 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft