Command Button on Userform not working

mrnoobexcel

New Member
Joined
Nov 20, 2017
Messages
3
Hello All,

I found a macro that creates an outlook appointment, this code works perfectly inside a module. I then copied this code and pasted it inside a command button in a userform, but when I run the form and click on the button, nothing happens as if there's no code inside the button. I've searched the web for an answer to no avail. Can someone help? Also, the working module is a public sub just in case this info is necessary. Here's the code inside the command button:

Code:
Public Sub CreateAppointment()
  
  Dim oApp As Outlook.Application
  Dim oNameSpace As NameSpace
  Dim oItem As AppointmentItem
      
  On Error Resume Next
  ' check if Outlook is running
  Set oApp = GetObject("Outlook.Application")
  If Err <> 0 Then
    'if not running, start it
    Set oApp = CreateObject("Outlook.Application")
  End If
  
  Set oNameSpace = oApp.GetNamespace("MAPI")
  
  Set oItem = oApp.CreateItem(olAppointmentItem)
  
  
  
  With oItem
  
    .subject = "This is the subject"
    .Start = "31/05/2011 11:45"
    
    .AllDayEvent = True
    .Importance = olImportanceNormal
    .Categories = "Green Category"
    
    .ReminderSet = True
    .ReminderMinutesBeforeStart = "15"
    .ReminderPlaySound = True
    .ReminderSoundFile = "C:\Windows\Media\Ding.wav"
    
    Select Case 1 ' do you want to display the entry first or save it immediately?
      Case 1
        .Display
      Case 2
        .Save
    End Select
  
  End With
    
  Set oApp = Nothing
  Set oNameSpace = Nothing
  Set oItem = Nothing
     
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What do you see if you double click the button on the userform when you are in design mode?
 
Upvote 0
You don't see anything like this?
Code:
Private Sub CommandButton1_Click()

End Sub
 
Upvote 0
Yes, I see this:

Code:
[Private Sub CommandButton1_Click()
  
  Dim oApp As Outlook.Application
  Dim oNameSpace As NameSpace
  Dim oItem As AppointmentItem
      
  On Error Resume Next
  ' check if Outlook is running
  Set oApp = GetObject("Outlook.Application")
  If Err <> 0 Then
    'if not running, start it
    Set oApp = CreateObject("Outlook.Application")
  End If
  
  Set oNameSpace = oApp.GetNamespace("MAPI")
  
  Set oItem = oApp.CreateItem(olAppointmentItem)
  
  
  
  With oItem
  
    .subject = "This is the subject"
    .Start = "31/05/2011 11:45"
    
    .AllDayEvent = True
    .Importance = olImportanceNormal
    .Categories = "Green Category"
    
    .ReminderSet = True
    .ReminderMinutesBeforeStart = "15"
    .ReminderPlaySound = True
    .ReminderSoundFile = "C:\Windows\Media\Ding.wav"
    
    Select Case 1 ' do you want to display the entry first or save it immediately?
      Case 1
        .Display
      Case 2
        .Save
    End Select
  
  End With
    
  Set oApp = Nothing
  Set oNameSpace = Nothing
  Set oItem = Nothing

End Sub
/CODE]
 
Upvote 0
Does the code run when you click the command button?

You can find that out by putting a breakpoint on the the first line of code using F9, running the userform and clicking the button.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top