How do I convert old vba code to Office Script to run in Excel?

Rick Neff 0 Reputation points
2026-01-17T07:35:57.1033333+00:00

I would like to convert the following vba code to Office script. Is there an automated converter ?

Sub FillWordContentControls()
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim cc As Object
    Dim path As String
    Dim valueName As String
    Dim valueAddress As String
    ' Path to your Word template
    path = "C:\Temp\Template.docx"
    ' Start Word (or get existing instance)
    On Error Resume Next
    Set wdApp = GetObject(Class:="Word.Application")
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    wdApp.Visible = True
    ' Open the document
    Set wdDoc = wdApp.Documents.Open(path)
    ' Example: Read Excel values
    Dim customerName As String
    Dim orderDate As String
    Dim amount As String
    customerName = ThisWorkbook.Sheets("Sheet1").Range("B2").Value
    orderDate = ThisWorkbook.Sheets("Sheet1").Range("B3").Value
    amount = ThisWorkbook.Sheets("Sheet1").Range("B4").Value
    ' Insert into Word Content Controls by Title
    For Each cc In wdDoc.ContentControls
        Select Case cc.Title
            Case "CustomerName"
                cc.Range.Text = customerName
            Case "OrderDate"
                cc.Range.Text = orderDate
            Case "Amount"
                cc.Range.Text = amount
        End Select
    Next cc
    ' Save and close
    wdDoc.Save
    wdDoc.Close
    wdApp.Quit
    Set wdDoc = Nothing
    Set wdApp = Nothing
    MsgBox "Word document updated successfully."
End Sub
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Doris V 625 Reputation points Microsoft External Staff Moderator
    2026-01-19T03:55:25.1233333+00:00

    Hello @Rick Neff

    Thank you for posting your question in the Microsoft Q&A forum. 

    Converting VBA code into Office Scripts can be difficult because the two use different syntax and APIs, but it's still possible to translate many basic VBA elements such as loops, conditions, and cell operations. 

    For your reference, you may visit the following community thread, which includes a user‑created tool for converting VBA to Office Scripts: Converter, VBA to Office Script. | Microsoft Community Hub 

    Note: Please note that this tool is not an official Microsoft product, and therefore you should use it at your own discretion and risk. 

    You can also post your issue on Stack Overflow, where many experienced developers actively support Office Scripts and VBA questions, and they may be able to help manually convert your code since that community has deeper coding expertise. 

    Note: Please understand that as a forum moderator, my primary goal is to provide helpful guidance and support through general troubleshooting steps. While I don’t have access to internal systems or test devices required to resolve backend/account issues, I truly appreciate your understanding of these limitations.  

    If anything is unclear, please don’t hesitate to reach out to me. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have any extra questions about this answer, please click "Comment."    

    If you want to receive the related email notification for this thread, please follow the steps in our documentation to enable e-mail notifications.  

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.