I am working with VBA in MS Excel trying to get emails to open from a file location on one of our servers. I am successful in doing so, but I am experiencing an odd error with outlook. After the script has located the correct file I was looking for, it opens it in an instance of Outlook. If I do not have an active instance of Outlook open everything works fine and no noticeable errors occur, though if Outlook is open and I open the .msg file and say I accidentally open it again or a part of the program proceeds into more code, Outlook will forever think the file is open even if it is closed, Outlook will keep this file open in it's memory until the application is killed.
To replicate this issue
- made a userform with a ListBox and Button called "guiBody" and "MoBtn"
- populated guiBody with the file names from a location on my computer which was under "E:"
- once someone selects an item from guiBody and presses MoBtn it activates a module which then opens that corresponding file in outlook for editing.
- If the user then retries to open that file the system will throw an error for the file already being opened.
this is an example snip for someone to try and replicate it quick
Private Sub CommandButton1_Click()
Dim Msg As Object
Dim update As Boolean: update = False
For r = 0 To Me.ListBox1.ListCount - 1
If (Me.ListBox1.Selected(r) = True) Then
folderPath = "E:\" 'Your file path
thisFile = Dir(folderPath & "\" & Me.ListBox1.List(r) & ".msg")
On Error Resume Next
Set Msg = GetObject("", "Outlook.Application").Session.OpenSharedItem(folderPath & "\" & thisFile)
Msg.Display
MsgBox("Wait")
Msg.Close olSave
Set Msg = Nothing
If (Err <> 0) Then
MsgBox("Throwing a fit")
End If
On Error GoTo 0
End If
Next
End Sub
Private Sub UserForm_Activate()
folderPath = "E:\" 'Your file path
thisFile = Dir(folderPath & "\*.msg")
On Error Resume Next
Do While thisFile <> ""
Me.ListBox1.AddItem (Replace(thisFile, ".msg", ""))
thisFile = Dir
Loop
End Sub
Private Sub MD(guiList As MSForms.ListBox)
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("RecordedInfo")
Dim Msg As Object
Dim update As Boolean: update = False
sheetLength = CalcSheetLength.CalculateSheetLength("RecordedInfo")
For r = 0 To guiList.ListCount - 1
If (guiList.Selected(r) = True) Then
update = False
folderPath = "E:\ReportingTeam\ReportProcedures\Draft Templates"
thisFile = Dir(folderPath & "\" & guiList.List(r) & ".msg")
On Error Resume Next
Set Msg = GetObject("", "Outlook.Application").Session.OpenSharedItem(folderPath & "\" & thisFile)
Msg.Display
If (Err = 0) Then
update = True
End If
On Error GoTo 0
For InfoLen = 2 To sheetLength
If (guiList.List(r) = ws.Range("A" & InfoLen) And update = True) Then
If (ws.Range("C" & InfoLen) = "") Then
ws.Range("C" & InfoLen) = Date
response = InputBox("Why did you modify the " & guiList.List(r) & " template?", "Comment")
If (ws.Range("D" & InfoLen) = "") Then
If (Trim(response) = "") Then
ws.Range("D" & InfoLen) = "No Response Given"
Else
ws.Range("D" & InfoLen) = Trim(response)
End If
Else
If (Trim(response) = "") Then
ws.Range("D" & InfoLen) = "No Response Given"
Else
ws.Range("G" & InfoLen) = ws.Range("D" & InfoLen) & ";" & ws.Range("G" & InfoLen)
ws.Range("D" & InfoLen) = Trim(response)
End If
End If
Else
ws.Range("F" & InfoLen) = ws.Range("C" & InfoLen) & ";" & ws.Range("F" & InfoLen)
ws.Range("C" & InfoLen) = Date
response = InputBox("Why did you modify the " & guiList.List(r) & " template?", "Comment")
If (ws.Range("D" & InfoLen) = "") Then
If (Trim(response) = "") Then
ws.Range("D" & InfoLen) = "No Response Given"
Else
ws.Range("D" & InfoLen) = Trim(response)
End If
Else
If (Trim(response) = "") Then
ws.Range("G" & InfoLen) = ws.Range("D" & InfoLen) & ";" & ws.Range("G" & InfoLen)
ws.Range("D" & InfoLen) = "No Response Given"
Else
ws.Range("G" & InfoLen) = ws.Range("D" & InfoLen) & ";" & ws.Range("G" & InfoLen)
ws.Range("D" & InfoLen) = Trim(response)
End If
End If
End If
End If
Next
If update = False Then
MsgBox ("The program encountered an error with modifying this draft." & vbCrLf & vbCrLf & "This issue is due to the file still being open on your system, the server not registering the file has closed or another user has the file open." & vbCrLf & vbCrLf & "If the error persists contact Kyle Willman.")
End If
On Error Resume Next
Msg.Close olSave
Set Msg = Nothing
On Error GoTo 0
End If
Next
Call Refresher.WindowRefresh(guiList)
End Sub
I wanted Outlook to open the file but not retain it in it's memory. Or have the ability to kill off the active instances of the opened files.
I have already tried .close .quit ActiveInspector but none of these suit my needs.
The next solution I was going to try is attempting to get the program to wait until the .msg stops displaying but I am unsure if I have the knowledge to do so.
Additionally, the specific reason I was doing this in excel is for the background capability to store information that we might need like dates, and comments based on when someone accesses these .msg files or alters them.