Results 1 to 8 of 8

Thread: Create XML file using VBA?

  1. #1
    Active Member
    Join Date
    2008-12
    Posts
    75
    Login to Give a bone
    0

    Default Create XML file using VBA?

    Hi guys,

    I tried asking around on the Autodesk discussion forums but noone had any advice...

    Does anyone have a snippet of code to create an XML file and add content with VBA?

    I've done some research, and it seems I may need to download a 3rd party reference to add to the code, but I'm don't think it needs to be that complicated at all... I'd be quite happy to create a text file, and save it as a .xml file...

    Any help would be appreciated.

    Regards

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,420
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    .NET is designed to work with xml, but vba is not. As you said, can use txt but you have to format your own lines and manually write them. See this post on writing to txt. You can search this forum for similar posts using the keywords you find in this post.
    C:> ED WORKING....


    LinkedIn

  3. #3
    AUGI Addict
    Join Date
    2015-12
    Posts
    2,095
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    XML files are plain text, so you could just could just write it out. You would have to make sure it is valid though. The preferred option would be to reference the MSXML6 module, which provides access to the DOMDocument, documentElement, selectSingleNode, and other objects and their members.

  4. #4
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    Quote Originally Posted by gerrard.hickson View Post
    Hi guys,

    I tried asking around on the Autodesk discussion forums but noone had any advice...

    Does anyone have a snippet of code to create an XML file and add content with VBA?

    I've done some research, and it seems I may need to download a 3rd party reference to add to the code, but I'm don't think it needs to be that complicated at all... I'd be quite happy to create a text file, and save it as a .xml file...

    Any help would be appreciated.

    Regards
    Here is working example, this will allow you to
    write all of the block references attributes in Model Space
    into the .xml file
    (Sorry, don't remember who is an author of this code)

    Code:
    Option Explicit
    Public Const strFilePath As String = "C:\TestXML.xml"
     
    ''  ===  request reference to Microsoft XML, vX.0  ===  ''
     
    Sub ExtractToXML()
        Dim objDoc As MSXML2.DOMDocument
        Dim objNode As MSXML2.IXMLDOMNode
        Dim objRoot As MSXML2.IXMLDOMElement
        Dim objElem As MSXML2.IXMLDOMElement
        Dim oblkRef As AcadBlockReference
        Dim ent As AcadEntity
        Dim ar As Variant
        Dim i As Integer
        Set objDoc = New DOMDocument
        objDoc.resolveExternals = True
        Set objNode = objDoc.createProcessingInstruction( _
                      "xml", "version='1.0' encoding='UTF-8'")
        Set objNode = objDoc.insertBefore(objNode, _
                                          objDoc.childNodes.Item(0))
        Set objRoot = objDoc.createElement("blockdata")
        Set objDoc.documentElement = objRoot
        objRoot.setAttribute "xmlns:od", _
                             "urn:schemas-microsoft-com:officedata"
        For Each ent In ThisDrawing.ModelSpace
            If TypeOf ent Is AcadBlockReference Then
                Set oblkRef = ent
                If oblkRef.HasAttributes Then
                    Set objElem = objDoc.createElement(oblkRef.Name)
                    objRoot.appendChild objElem
                    ar = oblkRef.GetAttributes
                    For i = LBound(ar) To UBound(ar)
                        Set objNode = objDoc.createElement(ar(i).TagString)
                        objNode.Text = ar(i).TextString
                        objElem.appendChild objNode
                    Next i
                End If
            End If
        Next ent
        objDoc.Save strFilePath
    End Sub
    ~'J'~

  5. #5
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    Quote Originally Posted by Ed Jobe View Post
    .NET is designed to work with xml, but vba is not. As you said, can use txt but you have to format your own lines and manually write them. See this post on writing to txt. You can search this forum for similar posts using the keywords you find in this post.
    I had agreed, perhaps easier yet to write as plain text
    something like this one
    (tested on A2009 only)

    Code:
    Public Sub WriteBlocksToXML()
        Dim oBlkRef As AcadBlockReference
        Dim oAttrib As AcadAttributeReference
        Dim oEnt As AcadEntity
        Dim attArray As Variant
        Dim i As Integer
        Dim xmlFile
        xmlFile = "C:\UsedFiles\MyBlocks.xml"
     
        Open xmlFile For Output As #1
            Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
                  " encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>"
                  '' <  add xml-schema if it's needed here >
            Print #1, "<blocks>"
     
       For Each oEnt In ThisDrawing.ModelSpace
            If TypeOf oEnt Is AcadBlockReference Then
                Set oBlkRef = oEnt
                If oBlkRef.HasAttributes Then
                    Print #1, "<block>"
                    Print #1, "<name>" & oBlkRef.EffectiveName & "</name>"
                    Print #1, "<id>" & CStr(oBlkRef.ObjectID) & "</id>"
                    Print #1, "<attributes>"
                    attArray = oBlkRef.GetAttributes
                    For i = LBound(attArray) To UBound(attArray)
                    Set oAttrib = attArray(i)
                    Print #1, "<attribute>"
                    Print #1, "<tag>" & oAttrib.TagString & "</tag>"
                    Print #1, "<value>" & oAttrib.TextString & "</value>"
                    Print #1, "</attribute>"
                    Next i
                    Print #1, "</attributes>"
                    Print #1, "</block>"
                End If
            End If
        Next oEnt
     
        Print #1, "</blocks>"
        Close #1
    End Sub
    ~'J'~

  6. #6
    Active Member
    Join Date
    2008-12
    Posts
    75
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    Thanks for the replies guys...

    I was prepared to take the manually formatted text file approach, but Neil Munro on the autodesk forums suggested the approach of adding reference to the Microsoft XML library the same as fixo's first post... I'll try that one on for size, to see if it does a better job.

    I'm also playing with .Net for another project, so I'll look into using that instead of VBA...

    Thanks for the tips

    Cheers

  7. #7
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    Quote Originally Posted by gerrard.hickson View Post
    Thanks for the replies guys...

    I was prepared to take the manually formatted text file approach, but Neil Munro on the autodesk forums suggested the approach of adding reference to the Microsoft XML library the same as fixo's first post... I'll try that one on for size, to see if it does a better job.

    I'm also playing with .Net for another project, so I'll look into using that instead of VBA...

    Thanks for the tips

    Cheers
    What is your goal, I want to know what the
    kind of sort the data you want to store?
    (just a thoughts, probaly better yet to use
    Serialize / Deserialize .NET methods for
    this task)

    ~'J'~

  8. #8
    AUGI Addict
    Join Date
    2015-12
    Posts
    2,095
    Login to Give a bone
    0

    Default Re: Create XML file using VBA?

    I've done manual XML construction before and its a PITA to track down where the invalid elements are. Read through the MSXML documentation, its a pretty good primer on the objects methods and properties involved. Also check w3schools.com for their XML and XPATH references if you aren't familiar with it.
    Last edited by Ed Jobe; 2010-10-13 at 05:04 PM. Reason: added link

Similar Threads

  1. 2014: Revit reporting Central File is not available, no one else is in file, can't save, won't create local file
    By patricks in forum Revit - Worksharing/Worksets/Revit Server
    Replies: 3
    Last Post: 2013-10-03, 10:31 PM
  2. create a file joined onto a CAD file for notes/amendments
    By solarsupplies in forum AutoCAD General
    Replies: 6
    Last Post: 2009-11-18, 05:17 AM
  3. Replies: 10
    Last Post: 2007-10-09, 01:11 PM
  4. Replies: 4
    Last Post: 2007-03-06, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •