Link to home
Start Free TrialLog in
Avatar of gdunn59
gdunn59

asked on

How to Insert an XML Tag in a Certain Place in the XML Document

I have some VBA Code in Sheet1 that loops through Sheet1 of the spreadsheet and places certain data on Sheet2, then the Code creates an XML file from the data on Sheet2.

I'm not sure exactly how to incorporate code to my existing code that will place the XML Tags in the proper place (I have included the XML file to show you the different tags/structure of the XML file).

Here is the VBA Code that creates the XML File:
Public Function ExportToXML(FullPath As String, RowName As String) As Boolean
On Error GoTo ErrorHandler

Dim colIndex As Integer
Dim rwIndex As Integer
Dim asCols() As String
Dim oWorkSheet As Worksheet
Dim sName As String
Dim lCols As Long, lRows As Long
Dim iFileNum As Integer
 
Application.ScreenUpdating = False
 
Set oWorkSheet = ThisWorkbook.Worksheets(1)
sName = oWorkSheet.Name
lCols = oWorkSheet.ListObjects(1).ListColumns.Count
lRows = oWorkSheet.ListObjects(1).ListRows.Count

 ReDim asCols(lCols) As String

 iFileNum = FreeFile
 Open FullPath For Output As #iFileNum
 
' Change the Column Names
  For i = 0 To lCols - 1
     'Assumes no blank column names
     If Trim(Sheet2.Cells(1, i + 1).Value) = "" Then Exit For
     Select Case Sheet2.Cells(1, i + 1).Value
        Case "InvIndex"
            ColName = "InvMaster KeyValue"
        Case "InvElecHistoryID"
            ColName = "InvElecHistory"
        Case "DateSubmitted"
            ColName = "DateSent"
        Case Else
            ColName = Sheet2.Cells(1, i + 1).Value
     End Select
     asCols(i) = ColName
 Next i
 
 If i = 0 Then GoTo ErrorHandler
 lCols = i
 
 Print #iFileNum, "<InvMaster xmlns=""http://elite.com/schemas/transaction/process/write/InvMaster"">"
 Print #iFileNum, "<Initialize xmlns=""http://elite.com/schemas/transaction/object/write/InvMaster"">"
 Print #iFileNum, "<Edit>"
 
 For i = 2 To lRows
 If Trim(Sheet2.Cells(i, 1).Value) = "" Then Exit For
     For j = 1 To lCols
         If Trim(Sheet2.Cells(i, j).Value) = "InvIndex" Then
         Print #iFileNum, "  <" & asCols(j - 1) & ">"
         Print #iFileNum, Trim(Sheet2.Cells(i, j).Value);
         Print #iFileNum, "</" & asCols(j - 1) & ">"
         End If
         
         If Trim(Sheet2.Cells(i, j).Value) <> "" Then
         Print #iFileNum, "  <" & asCols(j - 1) & ">"
         Print #iFileNum, Trim(Sheet2.Cells(i, j).Value);
         Print #iFileNum, "</" & asCols(j - 1) & ">"
         
         DoEvents 'OPTIONAL

         End If
 
     Next j
 Next i

 Print #iFileNum, "</Children>"
 Print #iFileNum, "</Edit>"
 Print #iFileNum, "</Initialize>"
 Print #iFileNum, "</InvMaster>"
 
 ExportToXML = True
ErrorHandler:
 If iFileNum > 0 Then Close #iFileNum
 Exit Function
 
Application.ScreenUpdating = True

End Function

Open in new window


Here is an example of what the XML file that is created from the above VBA Code should look like:
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster">
    <Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
        <Edit>

            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>



            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>


            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>


        </Edit>
    </Initialize>
</InvMaster>

Open in new window


I've never worked with XML.  Could someone please help me on this?

Thanks,
gdunn59
Avatar of regmigrant
regmigrant
Flag of United Kingdom of Great Britain and Northern Ireland image

When you store data in an excel table you have a heading row and a list of data.

In excel the headings are treated as the XML Tags and the data underneath is the value of each of the tags for a given instance (or row). How the Excel headings are translated to XML tags is stored in an XML MAP - but you aren't using Excel XML functionality here - you are creating a text file which uses some XML standard formatting (like angle brackets <>)

The sequence 'Select Case' is reading data on sheet2 and creating column names in the active sheet whilst the second loop (For j = 1 To lCols) is reading the table and outputting the data to a text file with <> around it. Outside of this loop you have some standalone statement outputting some other XML style tags (ie: data wrapped in <> like <children>).

for this vba to produce the file listing shown you will need to have a column heading for each of the tags:-
            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>
your code then needs to loop through the data in each row of the table and generate output line for each cell with the header name as the tag followed by the cell contents as the data, once you reach the end of the row it will then need to output the close tag sequence  by outputting the header with a / in front:
                      </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>

this will be quite a useful and instructive way of learning about well formed XML but if you download the MS Addin I recommended in the other question you can start to use excel built in XML handling - in that case you would create the table in excel, create a map of the table headers to XML tags and excel would handle the creation of the well formed XML automagically
Avatar of gdunn59
gdunn59

ASKER

regmigrant,

Thanks for the information.  I'll give it a shot.

In regards to downloading the Addin, we're restricted from downloading anything.

Thanks,

gdunn59
Avatar of gdunn59

ASKER

regmigrant,

There's no way to use VBA code to insert these tags?

Thanks,
gdunn59
Avatar of gdunn59

ASKER

regmigrant,

Your solution didn't work.

Thanks,

gdunn59
I can provide a more complete explanation if you can give me a sample of the data.

In the vba you quoted, for example, there is no way for the 'children' element to be repeated with the other tags because its outside the loop. If I have the expected input I can look at ways to modify the vba

Specifically when you say 'some tags' which tags are missing!
Avatar of gdunn59

ASKER

regmigrant,

Here is the code I currently have:
Public Function ExportToXML(FullPath As String, RowName As String) As Boolean
On Error GoTo ErrorHandler

 Dim colIndex As Integer
 Dim rwIndex As Integer
 Dim asCols() As String
 Dim oWorkSheet As Worksheet
 Dim sName As String
 Dim lCols As Long, lRows As Long
 Dim iFileNum As Integer
 
Application.ScreenUpdating = False
 
Set oWorkSheet = ThisWorkbook.Worksheets(1)
sName = oWorkSheet.Name
lCols = oWorkSheet.ListObjects(1).ListColumns.Count
lRows = oWorkSheet.ListObjects(1).ListRows.Count

 ReDim asCols(lCols) As String

 iFileNum = FreeFile
 Open FullPath For Output As #iFileNum
 
 For i = 0 To lCols - 1
     'Assumes no blank column names
     If Trim(Sheet2.Cells(1, i + 1).Value) = "" Then Exit For
     asCols(i) = Sheet2.Cells(1, i + 1).Value
 Next i

 If i = 0 Then GoTo ErrorHandler
 lCols = i
 
 Print #iFileNum, "<InvMaster xmlns=""http://elite.com/schemas/transaction/process/write/InvMaster"" xsl:output indent=""yes""/>"
 Print #iFileNum, "<Initialize xmlns=""http://elite.com/schemas/transaction/object/write/InvMaster"">"
 Print #iFileNum, "<Edit>"
 
 For i = 2 To lRows
 If Trim(Sheet2.Cells(i, 1).Value) = "" Then Exit For

     For j = 1 To lCols

         If Trim(Sheet2.Cells(i, j).Value) <> "" Then
         Print #iFileNum, "  <" & asCols(j - 1) & ">"
         Print #iFileNum, Trim(Sheet2.Cells(i, j).Value);
         Print #iFileNum, "</" & asCols(j - 1) & ">"

         DoEvents 'OPTIONAL

         End If
     Next j
 
 Next i

 Print #iFileNum, "</Edit>"
 Print #iFileNum, "</Initialize>"
 Print #iFileNum, "</InvMaster>"
 
 ExportToXML = True
ErrorHandler:
 If iFileNum > 0 Then Close #iFileNum
 Exit Function
 
Application.ScreenUpdating = True

End Function

Open in new window


Here are the results of the XML file that is created from the current VBA Code:
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster" xsl:output indent="yes"/>
<Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
<Edit>
  <InvIndex>707785</InvIndex>
  <InvElectHistID>9B2932BE-B778-464C-9F48-C5D0592C9A43</InvElectHistID>
  <DateSubmitted>1/20/2016</DateSubmitted>
  <Comments>This is test1</Comments>
  <InvIndex>707949</InvIndex>
  <InvElectHistID>4EDD4E14-122D-4456-BC4C-C8BD8C7301B9</InvElectHistID>
  <DateSubmitted>1/22/2016</DateSubmitted>
  <Comments>This is test2</Comments>
  <InvIndex>707975</InvIndex>
  <InvElectHistID>56396562-F974-40A8-9FDA-B2B3D5A51BB7</InvElectHistID>
  <DateSubmitted>1/31/2016</DateSubmitted>
  <Comments>This is test3</Comments>
</Edit>
</Initialize>
</InvMaster>

Open in new window



Here is what I need the XML file to look like and all the tags it should have (need VBA Code to be able to produce the XML file this way with all the tags listed below, if possible):
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster">
    <Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
        <Edit>

            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>



            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>


            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>


        </Edit>
    </Initialize>
</InvMaster>

Open in new window



I've attached a test file for you (it has somewhat different data than what I have posted here).

Thanks,

gdunn59
Test.xlsm
ok, you need to understand how the table headings map to the XML tags, here's what your looking for followed by what you currently have:
 User generated image
User generated image
 I've added them in where I think they go in the attached update to your code but you need to confirm how the 'edit' sequence works and how the repeating element (especially invelechistory and attributes) is expected to work (see screen grab below).
In the revised spreadsheet I had to copy the table from sheet 1 to sheet 2 to generate some output so it may be adding more data  than is needed - if you can establish the repeating group rules that will help.
- don't forget to change the output name
User generated imageTest.xlsm
Avatar of gdunn59

ASKER

regmigrant,

You're close, but still a little off.

Here is my XML example of how the results should be laid out (I don't need the extra blank lines in between, I just wanted to separate them because it is easier to see how I need them layout/tagged):
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster">
    <Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
        <Edit>

            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>

            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>

            <InvMaster KeyValue="707786">
                <Children>
                    <InvElecHistory>
                        <Edit>
                            <InvElecHistory KeyValue="GUID">
                                <Attributes>
                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>
                                </Attributes>
                            </InvElecHistory>
                        </Edit>
                    </InvElecHistory>
                </Children>
            </InvMaster>

        </Edit>
    </Initialize>
</InvMaster>

Open in new window


The InvMaster should be first by itself, and then the others, and then each time there is a new InvMaster, it should start over and have each one grouped/tagged together (please see my XML Results layout above).

Could you give it another shot please.  I tried and still not able to get it where they should be.

Thanks,

gdunn59
The code can be modified to put the tags in whatever order, the key thing is understanding the mapping between the cells in the spreadsheet to the tags in the xml and the understanding (schema) that the recipient of the file is working to.

So, for example:
  <InvMaster KeyValue="707786">  
- is the main invoice record? presumably there will be no more invoices with that value so all entries with invoice 707786 will be related to this?

                <Children>
                    <InvElecHistory>
                        <Edit>

               - these have no equivalent in they spreadsheet and they contain no values; are they there
               to maintain the xml structure?

<InvElecHistory KeyValue="GUID">

We have now, presumably,  reached a repeating group starting with the second occurrence of InvElecHistory? how is that identified on the spreadsheet? - is it the GUID? should that be included in the Tag?

 <Attributes> another placeholder to show the actual values of the edit come next?

                                    <DateSent>0001-01-02T00:00:00</DateSent>
                                    <Comments>NewValue</Comments>

So these two are the only data elements we are sending? Which spreadsheet cells represent these two? is the format of the date significant?

Having established this map and the rules around the repeating element (ie; answered the questions above) there is next the question of definition
 - if there are multiple edits to a single INVElecHistory are you expecting them all under the same <Invmaster keyvalue> with, perhaps, different GUID on the InvElecHistory, or is it acceptable to have multiple <Invmaster keyvalue 707786> entries with their own attributes showing a different update each time.
Avatar of gdunn59

ASKER

regmigrant,

In regards to your questions:

This is a unique value and is the main invoice record:  <InvMaster KeyValue="707786">  

Yes these are for the XML Structure:
              <Children>
                     <InvElecHistory>
                         <Edit>

So these two are the only data elements we are sending?   YES

Which spreadsheet cells represent these two?  SubmittedDate and Comments2

is the format of the date significant?  I'm not sure.  The person I need to confirm that is out of the office until next week.

if there are multiple edits to a single INVElecHistory are you expecting them all under the same <Invmaster keyvalue> with, perhaps, different GUID on the InvElecHistory, or is it acceptable to have multiple <Invmaster keyvalue 707786> entries with their own attributes showing a different update each time.

No, the InvMaster KeyValue is a unique value.

Here is exactly how it should be laid out:
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster" xsl:output indent="yes"/>
<Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
 <Edit>
  <InvMaster KeyValue>707785</InvMaster KeyValue>  
     <Children>
       <InvElecHistory>  'This is like a group tag for the record
	    <Edit>  
       	<InvElecHistory KeyValue>9B2932BE-B778-464C-9F48-C5D0592C9A43</InvElectHistory KeyValue>
             <Attributes>
               <DateSubmitted>1/20/2016</DateSubmitted>
               <Comments>This is test1</Comments>
	' Closing Tags for the first set of data
  	        </Attributes>
           </InvElecHistory KeyValue>
         </Edit>
 	  </InvElecHistory>
	</Children>
  </InvMaster KeyValue>


  <InvMaster KeyValue>707949</InvMaster KeyValue>  
     <Children>
       <InvElecHistory>  'This is like a group tag for the record
	    <Edit>  
       	<InvElecHistory KeyValue>4EDD4E14-122D-4456-BC4C-C8BD8C7301B9</InvElectHistory KeyValue>
             <Attributes>
               <DateSubmitted>1/22/2016</DateSubmitted>
               <Comments>This is test2</Comments>
	' Closing Tags for the first set of data
  	        </Attributes>
           </InvElecHistory KeyValue>
         </Edit>
 	  </InvElecHistory?
	</Children>
  </InvMaster KeyValue>


</Edit>
</Initialize>
</InvMaster>

Open in new window

Avatar of gdunn59

ASKER

regmigrant,

Hello, just wanted to check and see if my last posting made sense, and if you've been able to work on it yet?

Thanks,

gdunn59
sorry - timezone problems, working on it now
ok, here's a version that gets close to what you outlined.

From a purist xml point of view I think it has issues but take a look and see what you think

Remember to change the output file path
Copy-of-Test-0v4.xlsm
Avatar of gdunn59

ASKER

Ok. Thanks
Avatar of gdunn59

ASKER

regmigrant,

I'm not exactly sure step-by-step what your code is doing, but when I pasted the code into my spreadsheet, it's not producing anything but a file that contains the first 4 tags and that's it:

<?xml version= "1.0" encoding="utf-8"?>
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster">
<Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
<Edit>

It won't even work in the original Test file that I sent you and pasted the code into.

Any clues?

Thanks,
gdunn
Avatar of gdunn59

ASKER

regmigrant,

I did step through your code and figured out somewhat what's going on.

First thing I noticed is that your code is pulling from the "InvNumber" field, and should be pulling from the following 4 fields:

1.  InvIndex (a/k/a InvMaster KeyValue) i.e. 111111
2.  InvElecHistoryID (a/k/a InvElecHistory KeyValue) i.e. 86868686868
3.  SubmittedDate i.e. 1/22/2016
4.  Comments2 i.e. This is test2

The InvNumber for the following 4 items listed above that you pulled from is:  609333

I tried changing the code to match the correct fields, but for some reason it only runs through the first one and then stops.

I'm going to try to get it working.  If I do, I will let you know.

In the meantime if you have time and can change things the way it should, that would be great.

Thanks,

gdunn59
Avatar of gdunn59

ASKER

regmigrant,

Ok.  I changed the code to reflect the correct fields, and it is giving me what I need with all the appropriate tags and in the correct order, but even though there is only 3 rows that have information in the "DateSubmitted" and "Comments" fields, it continues to create XML rows for several other rows from Sheet1, and it shouldn't be doing that.  It should only be capturing and creating XML based off of rows in the spreadsheet (Sheet1) that has information/updates in the "DateSubmitted" and "Comments2" fields.

It's literally doing the entire file and capturing the fields for each row in the spreadsheet (Sheet2) whether there is data in those 2 fields or not.

Here is the VBA Code that I'm using that is at least giving me all the correct tags and the correct order of the tags, but producing too many rows in the XML file with rows that don't have any updates.  
Public Function ExportToXML(FullPath As String, RowName As String) As Boolean
On Error GoTo ErrorHandler

 Dim colIndex As Integer
 Dim rwIndex As Integer
 Dim asCols() As String
 Dim oWorkSheet As Worksheet
 Dim sName As String
 Dim lCols As Long, lRows As Long
 Dim iFileNum As Integer
 Dim invRange As Range
 
Application.ScreenUpdating = False
 
Set oWorkSheet = ThisWorkbook.Worksheets(1)
sName = oWorkSheet.Name
lCols = oWorkSheet.ListObjects(1).ListColumns.Count
lRows = oWorkSheet.ListObjects(1).ListRows.Count

 ReDim asCols(lCols) As String

 iFileNum = FreeFile
 Open FullPath For Output As #iFileNum
 
 For i = 0 To lCols - 1
     'Assumes no blank column names
     If Trim(Sheet2.Cells(1, i + 1).Value) = "" Then Exit For
     asCols(i) = Sheet2.Cells(1, i + 1).Value
 Next i

 If i = 0 Then GoTo ErrorHandler
 
 lCols = i
 Print #iFileNum, "<?xml version= ""1.0"" encoding=""utf-8""?>"
 Print #iFileNum, "<InvMaster xmlns=""http://elite.com/schemas/transaction/process/write/InvMaster"">"
 Print #iFileNum, "<Initialize xmlns=""http://elite.com/schemas/transaction/object/write/InvMaster"">"
 Print #iFileNum, "<Edit>"
 
 For J = 1 To lCols
   ijCell = Trim(Sheet2.Cells(1, J).Value)
   Select Case ijCell
    Case "InvIndex"
        invIndexLoc = J
    Case "InvElecHistID"
        invElecHistoryLoc = J
    Case "DateSubmitted"
        DateSubmittedLoc = J
    Case "Comments"
        CommentsLoc = J
    End Select
Next J

If invIndexLoc = 0 Or invElecHistoryLoc = 0 Or DateSubmittedLoc = 0 Or CommentsLoc = 0 Then GoTo ErrorHandler:

Set invRange = Sheet2.Range(Cells(2, invIndexLoc).Address, Cells(lRows, invIndexLoc).Address)
  For Each cell In invRange
    Print #iFileNum, "<Invmaster KeyValue>" & cell.Value & "/<Invmaster KeyValue>"
    Print #iFileNum, "<Children>"
    Print #iFileNum, "<InvElecHistory>"
    Print #iFileNum, "<Edit>"
    
    Print #iFileNum, "<InvElecHistory KeyValue>" & Sheet2.Cells(cell.Row, invElecHistoryLoc).Value & "</InvElecHistory KeyValue>"
    Print #iFileNum, "<Attributes>"
    Print #iFileNum, "<DateSubmitted>" & Sheet2.Cells(cell.Row, DateSubmittedLoc).Value & "</DateSubmitted>"
    Print #iFileNum, "<Comments>" & Sheet2.Cells(cell.Row, CommentsLoc).Value & "</Comments>"
 
    Print #iFileNum, "</Attributes>"
    Print #iFileNum, "</InvElecHistory KeyValue>"

    Print #iFileNum, "</Edit>"
    Print #iFileNum, "</InvElecHistory>"
    Print #iFileNum, "</Children>"
    Print #iFileNum, "</Invmaster KeyValue>"
    DoEvents 'OPTIONAL
  Next cell

 Print #iFileNum, "</Edit>"
 Print #iFileNum, "</Initialize>"
 Print #iFileNum, "</InvMaster>"
 
 ExportToXML = True
ErrorHandler:
 If iFileNum > 0 Then Close #iFileNum
 Exit Function
 
Application.ScreenUpdating = True

End Function

Open in new window


Current XML Results produced from the VBA Code I posted above:
<?xml version= "1.0" encoding="utf-8"?>
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster">
<Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
<Edit>

<Invmaster KeyValue>711297/<Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue>258CF690-7329-4FC0-BA54-140FC3095E1D</InvElecHistory KeyValue>
<Attributes>
<DateSubmitted>1/20/2016</DateSubmitted>
<Comments>This is test1</Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>


<Invmaster KeyValue>711334/<Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue>14D0EE47-2AAD-478A-A21F-F0E3BFA268DC</InvElecHistory KeyValue>
<Attributes>
<DateSubmitted>1/22/2016</DateSubmitted>
<Comments>This is test2</Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

<Invmaster KeyValue>711343/<Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue>B5BA3761-704C-46E4-939C-F8215166F931</InvElecHistory KeyValue>
<Attributes>
<DateSubmitted>1/31/2016</DateSubmitted>
<Comments>This is test3</Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

<Invmaster KeyValue>/<Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue></InvElecHistory KeyValue>
<Attributes>
<DateSubmitted></DateSubmitted>
<Comments></Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

<Invmaster KeyValue>/<Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue></InvElecHistory KeyValue>
<Attributes>
<DateSubmitted></DateSubmitted>
<Comments></Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

Open in new window


There is several more rows in the XML that was created, but I just wanted to post the first 3 that have values (and are correct), and then some of the others following those that have no values in the XML because the "DateSubmitted" and "Comments2" are empty.

Thanks,
gdunn59
ASKER CERTIFIED SOLUTION
Avatar of regmigrant
regmigrant
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gdunn59

ASKER

I think i provided initially the fields i wanted, and although the names were a little different i had code in there that renames the tags to what i need in the end.  I think thats where the confusion is.

Anyway, thanks for staying in for the long haul.  Greatly appreciated.

I'll give your latest solution a try hopefully sometime tomorrow.

Thanks again,

gdunn59
Avatar of gdunn59

ASKER

regmigrant,

I tried your last solution and this time it wasn't producing any results.  Whereas, the previous posting was.

After looking at your last posting, I realized that you changed the Sheet from (1) to (2) - SEE LINE 15 OF THE CODE I INCLUDED, and it should have been left as (1), which it was in the last posting (uploaded file) that I provided to you where everything was working fine with the exception that it was pulling rows from every single row in the Spreadsheet and not just the rows where the 2 fields (SubmittedDate) and (Comments) contained data.  All that was working when I sent you my last post was that issue.

Here is the VBA code that I changed the Sheet back to (1) and also had to change back some of the Case Statements (Line 44: InvElecHistID changed from Line 48: InvElecHistoryID, and Comments changed from Comments2):

Public Function ExportToXML(FullPath As String, RowName As String) As Boolean
On Error GoTo ErrorHandler

 Dim colIndex As Integer
 Dim rwIndex As Integer
 Dim asCols() As String
 Dim oWorkSheet As Worksheet
 Dim sName As String
 Dim lCols As Long, lRows As Long
 Dim iFileNum As Integer
 Dim invRange As Range
 
Application.ScreenUpdating = False
 
Set oWorkSheet = ThisWorkbook.Worksheets(1)
sName = oWorkSheet.Name
lCols = oWorkSheet.ListObjects(1).ListColumns.Count
lRows = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row

 ReDim asCols(lCols) As String

 iFileNum = FreeFile
 Open FullPath For Output As #iFileNum
 
 For i = 0 To lCols - 1
    'Assumes no blank column names
     If Trim(Sheet2.Cells(1, i + 1).Value) = "" Then Exit For
     asCols(i) = Sheet2.Cells(1, i + 1).Value
 Next i

 If i = 0 Then GoTo ErrorHandler
 
 lCols = i
 Print #iFileNum, "<?xml version= ""1.0"" encoding=""utf-8""?>"
 Print #iFileNum, "<InvMaster xmlns=""http://elite.com/schemas/transaction/process/write/InvMaster"">"
 Print #iFileNum, "<Initialize xmlns=""http://elite.com/schemas/transaction/object/write/InvMaster"">"
 Print #iFileNum, "<Edit>"
 
 For J = 1 To lCols
   ijCell = Trim(Sheet2.Cells(1, J).Value)
   Select Case ijCell
    Case "InvIndex"
        invIndexLoc = J
    Case "InvElecHistID"
        invElecHistoryLoc = J
    Case "DateSubmitted"
        DateSubmittedLoc = J
    Case "Comments"
        CommentsLoc = J
    End Select
Next J

If invIndexLoc = 0 Or invElecHistoryLoc = 0 Or DateSubmittedLoc = 0 Or CommentsLoc = 0 Then GoTo ErrorHandler:

Set invRange = Sheet2.Range(Cells(2, invIndexLoc).Address, Cells(lRows, invIndexLoc).Address)
  For Each cell In invRange
   If Sheet2.Cells(cell.Row, CommentsLoc).Value <> "" Or Sheet2.Cells(cell.Row, DateSubmittedLoc).Value <> "" Then
    Print #iFileNum, "<Invmaster KeyValue>" & cell.Value & "</Invmaster KeyValue>"
    Print #iFileNum, "<Children>"
    Print #iFileNum, "<InvElecHistory>"
    Print #iFileNum, "<Edit>"
    
    Print #iFileNum, "<InvElecHistory KeyValue>" & Sheet2.Cells(cell.Row, invElecHistoryLoc).Value & "</InvElecHistory KeyValue>"
    Print #iFileNum, "<Attributes>"
    Print #iFileNum, "<DateSubmitted>" & Sheet2.Cells(cell.Row, DateSubmittedLoc).Value & "</DateSubmitted>"
    Print #iFileNum, "<Comments>" & Sheet2.Cells(cell.Row, CommentsLoc).Value & "</Comments>"
 
    Print #iFileNum, "</Attributes>"
    Print #iFileNum, "</InvElecHistory KeyValue>"

    Print #iFileNum, "</Edit>"
    Print #iFileNum, "</InvElecHistory>"
    Print #iFileNum, "</Children>"
    Print #iFileNum, "</Invmaster KeyValue>"
   End If
    DoEvents 'OPTIONAL
  Next cell

 Print #iFileNum, "</Edit>"
 Print #iFileNum, "</Initialize>"
 Print #iFileNum, "</InvMaster>"
 
 ExportToXML = True
ErrorHandler:
 If iFileNum > 0 Then Close #iFileNum
 Exit Function
 
Application.ScreenUpdating = True

End Function

Open in new window


Here is the XML Results from the VBA Code I posted here:
<?xml version= "1.0" encoding="utf-8"?>
<InvMaster xmlns="http://elite.com/schemas/transaction/process/write/InvMaster">
<Initialize xmlns="http://elite.com/schemas/transaction/object/write/InvMaster">
<Edit>

<Invmaster KeyValue>711297</Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue>258CF690-7329-4FC0-BA54-140FC3095E1D</InvElecHistory KeyValue>
<Attributes>
<DateSubmitted>1/20/2016</DateSubmitted>
<Comments>This is test1</Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

<Invmaster KeyValue>711334</Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue>14D0EE47-2AAD-478A-A21F-F0E3BFA268DC</InvElecHistory KeyValue>
<Attributes>
<DateSubmitted>1/22/2016</DateSubmitted>
<Comments>This is test2</Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

<Invmaster KeyValue>711343</Invmaster KeyValue>
<Children>
<InvElecHistory>
<Edit>
<InvElecHistory KeyValue>B5BA3761-704C-46E4-939C-F8215166F931</InvElecHistory KeyValue>
<Attributes>
<DateSubmitted>1/31/2016</DateSubmitted>
<Comments>This is test3</Comments>
</Attributes>
</InvElecHistory KeyValue>
</Edit>
</InvElecHistory>
</Children>
</Invmaster KeyValue>

</Edit>
</Initialize>
</InvMaster>

Open in new window


I have to leave for about an hour and have meetings this afternoon, but I would like to review one last time to make sure it is doing everything the way I need it.  I will let you know.

Thanks again for all of your assistance!!!!!!

gdunn59
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank gdunn, soory I missed your last update as I was working way, glad you got it resolved
Avatar of gdunn59

ASKER

regmigrant,

No problem.  Thanks a million again to you for the assistance.

Have a great day!

gdunn59
Avatar of gdunn59

ASKER

I wanted to give regmigrant the points just because they were very willing to assist and got me on the right track.  I accepted mine also because I had to change some things and finally got everything to work 100% as I needed.

Thanks,

gdunn59