web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to pass data table...
Power Automate
Unanswered

How to pass data table variable in vb script action in PAD?

(0) ShareShare
ReportReport
Posted on by 357

Hi Team,

I have extracted the json data and store in Datatable .

now I want to write data table in word file , so I am using vb script action for the same .

 

so let me know How I pass the data table variable in vs script 

see the script

' Create an instance of the Word application
Set objWord = CreateObject("Word.Application")

' Make Word visible
objWord.Visible = True

' Add a new document
Set objDoc = objWord.Documents.Add()

' Define your DataTable variable (replace this with your actual DataTable variable)
Dim dataTable

' Get the DataTable variable passed as a command-line argument
Set dataTable = WScript.Arguments.Item(0)

' Assuming dataTable is your DataTable variable
' Get the number of rows and columns in the DataTable
numRows = dataTable.Rows.Count
numCols = dataTable.Columns.Count

' Add a table to the Word document
Set objTable = objDoc.Tables.Add(objDoc.Range, numRows + 1, numCols)

' Add column headers to the table
For col = 0 To numCols - 1
objTable.Cell(1, col + 1).Range.Text = dataTable.Columns(col).ColumnName
Next

' Add data to the table
For row = 0 To numRows - 1
For col = 0 To numCols - 1
objTable.Cell(row + 2, col + 1).Range.Text = dataTable.Rows(row)(col)
Next
Next

' Save the Word document
objDoc.SaveAs "C:\Users\sanket.shinde\Documents\@JsonExtraction\@JsonData\document.docx"

' Close the Word document and quit Word
objDoc.Close
objWord.Quit

' Release the objects
Set objTable = Nothing
Set objDoc = Nothing
Set objWord = Nothing

 

If you have any alternative solution for the same please let me know.

Thanks in advance!!!

I have the same question (0)
  • Anil_g Profile Picture
    668 Moderator on at

    @Sanket1 

     



    cheers

  • Anil_g Profile Picture
    668 Moderator on at

    rather you can send the string inside and convert to datatable

     

    Dim dataTable As DataTable = JsonConvert.DeserializeObject(Of DataTable)(jsonString)

     

    cheers

  • Sanket1 Profile Picture
    357 on at

    Hello @Anil_g 

    Could you please check bellow script that , Where and how I pass my dt variable in script ?

    I have tried but its not working. 

    thanks in advance

    ' Example datatable (replace this with your actual datatable)
    Dim %dataTable% As DataTable = JsonConvert.DeserializeObject(Of DataTable)(jsonString)

    ' Add columns to the datatable (example)
    datatable.Columns.Add("Name")
    datatable.Columns.Add("Age")
    datatable.Columns.Add("City")

    ' Add rows to the datatable (example)
    Dim row
    Set row = datatable.NewRow()
    row("Name") = "John"
    row("Age") = 30
    row("City") = "New York"
    datatable.Rows.Add(row)

    Set row = datatable.NewRow()
    row("Name") = "Jane"
    row("Age") = 25
    row("City") = "Los Angeles"
    datatable.Rows.Add(row)

    Set row = datatable.NewRow()
    row("Name") = "Mike"
    row("Age") = 35
    row("City") = "Chicago"
    datatable.Rows.Add(row)

    ' Create an instance of Word and a Document object
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True ' Make Word visible (for testing purposes)

    ' Add a new document
    Set objDoc = objWord.Documents.Add()

    ' Add a table with dynamic rows and columns
    Dim numRows, numCols
    numRows = datatable.Rows.Count + 1 ' +1 for headers
    numCols = datatable.Columns.Count

    ' Define headers from the datatable column names
    Dim headers(numCols - 1)
    For i = 0 To numCols - 1
        headers(i) = datatable.Columns(i).ColumnName
    Next

    ' Add a table to the document
    Set objRange = objDoc.Range()
    Set objTable = objDoc.Tables.Add(objRange, numRows, numCols)

    ' Format the table
    objTable.Borders.Enable = True

    ' Fill in headers
    For i = 0 To numCols - 1
        objTable.Cell(1, i + 1).Range.Text = headers(i)
    Next

    ' Fill in data
    For r = 0 To datatable.Rows.Count - 1
        For c = 0 To numCols - 1
            objTable.Cell(r + 2, c + 1).Range.Text = datatable.Rows(r)(c)
        Next
    Next

    ' Clean up objects
    Set objTable = Nothing
    Set objRange = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing

     

  • Anil_g Profile Picture
    668 Moderator on at

    @Sanket1 

     

    how you replace is wrong...this is how it should look

     

    ' Create an instance of the Word application
    Set objWord = CreateObject("Word.Application")

    ' Make Word visible
    objWord.Visible = True

    ' Add a new document
    Set objDoc = objWord.Documents.Add()

    ' Define your DataTable variable (replace this with your actual DataTable variable)
    Dim dataTable

    ' Get the DataTable variable passed as a command-line argument
    Set dataTable = JsonConvert.DeserializeObject(Of DataTable)(%jsonString%)

    ' Assuming dataTable is your DataTable variable
    ' Get the number of rows and columns in the DataTable

     

    Here %jsonstring% is the string variable from your power automate flow which is containing the json string value in it

     

    cheers

  • Sanket1 Profile Picture
    357 on at

    Could please give me complete script?

  • Anil_g Profile Picture
    668 Moderator on at

    @Sanket1 

     

    check this..I hope the script you gave is working I made only modification to include jsonstring variable

     

    ' Create an instance of the Word application
    Set objWord = CreateObject("Word.Application")

    ' Make Word visible
    objWord.Visible = True

    ' Add a new document
    Set objDoc = objWord.Documents.Add()

    ' Define your DataTable variable (replace this with your actual DataTable variable)
    Dim dataTable

    ' Get the DataTable variable passed as a command-line argument
    Set dataTable = JsonConvert.DeserializeObject(Of DataTable)(%jsonString%)

    ' Assuming dataTable is your DataTable variable
    ' Get the number of rows and columns in the DataTable
    numRows = dataTable.Rows.Count
    numCols = dataTable.Columns.Count

    ' Add a table to the Word document
    Set objTable = objDoc.Tables.Add(objDoc.Range, numRows + 1, numCols)

    ' Add column headers to the table
    For col = 0 To numCols - 1
    objTable.Cell(1, col + 1).Range.Text = dataTable.Columns(col).ColumnName
    Next

    ' Add data to the table
    For row = 0 To numRows - 1
    For col = 0 To numCols - 1
    objTable.Cell(row + 2, col + 1).Range.Text = dataTable.Rows(row)(col)
    Next
    Next

    ' Save the Word document
    objDoc.SaveAs "C:\Users\sanket.shinde\Documents\@JsonExtraction\@JsonData\document.docx"

    ' Close the Word document and quit Word
    objDoc.Close
    objWord.Quit

    ' Release the objects
    Set objTable = Nothing
    Set objDoc = Nothing
    Set objWord = Nothing

     

    cheers

  • Sanket1 Profile Picture
    357 on at

    Can I pass JsonAsCustomObject var in as a JsonString

  • Anil_g Profile Picture
    668 Moderator on at

    @Sanket1 

     

    Use convert custom object to json string and pass the json string

     

    cheers

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Valantis Profile Picture

Valantis 474

#2
11manish Profile Picture

11manish 268

#3
David_MA Profile Picture

David_MA 243 Super User 2026 Season 1

Last 30 days Overall leaderboard