Hi @Sanket1
Try the below code:
' Sample data (simulated DataTable)
Dim data
data = Array( _
Array("Sr. No.", "Raw Material", "Molecular Wt.", "Moles", "Mole Ratio", "Quantity", "Source", "A. R. No.", "Purity"), _
Array(1, "Material A", 100.5, 2.0, 1.0, 50, "Source A", "AR-001", 98.5), _
Array(2, "Material B", 200.2, 3.5, 2.0, 75, "Source B", "AR-002", 99.0), _
Array(3, "Material C", 150.8, 1.8, 1.5, 60, "Source C", "AR-003", 97.0), _
Array(4, "Material D", 180.0, 2.2, 1.2, 55, "Source D", "AR-004", 95.5), _
Array(5, "Material E", 220.5, 4.0, 2.5, 80, "Source E", "AR-005", 96.0) _
)
' Output file path
Dim outputPath
outputPath = "C:\Deenu\output.docx" ' Replace with your desired output path
' Create a Word document and add content
Dim wordApp, doc, table, row, col
' Create Word application object
Set wordApp = CreateObject("Word.Application")
' Make Word visible (for testing, you can set this to False to run in the background)
wordApp.Visible = True
' Create a new document
Set doc = wordApp.Documents.Add()
' Add a table at the end of the document
Set table = doc.Tables.Add(doc.Range(), UBound(data) + 1, UBound(data(0)) + 1)
' Set table style (optional)
table.Style = "Table Grid 1"
' Add column headers
For col = LBound(data(0)) To UBound(data(0))
table.Cell(1, col + 1).Range.Text = data(0)(col)
Next
' Add data rows
For row = LBound(data) + 1 To UBound(data)
For col = LBound(data(row)) To UBound(data(row))
table.Cell(row + 1, col + 1).Range.Text = data(row)(col)
Next
Next
' Save the document
doc.SaveAs2 outputPath
' Close Word document
doc.Close()
' Quit Word application
wordApp.Quit()
' Release objects from memory
Set table = Nothing
Set doc = Nothing
Set wordApp = Nothing
WScript.Echo "Word document created successfully at: " & outputPath
Thanks,
Deenuji Loganathan 👩💻
Automation Evangelist 🤖
Follow me on LinkedIn 👥
-------------------------------------------------------------------------------------------------------------
If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀