Command Button Controls

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
 
H

Hollis Paul

Dim item As MailItem

It looks to me like you are trying to run VBA code in the form, when
the form will only run VBScript. In the versions I used, "Dim AS" did
not work.
 
S

Sue Mosher [MVP]

This looks like VBA code, not VBScript behind a custom Outlook form. Please
clarify.

Note that code runs only on published Outlook custom forms.
 
A

Ashley

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
 
A

Ashley

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
 
H

Hollis Paul

Ok, so here is my code now, Doesn't give me an error but it doesn't run.
The best source for Outlook coding is http://www.outlookcode.com . You
will find example code for things you may want to do, but certainly
examples of how to style the code.

For instance, Item is an intrinsic variable, and always means the current
Item. I seem to recall that Application is also an intrinsic item, and
you use it as the basis for building objects. But, it has been a while
since I did any coding, so I am not going to provide specific coding
suggestions. Go look at the examples.
 
S

Sue Mosher [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top