I transfer the data to a word document with word automation. However, if the data is empty, I get output as shown in the picture. How do we ensure that a blank line appears if there is no data?

I have tried this but it didn't work.
if (dr["gorusme2"].ToString() == String.Empty)
{
myMergeField.Select();
oWord.Selection.TypeText("");
}
else
{
if (fieldName == "gorusme2")
{
myMergeField.Select();
oWord.Selection.TypeText(dr["gorusme2"].ToString());
}
}
Naimish MakwanaPosted Feb 15, 2024, 7:18 AM
In your code, when the data is empty, you are selecting the merge field and typing an empty string. This will not create a new line. Instead, you can try inserting a newline character (
\nor\r\ndepending on the system) to ensure a blank line appears when there is no data. Here’s how you can modify your code:This will insert a blank line in the Word document when
dr["gorusme2"]is empty. Please give it a try and let me know if it works for you.Thanks
Mehmet FatihPosted Feb 15, 2024, 12:13 PM
Thanks for your intrest Naimish. I have foun out the mistake. I madded a line to your code and it worked.This vas missing. if (fieldName == "gorusme2")
{
It worked like that.
if (dr["gorusme2"].ToString() == String.Empty)
{
if (fieldName == "gorusme2")
{
myMergeField.Select();
oWord.Selection.TypeText("\r\n");
}
else
{
if (fieldName == "gorusme2")
{
myMergeField.Select();
oWord.Selection.TypeText(dr["gorusme2"].ToString());
}
}
}
Naimish MakwanaPosted Feb 15, 2024, 9:37 AM
The error message “An error occurred during the operation. The object was deleted” typically occurs when you’re trying to perform an operation on an object that has already been deleted or is no longer available in memory.
In your case, it seems like the
myMergeFieldobject is getting deleted before theTypeTextmethod is called. This could happen if themyMergeFieldobject is being manipulated elsewhere in your code outside of the provided snippet.Here are a few things you can check:
myMergeFieldis not being deleted or disposed of elsewhere in your code before this block is executed.myMergeFieldobject.myMergeFieldobject is properly initialized and not null when this block of code is executed.Thanks
Mehmet FatihPosted Feb 15, 2024, 9:13 AM
Thanks Naimish but it is giving this error. "An error occurred during the operation. The object was deleted"