What are the correct Dim statements to go with this Microsoft Sample Code?

B

Bill

I am having some trouble getting the text value of the two text
controls I have added to page 2 of an email for my customer's clients
to us to place orders. The two text controls are named "ClientName"
and "ClientContact". There will be many other controls/fields later
but I am just trying to get a handle on getting the text from a
control here.

I am trying to work through the Microsoft Article:
OL2000: Working with Keywords Fields from VBScript
Article ID: 201099

Sue Mosher suggested this article in another post I read about getting
the value of a text box.

The article shows the following example code:

Sub Commandbutton1_Click()
Set MyPage = Item.GetInspector.ModifiedFormPages("Message")
Set MyControl = MyPage.Controls("TextBox1")
MyControl.Value = Now() & ", " & MyControl.Value
MsgBox MyControl.Value
End Sub

When I run the code, of course, the first error message I get is that
MyPage is undefined.

However, I am not sure what Dim statements to use for MyPage,
MyControl, and Item.

Something I read said that "Item" is a built-in object with a mail
item.

I have the following code in which I have tried to incorporate the
example code above.

Thank you in advance for any help you can give me. I am new at
programming with Outlook so please give me full details of what I need
to do. I have done a lot of reading, but just can't find it.
------------------------------------------------

Private Sub GetMail()
On Error GoTo ErrorHandler

Dim OL As Outlook.Application
Dim OLNS As Outlook.NameSpace
Dim Folder As Outlook.MAPIFolder
Dim ToFolder As Outlook.MAPIFolder
Dim MyMail As MailItem
Dim numErrors As Integer
Dim numCancelledOrders As Integer
Dim numWebOrders As Integer

Dim errPos As Integer

'Declare strings to hold text values of controls in email
Dim strClient As String
Dim strClientName String

errPos = 1

Set OL = New Outlook.Application
Set OLNS = OL.GetNamespace("MAPI")
Set Folder = OLNS.GetDefaultFolder(olFolderInbox)

Set ToFolder = OLNS.GetDefaultFolder(olFolderInbox)

errPos = 2
Set ToFolder = ToFolder.Folders("Test")
MsgBox ToFolder.Items.Count & " items in Test"

errPos = 3
For Each MyMail In Folder.Items

'Process fields
Set MyPage = MyMail.GetInspector.ModifiedFormPages("Message")

strClient = MyMail.UserProperties("ClientName")
strClientContact = MyMail.UserProperties("ClientContact")

PostData "Client: " & strClient & vbCrLf & "Client Contact: " &
strClientContact
End If

errPos = 99
Next MyMail

errPos = 4
PostData "Items processed: " & numItems & vbCrLf & _
"New orders processed: " & numNewOrders & vbCrLf & _
"Cancelled orders processed: " & numCancelledOrders

errPos = 5
Set OL = Nothing
Set OLNS = Nothing
Set Folder = Nothing
Set MyMail = Nothing

Exit Sub

ErrorHandler:

MsgBox "Error in GetMail" & Err.Number & vbCrLf & _
Err.Description & vbCrLf & "errPos: " & errPos & vbCrLf

End Sub
 
K

Ken Slovak - [MVP - Outlook]

What's the name you gave to that customized page 2? Is it "Message"?

If so, to get the controls use something like this:

Set control1 = MyPage.Controls.Item("ClientName")
strName = control1.Text
Set control2 = MyPage.Controls.Item("ClientContact")
strClientContact = control2.Text

In form code you use VBScript. That means no As declarations, all variables
are Variants. So leave the As clauses out. You don't even have to Dim a
variable in VBScript, but for readability I recommend it. Just something
like Dim MyPage.

No error handlers in VBScript, just On Error Resume Next.

Do not declare an Application or Item object. Those are intrinsic to form
code. In form code Application is always the current Outlook session and
Item is the item where the code is running.
 
B

Bill

What's the name you gave to that customized page 2? Is it "Message"?

If so, to get the controls use something like this:

Set control1 = MyPage.Controls.Item("ClientName")
strName = control1.Text
Set control2 = MyPage.Controls.Item("ClientContact")
strClientContact = control2.Text

In form code you use VBScript. That means no As declarations, all variables
are Variants. So leave the As clauses out. You don't even have to Dim a
variable in VBScript, but for readability I recommend it. Just something
like Dim MyPage.

No error handlers in VBScript, just On Error Resume Next.

Do not declare an Application or Item object. Those are intrinsic to form
code. In form code Application is always the current Outlook session and
Item is the item where the code is running.

Ken,

Thank you for your response.

Believe it or not, I woke up in the middle of the night thinking that
maybe the demo was for VBScript in Outlook.

I wrote the program in Visual Basic. I wanted to include it as part
of a larger system in VB.

Is that possible?

I already have a VB program that deals with customer emails. I was
trying to improve it by adding forms to customer emails so that I can
control the input. You would probably not be surprised to find out
that some people can't spell the name of their own company correctly.
While that is usually the result of carelessness rather than not
knowing how to spell, I prefer to not allow mistakes to get past an
initial edit in the form.

If I cannot do it in VB, I will have Outlook do the preliminary edits,
save to a file, and process it from there with VB.

Also, I didn't think about "MyMail" being the name of the page, I
thought it was some kind of object that I didn't know about.
Substituting the name of the page will make all the difference, as you
suggest.

Thanks for your help.

Bill
 
K

Ken Slovak - [MVP - Outlook]

Outlook form code can only be VBScript code, embedded in the form. It cannot
be VB or VBA code.

I try to avoid as much form code as I can but I have to use COM addins for
that usually. I can handle any item being opened from the NewInspector event
of the Inspectors collection. The I subscribe to any item or Inspector
events I want to handle in the open item and that eliminates the need for
form code. However, it does require a COM addin, which is another level of
complexity.
 
B

Bill

Outlook form code can only be VBScript code, embedded in the form. It cannot
be VB or VBA code.

I try to avoid as much form code as I can but I have to use COM addins for
that usually. I can handle any item being opened from the NewInspector event
of the Inspectors collection. The I subscribe to any item or Inspector
events I want to handle in the open item and that eliminates the need for
form code. However, it does require a COM addin, which is another level of
complexity.


Thanks, Ken.
 

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