Hiding a barcode in a mailmerge in Word

Milan | KRS 1 Reputation point
2021-12-08T10:54:14.047+00:00

Hi there!

I'm trying to hide a barcode on a label if the barcode value is empty, but I'm having no luck with the IF-statement.

This is what I have now:

{ IF { MERGEFIELD Barcode } = "" "' "{MERGEBARCODE Barcode CODE128 \h 1927 \t }" }

The IF statement works, but I can't get it to display the barcode when the IF-condition is false. It does not display anything.

This does work for example:

{ IF { MERGEFIELD Barcode } = "" "' "display this text" }

I also tried with DISPLAYBARCODE, but same result.

Just returning the 0 string still displays a small barcode, but I want it to be empty so the layout can shift up.

Thanks in advance!

Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
892 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Peter Jamieson 116 Reputation points
    2022-03-30T14:29:06.927+00:00

    One of the field codings that Emily Hua tried originates from this conversation:

    https://social.technet.microsoft.com/Forums/Lync/en-US/a3baa5d0-d808-4fc9-8de9-4e09e44a9a7b/word-2016-displaybarcode-not-working-when-used-inside-an-if-statement-in-mail-merge?forum=word

    However, the original (which I believe does what is necessary) is this:

    { IF "{ MERGEFIELD Ticket_Number }" = "" "{ SET tn " " }{ SET h 1 }" "{ SET tn { MERGEFIELD Ticket_Number } }{ SET h 1300 }" }{ DISPLAYBARCODE "{ tn }" CODE128 \t \h { h } }

    There are two main differences:

    • the original has { SET tn " " } (i.e. sets tn to a space) rather than { SET tn "" } . If you set tn to an empty string you just end up with the error message
    • the original has { SET h 1 } rather than { SET h "" }. This is crucial because setting h sets the height of the image to 1 rather than 1300, and in fact that is what really makes this work.

    This technique still inserts something rather than nothing, but I think it is the smallest something that can easily be achieved using field coding rather than post-merge edits.

    As usual, all the {} pairs have to be the special field code brace pairs that you can typically insert on Windows desktop Word using ctrl-F9.

    As mentioned in the Technet conversation, there is a sample document+data source:

    'I have uploaded a simple example to https://1drv.ms/u/s!AgOi7jMKouNNnnQr4HdM1cq3VCcM . If you want to try it, you will need to...

    • download the .zip file and unzip the two files into a folder. The mail merge main document is mmmd.docx and the data source document is mmds.docx
    • open mmmd.docx and use the mailmerge tab to connect it to the data source
    • preview the merge
      • perform the merge to a new document. There are some deliberate problems in the data source and some coding that will deliberately fail in those cases, so you will see a few popups.
      The barcodes do not appear if you merge to email using HTML format. I have not checked merge to printer or merge as attachment.

    If you have the time to check that and it does not work on your system that would be useful to know (but of course, the problem then is "what's the difference?")'

    This particular technique can't be used with MERGEBARCODE. Also, as far as I can tell, DISPLAYBARCODE and MERGEBARCODE are still not available on the Mac desktop version of Word.

    1 person found this answer helpful.
    0 comments No comments

  2. Emily Hua-MSFT 27,526 Reputation points
    2021-12-09T10:28:48.283+00:00

    Hi @Milan | KRS

    Welcome to Q&A forum ~

    Currently I could get the barcode image, but I could not let it show blank when your Barcode data is blank.
    156272-capture41.jpg
    156199-capture42.jpg
    156237-capture43.jpg

    I try to use "" as TrueText when the {MERGEFIELD Barcode}= "" is True, but it also does not work.

    If I have any updates or summary, I would update my reply.

    -------- Update ----------

    @Milan | KRS
    Based on my tests, we could not use If field to hide the "Data length is not supported by current type." when barcode value is empty.

    If needed, you could use Find and Replace to find the empty value for barcode, use one character to fill these blanks, use use "0" on my sample.

    Then use this field formula {IF"{MergeField Barcode}"<>"0" "{Set X {MergeField Barcode}}{Set h 1927}"}{DisplayBarcode"{X}" Code 128 \t\h{h} "{Set X ""}{Set h ""}"}, we could get a blank barcode without the error message "Data length is not supported by current type.".
    157166-capture62.jpg
    157130-capture63.jpg


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

  3. Milan | KRS 1 Reputation point
    2021-12-09T11:11:09.267+00:00

    Thank you for your input @Emily Hua-MSFT !

    It's strange, because the barcode works on its' own, as does the IF-statement. But as soon as you put them together, it stops working.

    Also with MERGEBARCODE and Displaybarcode it works the opposite. With Displaybarcode it works when Barcode has a value, but does not when it's empty. While with MERGEBARCODE it only works when it's empty. When Barcode has a value it displays nothing at all and when I tell it to display the formula, the correct barcode is right there, inside the formula (see screenshot). But when Barcode has no value, it does what it's supposed to do and disappears. I don't understand it.

    156169-schermafbeelding-2021-12-09-om-120541.png