This is my first time using VB in anger, I'm not a programmer, my previous experience has been Unix/C/Perl and some Delphi under Windows. I've now read various VBA books / web / sample code / help pages until the point where I'm thoroughly confused and I know I'm missing something dumb. Anyhow...
I'm trying to write some vba so that from an email inspector, I open a form to query a data base to decide where the email will be saved, then from the form save and delete the email.
I've cracked the db stuff and I can save the email succesfully with 'Application.ActiveInspector.CurrentItem.SaveAs' but the .Delete method always gives
Run-time error '-2147352567 (80020009)': Outlook cannot delete this item.
(as far as I can tell that error number is just 'undefined')
Now I can succesfully delete emails via the MAPI route with Namespace.GetDefaultFolder.Items(x).Delete but this needs an index to find the email in the folder which I don't have from the inspector
Likewise I'm having trouble trying to achieve the same thing from explorer view with multiple selections - I can loop through the selection saving each but when I try to delete the emails I get the same message
I would be eternally grateful if someone could point me to the link between these two methods of accessing emails (inspector vs MAPI) so that having saved it from my form I can delete the email.
I hope that makes sense!
Thanks
This is an example of the code from one of the forms. Its cobbled together from snippets of examples I've found on the web etc. so apologies/thanks if you recognise anything
Private Sub CommandButton3_Click()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rcArray As Variant
Dim sSQL, drive As String
Dim drive_index, job, fsplit, title As Variant
Dim strPrompt, strname As String
Set myNameSpace = Application.GetNamespace("MAPI")
Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
drive_index = cbDepartment.Value
job = cbJobNum.Value
fsplit = cbFileSplit.Value
title = tbTitle.Value
'Open connection to the database
cnt.Open "XXXXXXXX", "", "XXXXX"
sSQL = "SELECT tblTeams.Drive FROM tblTeams WHERE tblTeams.LevelNo = " & CStr(drive_index) & ";"
rst.Open sSQL, cnt
rcArray = rst.GetRows
drive = rcArray(0, 0)
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
'MsgBox "save path = " & drive & "Projects\" & job & "\" & fsplit & title
Set objItem = myItem.CurrentItem
strname = objItem.Subject
'Prompt the user for confirmation
strPrompt = "Are you sure you want to save the item? " & _
"If a file with the same name already exists, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
'objItem.SaveAs Environ("HOMEPATH") & "\My Documents\" & strname & ".TXT", olTXT
objItem.SaveAs drive & "Projects\" & job & "\" & fsplit & title & ".msg", olMSG
End If
If (SaveForm.CheckBox2.Value) Then
' **** this fails ******
'objItem.Delete
' **** so does this *****
'myMessage = myItem.CurrentItem
'myMessage.Delete
End If
Else
MsgBox "There is no open email."
End If
Unload SaveForm
End Sub
I'm trying to write some vba so that from an email inspector, I open a form to query a data base to decide where the email will be saved, then from the form save and delete the email.
I've cracked the db stuff and I can save the email succesfully with 'Application.ActiveInspector.CurrentItem.SaveAs' but the .Delete method always gives
Run-time error '-2147352567 (80020009)': Outlook cannot delete this item.
(as far as I can tell that error number is just 'undefined')
Now I can succesfully delete emails via the MAPI route with Namespace.GetDefaultFolder.Items(x).Delete but this needs an index to find the email in the folder which I don't have from the inspector
Likewise I'm having trouble trying to achieve the same thing from explorer view with multiple selections - I can loop through the selection saving each but when I try to delete the emails I get the same message
I would be eternally grateful if someone could point me to the link between these two methods of accessing emails (inspector vs MAPI) so that having saved it from my form I can delete the email.
I hope that makes sense!
Thanks
This is an example of the code from one of the forms. Its cobbled together from snippets of examples I've found on the web etc. so apologies/thanks if you recognise anything
Private Sub CommandButton3_Click()
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rcArray As Variant
Dim sSQL, drive As String
Dim drive_index, job, fsplit, title As Variant
Dim strPrompt, strname As String
Set myNameSpace = Application.GetNamespace("MAPI")
Set myItem = Application.ActiveInspector
If Not TypeName(myItem) = "Nothing" Then
drive_index = cbDepartment.Value
job = cbJobNum.Value
fsplit = cbFileSplit.Value
title = tbTitle.Value
'Open connection to the database
cnt.Open "XXXXXXXX", "", "XXXXX"
sSQL = "SELECT tblTeams.Drive FROM tblTeams WHERE tblTeams.LevelNo = " & CStr(drive_index) & ";"
rst.Open sSQL, cnt
rcArray = rst.GetRows
drive = rcArray(0, 0)
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
'MsgBox "save path = " & drive & "Projects\" & job & "\" & fsplit & title
Set objItem = myItem.CurrentItem
strname = objItem.Subject
'Prompt the user for confirmation
strPrompt = "Are you sure you want to save the item? " & _
"If a file with the same name already exists, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
'objItem.SaveAs Environ("HOMEPATH") & "\My Documents\" & strname & ".TXT", olTXT
objItem.SaveAs drive & "Projects\" & job & "\" & fsplit & title & ".msg", olMSG
End If
If (SaveForm.CheckBox2.Value) Then
' **** this fails ******
'objItem.Delete
' **** so does this *****
'myMessage = myItem.CurrentItem
'myMessage.Delete
End If
Else
MsgBox "There is no open email."
End If
Unload SaveForm
End Sub