locked
XSD File RRS feed

  • Question

  • I have an xml file, but I need to be able to program this file to automatically generate an xsd file upon saving.  Does anyone know how I can do that?  Is there a way to program a macro to generate the xsd file?

    Monday, October 3, 2011 2:53 PM

Answers

  • Hi AuntDi,

    Thanks for your post.

    I think this is what you are looking for:

    Sub Create_XSD2()
       Dim StrMyXml As String, MyMap As XmlMap
       Dim StrMySchema As String
       ' Book.xml is the file created in section one of this topic.
       StrMyXml = "C:\BookData.xml"
    
       ' Turn off async loading.
       Application.DisplayAlerts = False
       ' Add the string to the XmlMaps collection.
       Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)
       Application.DisplayAlerts = True
    
       ' Create an empty file and output the schema.
       StrMySchema = ThisWorkbook.XmlMaps(1).Schemas(1).XML
       Open "C:\BookData2.xsd" For Output As #1
       Print #1, StrMySchema
       Close #1
    End Sub
    

     

    For more about this, please refer to: http://msdn.microsoft.com/en-us/library/gg469857.aspx

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Calvin_Gao Thursday, October 20, 2011 1:40 PM
    Tuesday, October 11, 2011 4:15 AM

All replies

  • Hi AuntDi,

    Thanks for your post.

    I think this is what you are looking for:

    Sub Create_XSD2()
       Dim StrMyXml As String, MyMap As XmlMap
       Dim StrMySchema As String
       ' Book.xml is the file created in section one of this topic.
       StrMyXml = "C:\BookData.xml"
    
       ' Turn off async loading.
       Application.DisplayAlerts = False
       ' Add the string to the XmlMaps collection.
       Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)
       Application.DisplayAlerts = True
    
       ' Create an empty file and output the schema.
       StrMySchema = ThisWorkbook.XmlMaps(1).Schemas(1).XML
       Open "C:\BookData2.xsd" For Output As #1
       Print #1, StrMySchema
       Close #1
    End Sub
    

     

    For more about this, please refer to: http://msdn.microsoft.com/en-us/library/gg469857.aspx

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Calvin_Gao Thursday, October 20, 2011 1:40 PM
    Tuesday, October 11, 2011 4:15 AM
  • Thank you for responding to my post. 

    When I try to run the code provided, I receive a Microsoft Visual Basic error that states "System Error &H80041020 (-2147217376)."  I have no idea what that means.  What should I do next?

    • Proposed as answer by Calvin_Gao Wednesday, October 12, 2011 5:17 AM
    • Unproposed as answer by Calvin_Gao Wednesday, October 12, 2011 5:17 AM
    Tuesday, October 11, 2011 12:23 PM
  • Hi,

    Would you like to tell on which line cause the error? Which Excel version are your working with? What your XML file looks like?

    I would happy to help you to resolve the problem if you can provide some details like above:-)

    I look forward to hearing of you.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 12, 2011 5:20 AM
  • I experienced the same error...
    Excel 2007

    DEBUG:  Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)

    Does this line need to be written differently for 2007?

     

    Sub Create_XSD()
       Dim StrMyXml As String, MyMap As XmlMap
       Dim StrMySchema As String
       StrMyXml = "< BookInfo >"
       StrMyXml = StrMyXml & "<Book>"
       StrMyXml = StrMyXml & "<ISBN>Text</ISBN>"
       StrMyXml = StrMyXml & "<Title>Text</Title>"
       StrMyXml = StrMyXml & "<Author>Text</Author>"
       StrMyXml = StrMyXml & "<Quantity>999</Quantity>"
       StrMyXml = StrMyXml & "</Book>"
       StrMyXml = StrMyXml & "<Book></Book>"
       StrMyXml = StrMyXml & "</ BookInfo >"

       ' Turn off async loading.
       Application.DisplayAlerts = False
       ' Add the string to the XmlMaps collection.
       Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)
       Application.DisplayAlerts = True

       ' Create an empty file and output the schema.
       StrMySchema = ThisWorkbook.XmlMaps(1).Schemas(1).XML
       Open "C:\MySchema.xsd" For Output As #1
       Print #1, StrMySchema
       Close #1
    End Sub

    • Proposed as answer by biswebdotws Thursday, October 20, 2011 8:09 PM
    • Unproposed as answer by biswebdotws Thursday, October 20, 2011 8:10 PM
    Thursday, October 20, 2011 7:54 PM
  • remove the spaces in < BookInfo > & </ BookInfo >

    s/b  <BookInfo>  & </BookInfo>

     

    Works now!

    • Proposed as answer by biswebdotws Thursday, October 20, 2011 8:10 PM
    Thursday, October 20, 2011 8:10 PM
  • YO TENGO EL MISMO PROBLEMA Y YA MUY LOS espacion Y NADA, ME DICE QUE DEPURE Y ME SOMBREA CON AMARILLO LA SIGUIENTE SECCION 

    Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml)

    Monday, July 27, 2015 11:58 PM