Getting compilation issue with this c#code

i am getting Sequence1.xaml: No compiled code to run
error CS1513: } expected At line 44
error CS1022: Type or namespace definition, or end-of-file expected At line 54

string templatePath =“C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath =“D:\New folder.docx”;
// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Field names and values passed as arguments
string fieldName1 = args.my_phone;
string value1 = args.phone_no;
string fieldName2 = args.my_email;
string value2 = args.value2;

// Find and replace the fields in the document
ReplaceFieldWithValue(doc, fieldName1, value1);
ReplaceFieldWithValue(doc, fieldName2, value2);

// Save the document with a different name or path
doc.SaveAs2(outputPath);
}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

// Helper method to replace a field with a value
void ReplaceFieldWithValue(dynamic document, string fieldName, string value)
{
dynamic range = document.Content;
range.Find.ClearFormatting();
range.Find.Execute(fieldName, false, false, false, false, false, true, 1, false, value, 2);
}

Hi @Navneetj3,

In C#, you must escape certain characters in strings with a backslash. This includes the backslash.

e.g.

string templatePath ="C:\Users\Lenovo\Downloads\letter_template.docx";
string outputPath ="D:\New folder.docx";

string templatePath ="C:\\Users\\Lenovo\\Downloads\\letter_template.docx";
string outputPath ="D:\\New folder.docx";

image

You can also use string literals with the @ symbol at the beginning:

string templatePath = @"C:\Users\Lenovo\Downloads\letter_template.docx";

You can test out your code in a C# compiler, such as https://dotnetfiddle.net/ before inserting it into an Invoke Code Activity.

Also, this might be the forum formatting, but also make sure your quotes " are not slanted “

Thanks

I am using escape sequence …but still not able to produce correct compiled code.
as of now i am trying below.can you help me correct it .i am running same code on seperate machine with latest community studio . there it is working fine -

string templatePath =“C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath =“D:\New folder.docx”;
// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Field names and values passed as arguments
string fieldName1 = args.my_phone;
string value1 = args.phone_no;
string fieldName2 = args.my_email;
string value2 = args.value2;

// Find and replace the fields in the document
ReplaceFieldWithValue(doc, fieldName1, value1);
ReplaceFieldWithValue(doc, fieldName2, value2);

// Save the document with a different name or path
doc.SaveAs2(outputPath);
}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

// Helper method to replace a field with a value
void ReplaceFieldWithValue(dynamic document, string fieldName, string value)
{
dynamic range = document.Content;
range.Find.ClearFormatting();
range.Find.Execute(fieldName, false, false, false, false, false, true, 1, false, value, 2);
}

Hi @Navneetj3,

Can you provide a sample XAML and project.json with this issue please?

Thanks

Sequence1.xaml (6.2 KB)
project - Copy.json (1.4 KB)

please find attached

’ Get the path of the Word template and the desired output path
Dim templatePath As String = “C:\Users\Lenovo\Downloads\letter_template.docx”
Dim outputPath As String = “D:\New folder.docx”

’ Field names and values passed as arguments
Dim fieldName1 As String = my_phone
Dim value1 As String = phone_no
Dim my_email1 As String = my_email
Dim value2 As String = value2

’ Create a Word application object
Dim wordApp As Object = Nothing
Dim doc As Object = Nothing

Try
wordApp = CreateObject(“Word.Application”)

' Disable the visibility of the application
wordApp.Visible = False

' Open the template document
doc = wordApp.Documents.Open(templatePath)

' Find and replace the fields in the document
ReplaceFieldWithValue(doc, fieldName1, value1)
ReplaceFieldWithValue(doc, my_email1, value2)

' Save the document with a different name or path
doc.SaveAs2(outputPath)

Finally
’ Close and release the document
If doc IsNot Nothing Then
doc.Close()
wordApp = Nothing ’ Release the reference to the document
End If

' Quit and release the Word application
If wordApp IsNot Nothing Then
    wordApp.Quit()
    wordApp = Nothing ' Release the reference to the Word application
End If

End Try

’ Helper method to replace a field with a value
Sub ReplaceFieldWithValue(ByVal document As Object, ByVal fieldName As String, ByVal value As String)
Dim range As Object = document.Content
range.Find.ClearFormatting()
range.Find.Execute(fieldName, False, False, False, False, False, True, 1, False, value, 2)
End Sub

string templatePath = “C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath = “D:\New folder.docx”;

// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Field names and values passed as arguments
string fieldName1 = my_phone;
string value1 = phone_no;
string fieldName2 = my_email;
string valu2 = value2;

// Replace field with value using lambda function
Action<dynamic, string, string> ReplaceFieldWithValue = (document, fieldName, value) =>
{
    dynamic range = document.Content;
    range.Find.ClearFormatting();
    range.Find.Execute(fieldName, false, false, false, false, false, true, 1, false, value, 2);
};

// Find and replace the fields in the document
ReplaceFieldWithValue(doc, fieldName1, value1);
ReplaceFieldWithValue(doc, fieldName2, valu2);

// Save the document with a different name or path
doc.SaveAs2(outputPath);

}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

}

Hi @Navneetj3,

When creating a method within an Invoke Code Activity, you can try using lambda functions:

Action<dynamic, string, string> ReplaceFieldWithValue = (dynamic document, string fieldName, string value) => {
  dynamic range = document.Content;
  range.Find.ClearFormatting();
  range.Find.Execute(fieldName, false, false, false, false, false, true, 1, false, value, 2);
};

Thanks

string templatePath = “C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath = “D:\New folder.docx”;

// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Replace form field values
string fieldName1 = "my_phone";
string value1 = phone_no;
string fieldName2 = "my_email";
string value2 = email;
string fieldName3 = "my_address";
string value3 = address;

foreach (dynamic field in doc.FormFields)
{
    dynamic formField = field;
    string fieldTitle = formField.Name;

    if (fieldTitle == fieldName1)
    {
        formField.Result = value1;
    }
    else if (fieldTitle == fieldName2)
    {
        formField.Result = value2;
    }
    else if (fieldTitle == fieldName3)
    {
        formField.Result = value3;
    }
}

// Save the document with a different name or path
doc.SaveAs2(outputPath);

}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

}

string templatePath = “C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath = “D:\New folder.docx”;

// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Replace content control values
string tag1 = "Tag1";
string value1 = phone_no;
string tag2 = "Tag2";
string value2 = email;
string tag3 = "Tag3";
string value3 = address;

foreach (dynamic cc in doc.ContentControls)
{
    if (cc.Tag == tag1)
    {
        cc.Range.Text = value1;
    }
    else if (cc.Tag == tag2)
    {
        cc.Range.Text = value2;
    }
    else if (cc.Tag == tag3)
    {
        cc.Range.Text = value3;
    }
}

// Save the document with a different name or path
doc.SaveAs2(outputPath);

}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

}

string templatePath = @“C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath = @“D:\New folder.docx”;

// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Field names and values passed as arguments
string fieldName1 = "my_phone";
string value1 = phone_no;
string fieldName2 = "my_email";
string value2 = value2;
string fieldName3 = "my_address";
string value3 = value3;

// Replace field with value using lambda function
Action<dynamic, string, string> ReplaceFieldWithValue = (document, fieldName, value) =>
{
    dynamic shapeRange = document.Shapes.Range;
    foreach (dynamic shape in shapeRange)
    {
        if (shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
        {
            dynamic textFrame = shape.TextFrame;
            if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
            {
                dynamic textRange = textFrame.TextRange;
                string shapeText = textRange.Text;

                if (shapeText.Contains(fieldName))
                {
                    textRange.Find.Execute(FindText: fieldName, ReplaceWith: value);
                }
            }
        }
    }
};

// Find and replace the fields in the document
ReplaceFieldWithValue(doc, fieldName1, value1);
ReplaceFieldWithValue(doc, fieldName2, value2);
ReplaceFieldWithValue(doc, fieldName3, value3);

// Save the document with a different name or path
doc.SaveAs2(outputPath);

}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

}

using Microsoft.Office.Interop.Word;

string templatePath = @“C:\Users\Lenovo\Downloads\letter_template.docx”;
string outputPath = @“D:\New folder.docx”;

// Create a Word application object
Application wordApp = new Application();
wordApp.Visible = false;

try
{
// Open the template document
Document doc = wordApp.Documents.Open(templatePath);

// Field names and values passed as arguments
string fieldName1 = "my_phone";
string value1 = phone_no;
string fieldName2 = "my_email";
string value2 = value2;
string fieldName3 = "my_address";
string value3 = value3;

// Replace field with value in the main document
FindAndReplaceText(doc.Content, fieldName1, value1);
FindAndReplaceText(doc.Content, fieldName2, value2);
FindAndReplaceText(doc.Content, fieldName3, value3);

// Replace field with value in text boxes
foreach (Shape shape in doc.Shapes)
{
    if (shape.TextFrame != null && shape.TextFrame.HasText == MsoTriState.msoTrue)
    {
        TextRange textRange = shape.TextFrame.TextRange;
        FindAndReplaceText(textRange, fieldName1, value1);
        FindAndReplaceText(textRange, fieldName2, value2);
        FindAndReplaceText(textRange, fieldName3, value3);
    }
}

// Save the modified document
doc.SaveAs2(outputPath);
doc.Close();

}
finally
{
// Quit and release the Word application
wordApp.Quit();
}

void FindAndReplaceText(Range range, string findText, string replaceText)
{
range.Find.ClearFormatting();
range.Find.Execute(findText, ReplaceWith: replaceText, Replace: WdReplace.wdReplaceAll);
}

Action<Range, string, string> FindAndReplaceText = (range, findText, replaceText) =>
{
range.Find.ClearFormatting();
range.Find.Execute(findText, ReplaceWith: replaceText, Replace: WdReplace.wdReplaceAll);
};

string templatePath = “C:\Users\Lenovo\Downloads\07-07-2023-edit.docx”;
string outputPath = “D:\New folder.docx”;

// Create a Word application object
dynamic wordApp = null;
dynamic doc = null;

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

// Disable the visibility of the application
wordApp.Visible = false;

// Open the template document
doc = wordApp.Documents.Open(templatePath);

// Field names and values passed as arguments
string fieldName1 = my_phone;
string value1 = phone_no;
string fieldName2 = my_email;
string valu2 = value2;
string fieldName3=my_address;
string valu3=value3;

// Replace field with value using lambda function
Action<dynamic, string, string> ReplaceFieldWithValue = (document, fieldName, value) =>
{
    dynamic range = document.Content;
    range.Find.ClearFormatting();
    range.Find.Execute(fieldName, false, false, false, false, false, true, 1, false, value, 2);
};

// Find and replace the fields in the document
ReplaceFieldWithValue(doc, fieldName1, value1);
ReplaceFieldWithValue(doc, fieldName2, valu2);

ReplaceFieldWithValue(doc, fieldName3, valu3);
// Save the document with a different name or path
doc.SaveAs2(outputPath);
}
finally
{
// Close and release the document
if (doc != null)
{
doc.Close();
wordApp = null; // Release the reference to the document
}

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

}