98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Not running code for certain users
Not running code for certain users
Resolved · Low Priority · Version 2010
Matthew has attended:
Excel VBA Intro Intermediate course
Not running code for certain users
I have created a tracker sheet for other departments to use that email myself & my manager when a change is made.
This is the code below that I am using
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Dim answer As String
answer = MsgBox("Do you wish to save changes?", vbYesNo + vbQuestion, "Save")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
newmsg.Recipients.Add ("Email Address")
'add subject
newmsg.Subject = "Appraisal Tracker"
'Add body
newmsg.body = "Changes have been made to the appraisal tracker"
newmsg.Display 'display
newmsg.Send 'send message
End If
End Sub
Is there way to stop the code from running when I make a change to the document?
Thank you in advance
Matt
RE: Not running code for certain users
Hi Matthew,
Thank you for the forum question.
You can do it by using one more IF decision code. The Application.UserName function will test the user name'
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.UserName <> "Jens Bonde" And Application.UserName <> "Matthew" Then
answer = MsgBox("Do you wish to save changes?", vbYesNo + vbQuestion, "Save")
If answer = vbNo Then
Cancel = True
end if
If answer = vbYes Then
'open outlook
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
newmsg.Recipients.Add ("Email Address")
'add subject
newmsg.Subject = "Appraisal Tracker"
'Add body
newmsg.body = "Changes have been made to the appraisal tracker"
newmsg.Display 'display
newmsg.Send 'send message
End If
End If
End Sub
I hope this make sense.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Tue 14 Feb 2017: Automatically marked as resolved.
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Apply currency format quickly in ExcelTo quickly apply the currency format to cell in your spreadsheet, select (highlight) the cells you wish to apply currency format to, then use Ctrl + Shift + $ |