Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Jan 2001
    Location
    Grand Rapids, Michigan, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Print Label from protected document (Office XP)

    I have never received training in VBA but (with a lot of your help) have managed to get by, but now am stumped. We have a letterhead template that needs to be protected, but users will need to have the ability to print envelopes or labels from documents based on this template. I have the code for printing envelopes working, but cannot seem to get the label code to work. I've attached the code: it actually prints the label, but prints it blank. I have created the EnvelopeAddress bookmark at the correct location in the template so the address will be extracted. Any help will be greatly appreciated!!

    Thank you!


    Sub FullPrintLabel2163()
    '
    ' Unprotects form, prints 2163 label and reprotects form

    ' displays message box reminding where address will print
    Prompt$ = "Address will be printed on the first label"
    MsgBox (Prompt$)

    Application.ScreenUpdating = False

    ' unprotects document
    ActiveDocument.Unprotect _
    Password:="XXXX"

    ' prints label using letter address at EnvelopeAddress bookmark
    Application.MailingLabel.DefaultPrintBarCode = True
    Application.MailingLabel.PrintOut Name:="2163 Mini", ExtractAddress:=True, _
    LaserTray:=wdPrinterManualFeed, SingleLabel:=True, Row:=1, _
    Column:=1, PrintEPostageLabel:=False

    ' protects document
    ActiveDocument.Protect _
    Password:="XXXX", NoReset:=True, _
    Type:=wdAllowOnlyFormFields

    End Sub

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Print Label from protected document (Office XP)

    Julie,

    See below for a different approach; it should work for both envelopes and labels.
    One drawback to this approach is that the user cannot save the return address, that is, the dialog box offers the feature, but when called via code this way, it just doesn't save.

    Gary

    <pre>Public Sub EnableEnvelopes()

    Dim lngReturn As Long
    Dim rngSelection As Range
    Dim strText As String
    Dim strReturnAddress As String
    Dim objCurDoc As Document

    Set objCurDoc = ActiveDocument
    objCurDoc.Unprotect "Password"

    Set rngSelection = ActiveDocument.Tables(1).Rows(10).Cells(1).Range
    rngSelection.End = rngSelection.End - 1
    rngSelection.Select
    strText = rngSelection.Text

    strReturnAddress = strBuildReturnAddress

    With Dialogs(wdDialogToolsEnvelopesAndLabels)
    .AddrText = strText
    .EnvReturn = strReturnAddress
    lngReturn = .Show
    'May help prevent hang if she selects 'Yes' on 'Save return address?'
    DoEvents

    If lngReturn <> -1 Then
    objCurDoc.Protect wdAllowOnlyFormFields, True, "Password"
    Set rngSelection = Nothing
    Set objCurDoc = Nothing
    Exit Sub
    End If

    End With

    Set rngSelection = Nothing
    Set objCurDoc = Nothing

    End Sub
    </pre>

    <pre>Private Function strBuildReturnAddress() As String

    Dim strName As String
    Dim strAddress1 As String
    Dim strAddress2 As String
    Dim strBuffer As String

    Dim objFrmFlds As FormFields
    Set objFrmFlds = ActiveDocument.FormFields

    strName = objFrmFlds(1).Result
    strAddress1 = objFrmFlds(3).Result
    strAddress2 = objFrmFlds(4).Result

    strBuffer = strName & vbCrLf & strAddress1 & vbCrLf & strAddress2

    strBuildReturnAddress = strBuffer

    Set objFrmFlds = Nothing

    End Function
    </pre>


  3. #3
    New Lounger
    Join Date
    Jan 2001
    Location
    Grand Rapids, Michigan, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Print Label from protected document (Office XP)

    Gary,

    Thank you, I like your method much better as it allows the user to customize their envelope/label a little more. But my ignorance will show here as I'm stuck... I was getting an error at the line

    Set rngSelection = ActiveDocument.Tables(1).Rows(10).Cells(1).Range

    I figured that my document needed a table with the address in row 10. But our template doesn't include a table and I don't know what code to use to select the address. Our users usually insert addresses either via AutoText entries or via the GroupWise address book. I've attached a document from the template without the password. If you or anyone can help me I really appreciate it.

    Thank you again.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Print Label from protected document (Office XP)

    Hi Julie,

    Definitely the code in my sample would need to be adapted for your purposes.
    In the template I took that code from, I knew the address was already going to be in that specific table cell, so I just specified the contents of that cell in the code. It probably would have been better coding not to use a specific cell address, but rather to use a formfield's result or a bookmark's range.text.

    In any case, in order for this method to work, the name and address need to be gotten from somewhere so that it can be put into the string to pass into the Env. & Labels dialog. Basically, whether the name and address come in via an autotext or a GroupWise address book, there needs to be some way for the env. and label code to subsequently 'grab it', after it has been entered into the document. In my template it was easy to do because the name and address were going into a table cell.

    In the case of your template, you might be able to set it up so that the name and address, when inserted into the letter, become the range.text of a specific bookmark. It's hard to tell from the sample, where the address is to be entered, and at what point it would be entered. Could you provide some more detail as to how the name and address get entered into the letter, at what point etc. - then we might be able to tailor something to work with your template. BTW I'll be offline for the next several hours but will check back later this evening.

    Gary

  5. #5
    New Lounger
    Join Date
    Jan 2001
    Location
    Grand Rapids, Michigan, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Print Label from protected document (Office XP)

    Gary,

    I really appreciate all your help with this. I've now attached a better sample with an address & body text. I understand what you're saying about needing to have the code select the text. I have the address being inserted at a bookmark, but my question now on selecting the text is that the address isn't always the same number of lines of text. Most common would be 4 lines of text, but sometimes it's 5, maybe only 3. I'm guessing a loop can be written to select the address, but there may also be a better way that I don't know of.

    Also, can you recommend anything for me for learning VBA. Any training, books, etc? I may have trouble getting my firm to pay for the training, but I could really use it.

    Thanks again for all of your help!!

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Print Label from protected document (Office XP)

    Julie,

    The simplest way to get the full address text, no matter how many lines long it is, is to make sure it gets enclosed in a bookmark (as opposed to being inserted before or after it), when you insert the full address text when creating the letter (I was hinting at this in my previous post but didn't go into detail).
    Probably the best tutorial on this can be found in this article by Dave Rado on the MS Word MVP website.

    You can adapt the code from there, for the purpose of inserting the name and address into your letter when you create the letter.
    Then when you need to put the name and address text into a string in order to use the envelopes and label macro, you can simply use code like:

    strAddress = ActiveDocument.Bookmarks("Address").Range.Text

    Also I wanted to ask: Why have you set some sections of this template up as a protected? Normally I'd expect to see forms protection used for a template which involves formfields, which don't appear to be used here. Unless there is a compelling reason to do so, it's best to avoid using forms protection in some sections, because even in the unprotected sections, there are a number of standard Word features which become disabled. If you are protecting the first two sections to discourage users from editing the 'header' information in these sections, it would be better to just put this text into the first page header - that will eliminate the need to protect sections.

    The articles on Word VBA at the MS Word MVP site are one good resource for learning about Word VBA. There have been threads here before in which people contributed suggestions for good VBA books - unfortunately I don't think there is any one really good book for learning Word VBA so it comes down to taking bits and pieces from various places. (When the Search features gets back up and running here, the Word and VB/VBA forums here are a great resource for looking up code solutions.) Long-time Lounger Howard Kaikow posted a list of books at his website here - it may not be quite up to date but it's worth a look.

    Gary

  7. #7
    New Lounger
    Join Date
    Jan 2001
    Location
    Grand Rapids, Michigan, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Print Label from protected document (Office XP)

    Thank you for your response Gary. We've had to switch gears these past couple weeks and focus on some other issues so I haven't been able to try this. I will give it a whirl & see how I come out.

    Thanks again for all your help!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •