Leading zero in MS Word 2007 document is not displaying

Hello,

I’m using MS Word 2007 document. In the document mergefields are used to insert some address data from database.
Issue is - When I get any 5/9 digit Zipcode starting with a zero, the leading zero is dropped out.

I tried one code like this -
mergefield ZipCode {if {mergefield ZipCode} > "99999" {mergefield ZipCode #"00000’-‘0000’" {mergefield ZipCode "# "00000"}

Above
is working fine for 9 digits zipcode starting with zero, however this
not displaying the same code is not working for 5 digits zipcode.
eg., zipcode(02312-1234) with 9 digits is shwon as - 02312-1234
and 5 digits zipcode(02312) is displayed as - 00000-2312

Please reply if there’s any good solution for this.
It should work for both 5 and 9 digits zipcode.

Thanks.

Hi Rasika,
Thank for your inquiry, please go through the following code . Hopefully it will help to resolve the issue.

Document doc = new Document(@"E:\aspose assignment\Data\SampleMM.doc");
doc.MailMerge.FieldMergingCallback = new HandleGroupElement();
doc.MailMerge.Execute(new string[] { "ZIP_Code" }, new object[] { "23121234" });
doc.Save(@"E:\aspose assignment\Data\out.doc");
private class HandleGroupElement : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldName.Equals("ZIP_Code"))
        {
            // Move DocumentBuilder cursor to the field.
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToField(e.Field, false);
            // Insert value & applying format to the mergefield using DocumentBuilder after checking length 5 or ?/9
            if (e.FieldValue.ToString().Length < 6)
                builder.Write(String.Format("{0:00000}", Convert.ToInt32(e.FieldValue.ToString())));
            else
                builder.Write(String.Format("{0:00000-0000}", Convert.ToInt32(e.FieldValue.ToString())));
            // Set text of the field to the empty string.
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
    }
}

Pleaes feel free to contact us for any further assitance we will be glad to extend our support.
Best Regards,
Tilal Ahmad

Hi,

Thanks for your inquiry. Could you please attach your input template document here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Hello,

I have one doubt regarding this line–

doc.MailMerge.FieldMergingCallback = new HandleGroupElement();

I’m not getting the above property when I try to create a new object. Can you tell me what needs to be imported on the page where I’m generating documents?
Also, IFieldMergingCallback this is not found…
Is above differene is a version issue? The aspose version in my code is 7.0.0

Thanks for your reply it seems to be solving my issues for zipcode.
-Rasika.

Dear Rasika,
Thanks for the inquiry. First of all please upgrade Aspose.Word to latest version Aspose.Words v11.3.0 to avoid any potential issue. Moreover in reference to code snippet, MailMerge class provides two events IFieldMergingCallback.FieldMerging and IFieldMergingCallback.ImageFieldMerging to implement custom control over the mail merge process.We will suggest you to follow given below link for further details on the topic. Please import Aspose.Words.Reporting namespace for using MailMerge class.
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Furthermore, following code is used to add a event handler to MergeField event.
doc.MailMerge.FieldMergingCallback = new HandleGroupElement();
Please feel free to contact us for any further assistance.
Regards,
Tilal

Hello,
As per your last reply I have downloaded latest vesrion for Aspose words from this link -
https://releases.aspose.com/words/net

Our .Net framework is 2.0 so, we took file from folder - .net 2.0, This file is then placed in Bin folder of the project.
After running the project when we are opening the document we got error as below. Please see the line below for your reference.

Document.Save(fileName, SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, _
System.Web.HttpContext.Current.Response)
Return "Success"

error we got is -
‘SaveType’ is not a member of ‘Words’.

Can you please provide some steps to upgrade Aspose.words version from 7.0.0 to 11.3.0 ?
do we need to check for any other files in the environment?

Thanks.

Hello,
After a bit search on the errors that we are getting now in project, we found that the latest version of Aspose.words is not compatitble with the old one… My project is in maintainance phase and its not feasible to upgrade the version now.
following links were checked for this issue–
https://docs.aspose.com/words/net/

Can you suggest any other way to implement solution for this issue?

Thanks.

Hi Rasika,
Thanks for your inquiry and sorry for the inconvenience faced. We will look the migration issue in detail. Meanwhile, please modify zip code field in your template file, as following
{ IF{ MERGEFIELD ZIP_code }>99999{MERGEFIELD ZIP_code \# "0####'-'####"}{MERGEFIELD ZIP_code \# "0####"}}
Hopefully it will serve your purpose. Please let us know for any further assistance required.
Regards,
Tilal Ahmad

Hello,

As I mentioned in my question, this merge-field format-code is not working for 5 digits zipcode…
{IF {MERGEFIELD ZipCode}> 99999 {MERGEFIELD ZipCode#“00000’-'0000”} {MERGEFIELD ZipCode#“00000”}} whereas its works fine for 9 digits zipcode.
So, if zipcode = 04567, after merging doc it gives – {if 4567 > 99999 “00000-4567” “04567”}as we see, the condition is giving proper o/p,
after alt+ f9(after hiding codes) output is – 4567 only…
The database field for zipcode is a varchar…still its giving issue in merging this zipcode.

I tried the one you have given - { IF{ MERGEFIELD ZipCode }>99999 {MERGEFIELD ZipCode # “0####’-’####”}{MERGEFIELD ZipCode # “0####”}}
After merging doc it shows – {if 4567 > 99999 “00000-4567” “04567”}condition is executing properly,
and alt+f9 (after hiding codes) - “Error! Unknown op code for
conditional

Please let me know solution for this.

Thanks.

Hi,

We apologize for your inconvenience. Could you please attach your input template Word document here for testing? I will investigate the issue further and provide you more information. It is safe to attach files in the forum. If you attach your document here, only you and Aspose staff members can download it. You can also remove any sensitive information by replacing it with dummy data instead.

Best Regards,

Hello,
Please chekc the attached file.

Thanks.

Hi Rasika,
We apologize for delayed reply, we’re working over your most recent query and will get back to you soon.
Best Regards,

Hi Rasika,
While using the latest version of Aspose.Words i.e. 11.4.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system as WORDSNET-6432. Your request has also been linked to this issue and you will be notified as soon as it is resolved.
Sorry for the inconvenience.
Best Regards,

Hello,

I’m just taking a follow-up if you have got any solutions for this issue…

Thanks,
Rasika T.

Hi Rasika,
Thanks for your inquiry.
I have verified the status of this issue from our issue tracking system and regret to share that this issue is still unresolved. Currently it is pending for analysis and is in the queue. Also, at the moment I can’t provide you the ETA for this issue. However, we will inform you via this forum thread as soon as it’s resolved.
Meanwhile, as a work around could you please test attached nested if mail merge syntax.
Sorry for inconvenience.
Best Regards,

Hello,

Thanks for your quick reply.
I checked the mergefield codes given in the document you have provided.
It worked good for 9 digits zipcode.
But its not showing 5 digits zipcode with leading zeros.
(However, it worked initailly when I had added this code, but in next previews for the doc it was not merging).

Please check attached screenshot of the document I have generated through application.
It would be easier if there’s any code in .net to cover this issue, since its not feasible to update several files to add mergefield code to zipcode fields.

Thanks,
Rasika T.

Hi Rasika,
Sorry for the inconvenience faced. Sure, we will keep you informed of any developments and let you know once your problem is resolved so you can fix the issue using Aspose.Words API.
Best Regards,

Hello,

"Can i know the status of the my inquiry about “Leading Zero Issue”? I have checked the attached nested ‘if mail merge syntax’, but it did not work. Shall i need to upgrade Aspose version from 7.0 to 11.0 or Is there any other alternative work around available for that?

Thanks
Rasika T

Hi Rasika,

Thanks for your inquiry. I am afraid, the implementation of this issue has now been postponed till a later date and we cannot push it into production right now because there are many other important issues we have to work on. Rest assured, we will inform you as soon as this issue is resolved. We apologize for your inconvenience.

Best regards,

Hi,

Any update on this? Is this fixed?

Thanks,
Pradnya