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
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();
}
}
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.