Hi
I have a macro which works fine for me and everyone in my office building. However when people outside of the office building attempts it, it returns a Runtime Error 1004.
What I am trying to do is when a user clicks on a 'Send' button, the code behind it will open up outlook and start a new email page. It will also save and attach the workbook.
As mentioned, works perfectly for me but not anyone else outside of my office building. Any thoughts on why this may be?
The code I have used below (I have a feeling it has something to do with it saving to the desktop first. If anyone can provide a code where it just simply attaches the document to the email, would be much appreciated and hopefully will solve my issue):
Sub Send_By_Email()
Dim OlApp As New Outlook.Application
Dim myNameSp As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myExplorer As Outlook.Explorer
Dim NewMail As Outlook.MailItem
Dim OutOpen As Boolean
Dim WB As Workbook
Dim Desktop, book As String
Desktop = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
book = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
book = book & " - Conflict Form.xls"
ThisWorkbook.SaveCopyAs Desktop & book
' Code to check if Outlook is already Open. If it's not, to open a new one
OutOpen = True
Set myExplorer = OlApp.ActiveExplorer
If TypeName(myExplorer) = "Nothing" Then
OutOpen = False
Set myNameSp = OlApp.GetNamespace("MAPI")
Set myInbox = myNameSp.GetDefaultFolder(olFolderInbox)
Set myExplorer = myInbox.GetExplorer
End If
'Create a new mail message item.
Set NewMail = OlApp.CreateItem(olMailItem)
With NewMail
.Display
.Subject = "Conflict of Interest Form - Completed - " & Range("E26") & ": " & Range("E23")
.To = abc@def.com.au
.Attachments.Add Desktop & book
End With
If Not OutOpen Then OlApp.Quit
Set OlApp = Nothing
Set myNameSp = Nothing
Set myInbox = Nothing
Set myExplorer = Nothing
Set NewMail = Nothing
Kill Desktop & book
End Sub
I have a macro which works fine for me and everyone in my office building. However when people outside of the office building attempts it, it returns a Runtime Error 1004.
What I am trying to do is when a user clicks on a 'Send' button, the code behind it will open up outlook and start a new email page. It will also save and attach the workbook.
As mentioned, works perfectly for me but not anyone else outside of my office building. Any thoughts on why this may be?
The code I have used below (I have a feeling it has something to do with it saving to the desktop first. If anyone can provide a code where it just simply attaches the document to the email, would be much appreciated and hopefully will solve my issue):
Sub Send_By_Email()
Dim OlApp As New Outlook.Application
Dim myNameSp As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myExplorer As Outlook.Explorer
Dim NewMail As Outlook.MailItem
Dim OutOpen As Boolean
Dim WB As Workbook
Dim Desktop, book As String
Desktop = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
book = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
book = book & " - Conflict Form.xls"
ThisWorkbook.SaveCopyAs Desktop & book
' Code to check if Outlook is already Open. If it's not, to open a new one
OutOpen = True
Set myExplorer = OlApp.ActiveExplorer
If TypeName(myExplorer) = "Nothing" Then
OutOpen = False
Set myNameSp = OlApp.GetNamespace("MAPI")
Set myInbox = myNameSp.GetDefaultFolder(olFolderInbox)
Set myExplorer = myInbox.GetExplorer
End If
'Create a new mail message item.
Set NewMail = OlApp.CreateItem(olMailItem)
With NewMail
.Display
.Subject = "Conflict of Interest Form - Completed - " & Range("E26") & ": " & Range("E23")
.To = abc@def.com.au
.Attachments.Add Desktop & book
End With
If Not OutOpen Then OlApp.Quit
Set OlApp = Nothing
Set myNameSp = Nothing
Set myInbox = Nothing
Set myExplorer = Nothing
Set NewMail = Nothing
Kill Desktop & book
End Sub