Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / April 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill - 26 Apr 2007 22:56 GMT
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
Ken Slovak - [MVP - Outlook] - 27 Apr 2007 14:46 GMT
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.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

>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
[quoted text clipped - 101 lines]
>
> End Sub
Bill - 27 Apr 2007 17:00 GMT
>What's the name you gave to that customized page 2? Is it "Message"?
>
[quoted text clipped - 15 lines]
>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
Ken Slovak - [MVP - Outlook] - 27 Apr 2007 19:58 GMT
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.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

<snip>
> Ken,
>
[quoted text clipped - 27 lines]
>
> Bill
Bill - 27 Apr 2007 20:08 GMT
>Outlook form code can only be VBScript code, embedded in the form. It cannot
>be VB or VBA code.
[quoted text clipped - 5 lines]
>form code. However, it does require a COM addin, which is another level of
>complexity.

Thanks, Ken.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.