How can I use UiPath with C# Invoke Code to open a Word document and insert "Hello World" with center alignment?

I want to use UiPath with C# Invoke Code to open a Word document and insert the text “Hello World” with center alignment. How can I do this?

This quote literally entered as a prompt in ChatGPT:

I didn’t validate the code (simply too lazy) but it should give you a decent code snip to work with.

Here the error, when i try to run the workflow…

Error ERROR Validation Error No compiled code to run
error CS1001: Identifier expected At line 0
error CS1001: Identifier expected At line 1
error CS1009: Unrecognized escape sequence At line 5
error CS0118: ‘System’ is a namespace but is used like a type At line 0
error CS0210: You must provide an initializer in a fixed or using statement declaration At line 0
error CS0118: ‘Word’ is a namespace but is used like a type At line 1
error CS0118: ‘Microsoft.Office.Interop.Word’ is a namespace but is used like a variable At line 1
error CS0136: A local or parameter named ‘filePath’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter At line 5
error CS0234: The type or namespace name ‘Application’ does not exist in the namespace ‘UiPath.Word’ (are you missing an assembly reference?) At line 8
error CS0234: The type or namespace name ‘Application’ does not exist in the namespace ‘UiPath.Word’ (are you missing an assembly reference?) At line 8
error CS0234: The type or namespace name ‘Document’ does not exist in the namespace ‘UiPath.Word’ (are you missing an assembly reference?) At line 12
error CS0234: The type or namespace name ‘Paragraph’ does not exist in the namespace ‘UiPath.Word’ (are you missing an assembly reference?) At line 15
error CS0234: The type or namespace name ‘WdParagraphAlignment’ does not exist in the namespace ‘UiPath.Word’ (are you missing an assembly reference?) At line 17 CobaMacro.xaml

Hi @Vhierdy_Hafidz

Use the below C# code in Invoke Code:

// Create a new instance of Word application
var wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = true; // Set Word visible

// Create a new document
var document = wordApp.Documents.Add();

// Add a new paragraph and insert "Hello World"
var paragraph = document.Content.Paragraphs.Add();
paragraph.Range.Text = "Hello World";

// Align the text to the center
paragraph.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

// Save the document to a specific path
string savePath = @"C:\Users\" + Environment.UserName + @"\Documents\UiPath\Dummy\Document.docx"; // Use @ to create a verbatim string
document.SaveAs2(savePath);

// Optionally, you can close the document after saving
document.Close();
wordApp.Quit();

Note: Please change the path savePath according to yours.

Output:

Hope it helps!!

1 Like

You may need to remove those Using statements and import the namespaces in the project itself if not yet done.

Also check if your project is set to VB or C# looking at the ‘line 5 error’.
The similair solution also works in VB, you just need to use VB syntax. The used interop methods remain the same.

Thankyou sir @Jeroen_van_Loon and miss @Parvathy for the response :pray:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.