Code ignores last command

J

jeanmac

I've been struggling with this for ages, being a VBA novice, and just when
everything seemed to be doing what I wanted it to do, the code suddenly
ignores the fact that I'm telling it to take off the protection in section 2
after Emailing a copy of the document. This is because I want to be able to
edit the original. It was working beautifully - I don't understand. Can
anyone please help??

Code is:

Sub Send_As_Mail_Attachment()

' send the document as an attachment in an Outlook Email message
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Prompt the user to save the document
ActiveDocument.SaveAs ("")

'unprotect the two protected sections of the document (need to do this
before protecting all)
ActiveDocument.Unprotect Password:="iknow"

'protect the whole document as read only before sending
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = True
ActiveDocument.Protect Password:=" iknow ", NoReset:=False, Type:= _
wdAllowOnlyFormFields

ActiveDocument.Save

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

' 'Outlook wasn't running, start it from code
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for a copy
.CC = "(e-mail address removed)"
'Add the document as an attachment, you can use the .displayname
property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName,
Type:=olByValue, _
DisplayName:="Document as attachment"
.Display
End With

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing

'reset protection in the original document
ActiveDocument.Unprotect Password:=" iknow "

'original protection settings
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = False
ActiveDocument.Sections(3).ProtectedForForms = True
ActiveDocument.Protect Password:=" iknow ", NoReset:=False, Type:= _
wdAllowOnlyFormFields
ActiveDocument.Save

End Sub
 
J

jeanmac

I know now why the code isn't working, it's because Word was set as the Email
editor, when I changed it to HTML without Word there was no problem. Does
anyone know if it is possible to code the message format into the subroutine?
I've been tearing my hair out for the past three hours trying to sort this
out, and it seems crazy that it's the Email editor that was the problem.

Cheers
Jean
 
G

Graham Mayor

The problem is the order in which you run the code - and it shouldn't matter
what format or editor Outlook uses
(I have removed the passwords for testing).


Sub Send_As_Mail_Attachment()

' send the document as an attachment _
in an Outlook Email message
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Prompt the user to save the document
With ActiveDocument
'unprotect the two protected sections of the document _
(need to do this before protecting all)
.Unprotect Password:=""
.Protect Password:="", NoReset:=False, Type:= _
wdAllowOnlyFormFields
'protect the whole document as read only before sending
.Sections(1).ProtectedForForms = True
.Sections(2).ProtectedForForms = True
.Sections(3).ProtectedForForms = True
.Save
End With

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

' 'Outlook wasn't running, start it from code
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for a copy
.CC = "(e-mail address removed)"
'Add the document as an attachment, you can use the _
.displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, _
Type:=olByValue, DisplayName:="Document as attachment"
.Display
End With

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing

'original protection settings
With ActiveDocument
.Unprotect Password:=""
.Protect Password:="", NoReset:=False, Type:= _
wdAllowOnlyFormFields
.Sections(1).ProtectedForForms = True
.Sections(2).ProtectedForForms = False
.Sections(3).ProtectedForForms = True
.Save
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jeanmac

Hi Graham

Thanks for your reply. I copied your code, and although it works very well,
the recipient's Email is not protected, they can edit it, which is what we
don't want. I've tried juggling it around a bit, but it doesn't seem to
work. Do you have any idea what the problem is?

Thanks for your help.

Jean
 
G

Graham Mayor

The attached document is protected for forms - all sections? What do you
mean by 'the recipient can edit it'?
Ultimately it is quite simple to remove the protection from a form - are you
expecting something different?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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