How to enable envelope in a protected document

B

Brent Morris

I was wondering if somebody could help me finish this macro to work
correctly.
Basically what I'm trying to accomplish is to be able to run the envelopes
and labels with a protected document.
I found a macro to spell check a protected document and that worked great,
at http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm
I then tried taking this macro and making it work for envelopes and labels.
I came fairly close, I can run the macro and the envelopes and labels dialog
box will pop up and if the user has a name and address it will bring that
into the envelope dialog box. If the user hits print it will print it
correctly and then close the dialog box and then protect the document. If
the user it's cancel they get an run-time error and the document doesn't get
protected.

Here is what I have and maybe if we can get this to work correctly maybe
this could be posted on the MVPS site for "How to enable envelopes and
labels in a protected document"



Option Explicit

Dim Cancelled As Boolean, MyRange As Range, CorrectedError As String


Sub WSNEnvelope()
'
' WSNEnvelope Macro
' Macro created 7/25/2006 by WSN
'

Dim oSection As Section, OriginalRange As Range, oDoc As Document

'If no documents open, quit macro
If Documents.Count = 0 Then
Exit Sub
End If

Set oDoc = ActiveDocument

Set OriginalRange = Selection.Range
System.Cursor = wdCursorWait

'Now unprotect the document
oDoc.Unprotect Password:="wsnforms"

Application.Run Macroname:="ToolsCreateEnvelope"
If Cancelled Then Exit Sub


'Re-protect the document
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="wsnforms"
'oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
OriginalRange.Select
Application.ScreenRefresh


'Release variables from memory
System.Cursor = wdCursorNormal
Cancelled = False
CorrectedError = vbNullString
Set MyRange = Nothing
End Sub






Thanks
Brent Morris
 
D

Dave Lett

Hi Brent,

We probably need to see your ToolsCreateEnvelope routine so that we can see
how you're handling the dialog box.

HTH,
Dave
 
B

Brent Morris

That's where I'm stuck. I'm just starting to learn how to create macros and
I happen to see some other code that used the "ToolsCreateEnvelope" so I
tried incorporating this into the macro. This is the only thing I have seen
to bring up the Envelope and Labels dialog box so far that will bring in the
name and address. I didn't write the ToolsCreateEnvelope routine so I'm
assuming it's built into Word.
 
D

Dave Lett

Hi Brent,

Hmmm, very interesting. I think you might be able to solve your issue by
calling the dialog box directly and then accounting for its options. Have a
look at the following and see if you might be able to ferret the issue out
for yourself:

With Dialogs(wdDialogToolsEnvelopesAndLabels)
'''Cancel or X in upper right corner
If .Show = 0 Then Exit Sub
''' rest of code
End With

HTH,
Dave
 
B

Brent Morris

That worked fine except using this method it doesn't grap the name and
address like the routine "Application.Run Macroname:="ToolsCreateEnvelope""
would. Is there a way to grap this info to paste into the envelope dialog
box?

Below is the code.

Thanks for help, I appreciate it.
Brent



Option Explicit

Dim Cancelled As Boolean, MyRange As Range, CorrectedError As String


Sub WSNEnvelope()
'
' WSNEnvelope Macro
' Macro created 7/25/2006 by WSN
'

Dim oSection As Section, OriginalRange As Range, oDoc As Document

'If no documents open, quit macro
If Documents.Count = 0 Then
Exit Sub
End If

Set oDoc = ActiveDocument

Set OriginalRange = Selection.Range
System.Cursor = wdCursorWait

'Now unprotect the document
oDoc.Unprotect Password:="wsnforms"

With Dialogs(wdDialogToolsEnvelopesAndLabels)
'''Cancel or X in upper right corner
If .Show = 0 Then
End If
End With

'Re-protect the document
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="wsnforms"
OriginalRange.Select
Application.ScreenUpdating = True
Application.ScreenRefresh

'Release variables from memory
System.Cursor = wdCursorNormal
Cancelled = False
CorrectedError = vbNullString
Set MyRange = Nothing
End Sub
 
D

Dave Lett

Hi Brent,

If you cancel this once, then the error number is 438 .
If you cancel this twice, then the error number is -2147352573.

'''place this at the beginning of your routine
''
On Error GoTo ErrHandler

'''run your routine (I've only included this line to make it shorter)
Application.Run Macroname:="ToolsCreateEnvelope"


'''this is what gets run if there is an error raised by running your routine
ErrHandler:
If Err.Number = -2147352573 Or Err.Number = 438 Then
'''I didn't include this in the last post (my oversight, sorry)
'''reprotect your document
oDoc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:="wsnforms"
End If

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top