Command Button Controls

  • Thread starter Thread starter Ashley
  • Start date Start date
A

Ashley

I created a command button on my custom form and when its clicked I want the following code to run. here is my code, but nothing happens when I click on the button. I know my code works when I run it mannually.

Thanks


Sub CommandButton1_Click()
Dim item As MailItem

' Set item = Outlook.Application.ActiveExplorer.Selection.item(1)
Set item = Outlook.Application.ActiveInspector.CurrentItem



Dim olApp As Outlook.Application
Dim olTsk As TaskItem
Dim userField As Outlook.UserProperty

Set olApp = New Outlook.Application
Set olTsk = olApp.CreateItem(olTaskItem)


With olTsk
.Subject = item.Subject
.Status = olTaskNotStarted
.Body = item.Body
Set ups = item.UserProperties
Set prp = ups.Find("DateDue")
.DueDate = ups("DateDue").Value


.Save
End With

Set olTsk = Nothing
Set olApp = Nothing
End Sub.
Submitted using http://www.outlookforums.com
 
This looks like VBA code, not VBScript behind a custom Outlook form. Please
clarify.

Note that code runs only on published Outlook custom forms.
 
I guess I was using VBA, I have never programmed in Outlook always access and excel. I wrote the code and can call the macro from the Developer Tab (Marcos). I tried pasteing the code where it allows you to under the form code (View Code). I rewrote my code, it still works calling it like a marco but I can't get it to assign to my button. Help!


Public Sub CommandButton1_Click()

Set ins = Application.ActiveInspector
Set itm = ins.CurrentItem

If itm.Class <> olMail Then
MsgBox "The active Inspector is not a mail message; exiting"
' GoTo ErrorHandlerExit
'Could add more error-trapping to determine if the mail message uses a
'specific custom form, or has specific data in one or more fields

Else
Set msg = itm


' Set item = Outlook.Application.ActiveExplorer.Selection.item(1)
'Set item = Outlook.Application.ActiveInspector.CurrentItem



Set olApp = New Outlook.Application
Set olTsk = olApp.CreateItem(olTaskItem)


With olTsk
.Subject = msg.Subject
.Status = olTaskNotStarted
.Body = msg.Body
Set ups = msg.UserProperties
Set prp = ups.Find("DateDue")
If TypeName(prp) <> "Nothing" Then
If prp.Value <> #1/1/4501# Then
.DueDate = ups("DateDue").Value
End If
End If


.Save
End With

Set olTsk = Nothing
Set olApp = Nothing
End If
End Sub.
Submitted using http://www.outlookforums.com
 
Ok, so here is my code now, Doesn't give me an error but it doesn't run.


Public Sub CommandButton1_Click()


Set ins = Outlook.Inspector
Set itm = Object
Set ups = Outlook.UserProperties
Set prp = Outlook.UserProperty
Set msg = Outlook.MailItem
Set olApp = Outlook.Application
Set olTsk = TaskItem
Set userField = Outlook.UserProperty
Set ins = Application.ActiveInspector
Set itm = ins.CurrentItem

If itm.Class <> olMail Then
MsgBox "The active Inspector is not a mail message; exiting"

Else
Set msg = itm



Set olApp = New Outlook.Application
Set olTsk = olApp.CreateItem(olTaskItem)


With olTsk
.Subject = msg.Subject
.Status = olTaskNotStarted
.Body = msg.Body
Set ups = msg.UserProperties
Set prp = ups.Find("DateDue")
If TypeName(prp) <> "Nothing" Then
If prp.Value <> #1/1/4501# Then
.DueDate = ups("DateDue").Value
End If
End If


.Save
End With

Set olTsk = Nothing
Set olApp = Nothing
End If
End Sub
 
Is the button named CommandButton1?

Is the form published with the "Send form definition with item" box on the
(Properties) page unchecked?

None of these expressions will work -- Outlook.UserProperties,
Outlook.Inspector , etc. -- because there is no intrinsic object named
Outlook. As Hollis pointed out, in VBScript code behind a custom form, you
have two intrinsic objects: Application for the currently running
Outlook.Application instance (hence you never would use New
Outlook.Application) and Item to represent the item where the code is
running.

I can't offer much more than that, because I can't tell from the code just
what you're trying to accomplish.
 
Back
Top