Click here to Skip to main content
15,175,307 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I need to get the xml output using some vbscript code using test/source excel.
Vbscript code.
----------------
VBScript
Dim fso,oExcel,wb,ws
' Create Scripting filesystemobject
Set fso = WScript.Createobject("Scripting.Filesystemobject")
' Create Excel application object
Set oExcel = WScript.Createobject("Excel.Application")
oExcel.Visible = True

Work_path=fso.GetAbsolutePathName("D:\")

' Create a new xml basic syntax into created Xml file
Set XmlFile=fso.CreateTextFile(Work_path&"\Sample.xml",True,True)

' Write some xml basic syntax into created Xml file
XmlFile.Write("<?xml version=""1.0"" encoding=""utf-8""?>"&vbNewLine)

' Open the excel from specified path
Set wb=oExcel.Workbooks.Open(Work_path&"\Sample.xlsx")
Set ws=wb.Sheets(1)
Row_count=ws.UsedRange.Columns.count 'Given the used rows count of sheet

XmlFile.Write("<asset class=""data_file"" repr=""Sample.xlsx"" ID=""a101"">"&vbNewLine)
    
    XmlFile.Write("<attribute name=""name"" value=""Sample.xlsx""/>"&vbNewLine)
    XmlFile.Write("<attribute name=""path"" tag=""true"" value=""/people""/>"&vbNewLine)
    XmlFile.Write("<reference name=""parent folder or host"" assetIDs=""a100""/>"&vbNewLine)
XmlFile.Write("</asset>"&vbNewLine)

XmlFile.Write("<asset class=""data_file_record"" repr=""Sample"" ID=""a102"">"&vbNewLine)
    XmlFile.Write("<attribute name=""name"" value=""Sample""/>"&vbNewLine)
    XmlFile.Write("<reference name=""data_file"" assetIDs=""a101""/>"&vbNewLine)
XmlFile.Write("</asset>"&vbNewLine)
For i=1 To Row_count
XmlFile.Write("<asset class=""data_file_field"" repr=""X1"" ID=""a103"">"&vbNewLine)
    X1=ws.cells(1,i).value 'Read the 1st row each column one by one
    XmlFile.Write("<attribute name=""name"" value=""X1""/>"&vbNewLine)
    XmlFile.Write("<reference name=""data_file_record"" assetIDs=""a102""/>"&vbNewLine)
XmlFile.Write("</asset>"&vbNewLine)
Next
wb.close 'Close the opened workbook
oExcel.Quit 'Quit Excel Application object and release the excel object
MsgBox("Done")


What I have tried:

My expected output.
----------------

XML
<asset class="data_file" repr="Sample.xlsx" id="a101">
<attribute name="name" value="Sample.xlsx">
<attribute name="path" tag="true" value="/people">
<reference name="parent folder or host" assetids="a100">

<asset class="data_file_record" repr="Sample" id="a102">
<attribute name="name" value="Sample">
<reference name="data_file" assetids="a101">

<asset class="data_file_field" repr="X1" id="a103">
<attribute name="name" value="X1">
<reference name="data_file_record" assetids="a102">

<asset class="data_file_field" repr="X2" id="a104">
<attribute name="name" value="X2">
<reference name="data_file_record" assetids="a102">

<asset class="data_file_field" repr="X3" id="a105">
<attribute name="name" value="X3">
<reference name="data_file_record" assetids="a102">

<asset class="data_file_field" repr="X4" id="a106">
<attribute name="name" value="X4">
<reference name="data_file_record" assetids="a102">

<asset class="data_file_field" repr="X5" id="a107">
<attribute name="name" value="X5">
<reference name="data_file_record" assetids="a102">

<asset class="data_file_field" repr="X6" id="a108">
<attribute name="name" value="X6">
<reference name="data_file_record" assetids="a102">
Posted
Updated 3-Nov-21 3:51am
v2
Comments
CHill60 3-Nov-21 5:44am
   
So what actually happens when you run your script?

1 solution

Without your sample data it's impossible to determine if this is the only error, however this line is incorrect
VB
Row_count=ws.UsedRange.Columns.count 'Given the used rows count of sheet
Based on the name of the variable and the comment on that line it should be
VB
Row_count=ws.UsedRange.Rows.count 'Given the used rows count of sheet
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900