• Print Labels (VBA/Word/97+)

    Author
    Topic
    #415560

    Hello

    I had a request to create a macro in MS Word that allowed a user to specify which label to print (or not print) when just a single or a couple of labels were required. This happens when a sheet of labels hasn’t been completely used and there may be a few left.

    So far I have created a Userform that has options in a frame and checkboxes that the user clicks to specify the type of label sheet they are using (eg 3 x 8) and the postion of the label to be printed (via the checkbox). My project is not complete as I am not sure about how to step through the cells of the page that has the labels data.

    The attached is a screen capture of the form and I’ll happily post the code for anyone that is interested in helping with the project.

    I imagine the finished project will be either a menu or toolbar button option but could also be an add-in for word with an attendant toolbar button.

    Suggestions welcome.

    Viewing 1 reply thread
    Author
    Replies
    • #927130

      Will the source data in the Word document always be in the same layout as the label setup chosen in the left hand frame? Or do you need to convert the data to the chosen label setup? (If not, what happens if the user selects a label setup that does not conform to the layout of the document?)

      • #927259

        I should give some more detail.
        * This process is not intended for mail-merge.
        * Currently there are a number of templates that match the label sheets with the required labels already on them.
        * The labels are meant for product (cans/cartons) and are printed as required.
        * Sometimes, not a whole sheet is used, and rather than waste it, currently the template is opened and the ‘not to be printed’ labels are changed to white font.
        * The user will choose the label type from the left frame and then select the labels to print from the right frame checkboxes.

        Currently, only the label types shown are in use. (ie 2 across by 2 down, 2 across by 4 down,… 3 across by 8 down) but the form can be modified to suit the actual template (read label sheet type).

        Does that make it easier to understand?

        • #927261

          You could use something like this – adapt as needed. It assumes:

          – You have named the check boxes in the right hand frame chkR1C1, chkR1C2 etc. This makes it possible to loop through the check boxes by row and column number.
          – You have defined and assigned variables lngNumberOfRows and lngNumberOfColumns according to the label type selected in the left hand frame.

          Private Sub cmdOK_Click()
          Dim i As Long
          Dim j As Long
          Dim tbl As Table
          Set tbl = ActiveDocument.Tables(1)
          For i = 1 To lngNumberOfRows
          For j = 1 To lngNumberOfColumns
          If Me.Controls(“chkR” & i & “C” & j) = False Then
          tbl.Cell(i, j).Range.Delete
          End If
          Next j
          Next i
          Set tbl = Nothing
          Unload Me
          End Sub

          You can replace …Range.Delete by something else that will suppress printing, such as making the text hidden, or white, if you prefer.

          • #927296

            Thanks Hans,

            That code was exactly what was needed – I did modify it to print the request and just to make sure that the labels template was closed without saving changes.

            Do you think other Loungers would benefit from the whole code being posted?

            • #927300

              Yes, if you don’t mind sharing it, it would be nice if you posted the result. If the code is longish, please post it in an attachment; if you want to post the template, either rename it to .doc, or zip it – you can’t attach a .dot file to a post. Thanks!

            • #927305

              Attachment contains a macro suitable for selecting which labels you want to print from a sheet when the Avery (or whichever brand) label sheet has been partly used.

              If you think the code is not efficiently written then by all means please post a reply. I am happy to see others suggestions.

              To use this code I would think that you would open the MS Word attachment and copy the userform (frmLabelSelect) and the module (labelPrint) to your Normal.dot template. Then create a simple toolbar button to run the macro.

              If you have any suggestions on improvements please post.

            • #927307

              Most of it is fine, but this bit

              MsgBox “Click OK to close file without saving changes or Cancel to return to document.”, vbOKCancel
              If vbOK = 1 Then
              ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
              Else: End
              End If

              won’t work as intended. You actually don’t do anything with the button clicked by the user in the message box. Try this instead:

              If MsgBox(“Click OK to close file without saving changes or Cancel to return to document.”, vbOKCancel + vbQuestion) = vbOK Then
              ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
              End If

              Depending on the average utilization of the label sheets, you may want to turn on all visible check boxes when the form is opened.

            • #927317

              Leigh

              I thought I would have a play to make reduce some of the code and add another button to help tick the remaining checkboxes. It is not going to run any faster but should be quicker to maintain this way.

              I didn’t make Hans suggested change so if you use this version you will need to add that in. I would also default the checkboxes as filled although I think my addition of the add remainder simplifies the task of checking all the boxes. The new button is meant to be used when one of the checkboxes is ticked to tick the subsequent ones. Your mileage will vary if multiple checkboxes are selected when the button is pressed sad

            • #927318

              Andrew, I must lead a sheltered life – but I must admit that I like what both you and Hans have offered!! Just brilliant and I am very appreciative of the insight I have gained from your replies.

              Attachment is the revised version for any Loungers that are following this thread. It incorporates the features mentioned by both Hans and Andrew and a small cleanup to the userform and added a toolbar button to a new toolbar that is attached to the document. As before, I would suggest that the userform, module and the toolbar button be moved to your Normal.dot so that it can be available whenever you need to print partial label sheets.

              It is not intended for use with the mailmerge command – although maybe it could be modified.

            • #927355

              Leigh

              I would set this to work without adding it to Normal.dot. I think it is specialised enough to be of little use unless working in a very restricted set of documents. Because of the specialised nature I would tend to not clutter the Normal template with it.

              You can do this by saving it as a template and then going into Tools > Templates and Addins and adding it to the addin list. I would then leave it ‘ticked off’ in everyday use but when you want it you can go to this dialog and turn it on to gain access to it. If you leave it ‘ticked on’ then you have it anytime you like but you can free up a little memory by leaving it off when it isn’t needed.

            • #927366

              Thanks Andrew, I take your advice and agree that it would be best as an occasional add-in due to its expected limited use.

              Thanks for your input…

    • #927132

      I’m with Hans – you should be obtaining the page layout from the document layout rather than asking the user to describe the page.

      You should also verify that this is a mail merge document before showing the form at all.

      Once that is done, I would ask for the number of rows before the labels start and insert that many empty rows at the top of the table. I wouldn’t bother with the unused columns in the topmost row on the grounds that the effort is not worth it. The money they save in stationery costs is going to be overpowered by the time it takes to code for it and the time it takes them to provide that information correctly.

    Viewing 1 reply thread
    Reply To: Print Labels (VBA/Word/97+)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: