PDA

View Full Version : Open Dialogue Boxes Vs Outlook



davidboutche
08-05-2009, 05:27 AM
I have the following code as part of a larger file that should run the email procedure that would select certain book marks and compose and email ready to be send as part of an IF Statement.

The Else Statement should run another email procedure.

However, when it comes to executing the email procedure if gives the following error message:

"Microsoft Office Wordmail could not be started. Close any open dialogue boxes and try again."

Private Sub UserForm_Initialize()
violencecombo_box.AddItem "YES"
violencecombo_box.AddItem "NO"
supervisorRankComboBox.AddItem "Acting Sergeant"
supervisorRankComboBox.AddItem "Sergeant"
supervisorRankComboBox.AddItem "Acting Inspector"
supervisorRankComboBox.AddItem "Inspector"
End Sub
Private Sub ChecklistnextButton_Click()
If violencecombo_box.Value = "YES" Then
MsgBox ("There are no PNDs that are suitable for issue when violence has been used. If you try to proceed with this PND, the ticket WILL be cancelled.The offender WILL be given their money back.Instead, make contact with CJD immediately and ask for help with a suitable disposal.")
Call eMailActiveDocument
'ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Application.Quit
Else: Call eMailCIBNonCourt
End If

Call FillBM("supervisorrankbook", genericcheckform.supervisorRankComboBox.Text)
Call FillBM("supervisornamebook", genericcheckform.supervisornamebox.Text)
Unload Me
End Sub

Also I can't work out how to split the long msgbox using the _.

When I comment out the email procedures it works fine each time.

The email procedures are as below using late binding (i think).

Option Explicit
Sub eMailActiveDocument()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
Set Doc = ActiveDocument
With EmailItem
.Subject = "Alternate disposal for PND required"
.Body = "PND " & UserForm1.PNDbox.Text & " has been incorrectly issued. It has been identified that the offence for which the PND was issued does not fit within the scope of the scheme. The PND was issued on " & UserForm1.issuedatebox.Text & "." & vbCrLf & _
"Please make contact with me to discuss alternative disposals for this offence." _
& vbCrLf & "Regards" & vbCrLf _
& UserForm1.officerbox.Text
.To = "Insert your local case director email"
.Importance = 1 ' olImportanceHigh 'Or olImprotanceHigh Or olImprotanceLow
' .Attachments.Add Doc.FullName
' .Send
.Display
End With

Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub

Sub eMailCIBNonCourt()

Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(0)
Set Doc = ActiveDocument
' Doc.Save
With EmailItem
.Subject = "A PND has been issued"
.Body = "A Penalty Notice for Disorder has been issued out of custody. The PND is number " & _
UserForm1.PNDbox.Text & "for" & UserForm1.offenceCombo_Box.Text & "Issued by: " & UserForm1.officerbox.Text
.To = ".Non Court Disposals; .Central Input Bureau"
.Importance = 2
' .Attachments.Add Doc.FullName
' .Send
.Display
End With

Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub


Any help, really appreciated. I've been looking at this for days and can't work out why.


:think:

lucas
08-05-2009, 06:50 AM
You have some other problems but I would do what the message tells you to do to get past this problem. IE, close the userform and then call the email procedure:

Private Sub ChecklistnextButton_Click()
If violencecombo_box.Value = "YES" Then
MsgBox ("There are no PNDs that are suitable for issue when violence has been used. If you try to proceed with this PND, the ticket WILL be cancelled.The offender WILL be given their money back.Instead, make contact with CJD immediately and ask for help with a suitable disposal.")
Unload Me
Call eMailActiveDocument
'ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Application.Quit
Else: Call eMailCIBNonCourt
End If

Call FillBM("supervisorrankbook", genericcheckform.supervisorRankComboBox.Text)
Call FillBM("supervisornamebook", genericcheckform.supervisornamebox.Text)
Unload Me
End Sub

davidboutche
08-05-2009, 07:15 AM
Thanks Steve, that solved it.

You might have also noticed that I'd commented out activedocument.close.

That's because I hadn't realised (but seems obvious now) that the email had become the active document.

I actually wanted to close the word document. What's the code for this please?

Also how do I split the message box over several lines for easier reading? I tried break it up with space_ . But that didn't seem to work. It kept giving me the message "expected : (".

lucas
08-05-2009, 07:25 AM
You have the code. You just need to put it in your email procudures and you need to make the word doc active or call it specifically in that procudure. If you close the word doc before all of your code is run your code will stop running.

lucas
08-05-2009, 07:26 AM
on the msgbox code, I don't think you can break a msg as you wish to do. I think...

davidboutche
08-05-2009, 08:31 AM
On the similar note ref the active document. I was having problems with further FillBM() because it was refering to active document which was at the time the email message but the code was still running to edit the word document. I got around this by just rearranging the order of the fillBM. But for future use, how do I tell it what is the activedocument or alternatively apply specific command to specific documents?