Delete space character after an empty Field

Is it possible to delete an space character after an empty Merge Field, or any other text from the word document.

thanks
Michael

Hi Michael,

Thanks for your inquiry. When you insert a merge field into a document, you can specify text that will be inserted before or after the merge field. See the following fields code:

{ MERGEFIELD mymergefield \b "text before" \f "text after" \* MERGEFORMAT }

If value of merge field is empty, then text before and text after will not also inserted. I suppose this is what you are looking for. (see also attached screenshot)

Best regards.

thx it works fine.
But is there a possibility to delete also single characters or a entire paragraph. I have an paragraph on my template what i have to delete if it is need. I can also set a bookmark an write the text on the template with aspose, it will be the the same thing from the other side but i would to it removing the paragraph.
thx

Hi

Thanks for your request. If there is no content in the paragraph except merge field, you can use RemoveEmptyParagraphs option to remove such paragraphs. Please see the following link for more information:
https://reference.aspose.com/words/net/aspose.words.mailmerging/mailmergecleanupoptions/
In addition, you can try using MergeField event handler in this case. Please follow the link to learn more:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
Hope this helps.
Best regards.

no there aren’t mergefields in the paragraph. The paragraph is a simple TEXT Paragraph like
BLA BLA BLA BLA
i would remove the paragraph if in the DB a Value is set to TRUE if it is FALSE i would leave the paragraph as it is

Hi

Thank you for additional information. I think you can insert bookmark in the paragraph that should be removed and remove it using code like the following:

// Open document
Document doc = new Document(@"in.doc");
// Remove paragpaph that contains bookmark
doc.Range.Bookmarks["mybk"].BookmarkStart.ParentNode.Remove();

Hope this helps.
Best regards.

Ok thx, it works fine.

An other question, is it possible to set the cursor to a position in the text and to delete there any character. Like clicking backspace.
For example I hav an Paragraph with the text :
“My namee is Michael”

i would set the cursor after “namee” and delete the last “e”

is anything like that possible, in practice I would delete any character from anywhere in the document.

thx

Hi

Thanks for your inquiry. All text in a Word document is represented as Runs. Please see Document Object Model for more information:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
So, you can edit text of particular Run and remove part of its text.
Best regards.

If I have an MergeField in a Table which I use the “Text to be inserted after :” the text inserting after the MergeField if it hase any value it doesn’t work.

can you help me

thx

Hi Michael,

Thanks for your request. Please attach sample template, expected output and code that will allow me to reproduce the problem. I will investigate the issue and provide you more information.
Best regards.

in the footer we have a table with the MergeField “FUNK_BEZ_D”, if the isn’t empty than after it should be a space character what can I do it as an paramter in MergeField.

But in that case it doesn’t work, if the MergeField is outside of an table it does work fine.

So the output is for example
------------------------------------------------
| RevierleiterBurger Michael
------------------------------------------------

but there should be a space between “Revierleiter” and “Burger”

thx

Hi

Thank you for additional information. However, I cannot reproduce the problem on my side. I used the following code for testing:

Document doc = new Document("C:\\Temp\\TestTables.doc");
doc.getMailMerge().execute(new String[]
    {
        "FUNK_BEZ_D",
        "PERS_NACHNAME",
        "PERS_NAME"
    },
    new Object[]
    {
        "Revierleiter",
        "Burger",
        "Michael"
    });
doc.save("C:\\Temp\\out.doc");

The output is correct; there is space between “Revierleiter” and “Burger”.
Best regards.