Invoke Method Word SaveAs2

I am trying to implement the Invoke Method for SaveAs2 when saving word doc.

Here is what I have:

wordApp = New Microsoft.office.Interop.Word.Application()

wordApp.Visible = False

docx = wordApp.Documents.Add()

Read activity that reads the text file output is set to: strTxt

docx.Content.Text = strTxt

docxFilePath = c:\Users\test\test.docx

I am having issues with the Invoke Method activity in achieving this using the SaveAs method. I would greatly appreciate some assistance in achieving this. Thank you :slight_smile:

@j1osu2006,

Try this code.

string docxFilePath = @"c:\Users\test\test.docx";
dynamic wordApp = null;
dynamic doc = null;

try
{
    wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Visible = false;

    // Open or create the document
    doc = wordApp.Documents.Add();
    doc.Content.Text = strTxt;

    // Save as .docx file
    doc.SaveAs2(docxFilePath);
}
finally
{
    // Close and release the document
    if (doc != null)
    {
        doc.Close();
    }

    // Quit and release the Word application
    if (wordApp != null)
    {
        wordApp.Quit();
    }
}

@j1osu2006

The earlier one is C# code. Here is vb expression.

Dim docxFilePath As String = "c:\Users\test\test.docx"
Dim wordApp As Object = Nothing
Dim doc As Object = Nothing

Try
    wordApp = CreateObject("Word.Application")
    wordApp.Visible = False

    ' Open or create the document
    doc = wordApp.Documents.Add()
    doc.Content.Text = strTxt

    ' Save as .docx file
    doc.SaveAs2(docxFilePath)
Finally
    ' Close and release the document
    If doc IsNot Nothing Then
        doc.Close()
    End If

    ' Quit and release the Word application
    If wordApp IsNot Nothing Then
        wordApp.Quit()
    End If
End Try

LLM helped me to write this but it’s validated by me.

How can this be achieved using the Invoke Method activity in UiPath. I’m trying to do this without using the invoke code activity

@j1osu2006,

The code is for Invoke code and I don’t think this could be achieved with invoke method.