Solved

VBA for Outlook - Custom form, object values not transfering to read page

Posted on 2006-11-20
16
479 Views
Last Modified: 2013-12-26
Hi there,

I have a custom form (based on message) that im using.

The form itself is working quite well except for that the values (hostname, username, fullname, ip address, etc..)
are not getting transfered to the "Read Page".

Could someone have a look at my code and tell me what i need to do to fix this? I've already copied all my objects and code into a new custom form and published it to make sure it's not a one off.

Any other suggestions?




Public ipaddarr()
Public nCounter
Public usrarray()
Public pbFullName
Public pbUsername
Public pbTelNumber
Public pbLocation
Public pbHostname
Public pbIPAddress1
Public pbIPAddress2

Function Item_Open()
      Dim      HDUsername      'Declare everything
      Dim      HDHostname
      Dim      WshNetwork
      Dim      Testvalue
      Dim      txhostname
      Dim      txusername
      Dim      txipaddress1
      Dim      transipaddress
      Dim      CNUsers
      Dim      pthUsername
      Dim      chkLoginname
      Dim      xfertest
      Dim      rsDetails

      Set WshNetwork = CreateObject("WScript.Network")      'Object decs.
      Set oPage = Item.GetInspector.ModifiedFormPages
      Set txhostname = oPage("Message").Controls("txhostname")
      Set txusername = oPage("Message").Controls("txusername")
      Set txipaddress1 = oPage("Message").Controls("txipaddress1")
      Set txipaddress2 = oPage("Message").Controls("txipaddress2")
      Set txfullname = oPage("Message").Controls("txfullname")
      Set txtelephone = oPage("Message").Controls("txtelephone")
      Set txlocation = oPage("Message").Controls("txlocation")
      Set IPConfigSet = GetObject("winmgmts:").ExecQuery("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

      HDHostname = WshNetwork.ComputerName      'Associations
      HDUsername = WshNetwork.UserName
      Set WshNetwork = Nothing

      nCounter = 0
      For Each IPConfig In IPConfigSet
                If Not IsNull(IPConfig.IPAddress) Then
                    For I = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
                        nCounter = nCounter+1
                        Redim Preserve ipaddarr(nCounter)
                        ipaddarr(nCounter) = IPConfig.IPAddress(I)
                    Next
                End If
      Next

      txusername.text = HDUsername      'Write to fields
      txhostname.text = HDHostname
      txipaddress1.text = ipaddarr(1)
      'xfertest = ipaddarr(2)
      If nCounter = 2 Then
            txipaddress2.text = ipaddarr(2)
      End If

      Set rsDetails = CreateObject("ADODB.Recordset")
      rsDetails.ActiveConnection = "Provider=ADSDSOObject"
      rsDetails.Source = "SELECT ADsPath, displayName, l, givenName, sn, telephoneNumber FROM 'LDAP://server' WHERE objectClass='user' AND objectCategory='Person' AND samAccountName='" & HDUsername & "'"
      rsDetails.CursorType = 0
      rsDetails.CursorLocation = 2
      rsDetails.LockType = 1
      rsDetails.Open()
      If Not rsDetails.EOF Then
                With rsDetails
                  txfullname.text = .Fields("givenName") & " " & .Fields("sn")
                  If Not IsNull(.Fields("telephoneNumber")) Then
                        txtelephone.text = .Fields("telephoneNumber")
                  End If
                  If Not IsNull(.Fields("l")) Then
                        txlocation.text = .Fields("l")
                  End If
                End With
      end if
      rsDetails.Close
      Set rsDetails = Nothing

      'Share the settings to read page.
      pbFullName = txfullname.text
      pbUsername = txusername.text
      pbTelNumber = txtelephone.text
      pbLocation = txlocation.text
      pbHostname = txhostname.text
      pbIPAddress1 = txipaddress1.text
      pbIPAddress2 = txipaddress2.text

End Function



Function Item_Read()
      msgbox "Item_Read: " & pbHostname  'This doesn't come up, for some reason this function not working.
      Dim      txhostname
      Dim      txusername
      Dim      txipaddress1
      Dim      txipaddress2
      Dim      txfullname
      Dim      txtelephone
      Dim      txlocation
      '

      Set txhostname = oPage("Message").Controls("txhostname")
      Set txusername = oPage("Message").Controls("txusername")
      Set txipaddress1 = oPage("Message").Controls("txipaddress1")
      Set txipaddress2 = oPage("Message").Controls("txipaddress2")
      Set txfullname = oPage("Message").Controls("txfullname")
      Set txtelephone = oPage("Message").Controls("txtelephone")
      Set txlocation = oPage("Message").Controls("txlocation")
      '
      txhostname.text = pbHostname
      txusername.text =      pbUsername
      txipaddress1.text = pbIPAddress1
      txipaddress2.text = pbIPAddress2
      txfullname.text = pbFullName
      txtelephone.text = pbTelNumber
      txlocation.text = pbLocation
      msgbox pbHostname
End Function
   

   
0
Comment
Question by:jcourtes
  • 7
  • 6
16 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 18009650
Hello again, Jamie.

For data to be saved and therefore available on the Read page it has to be stored in User Properties, not in the controls.  The controls are purely visual constructs.  They do not retain anything between form usages.
0
 
LVL 1

Author Comment

by:jcourtes
ID: 18010188
Hey BlueDevil :)

Ok... So I need to use "User properties"

Do you know of any tutorials that would be applicable to the learning path i need to follow?

It's obvious I went in a wrong direction
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 18010240
This is a good starting point.
http://www.outlookcode.com/d/propsyntax.htm
0
 
LVL 1

Author Comment

by:jcourtes
ID: 18020329
How can I programmatically determine if a form is being read instead of composed?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18024401
0
 
LVL 1

Author Comment

by:jcourtes
ID: 18027883
Well, I figured out how to determine which page is being show, and I have even managed to figure out "ShowComposeLayout" and "ShowReadLayout"

I'm having problems getting any code to fire when the receiver opens the message.

Trying to set the values for some text boxes on the Read page while in the Compose page.

I have already switch my code over to set my variables to user properties, but I can't seem to write to the text boxes on the read page.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 76

Expert Comment

by:David Lee
ID: 18034085
> I'm having problems getting any code to fire when the receiver opens the message.
Can you share the code?

0
 
LVL 1

Author Comment

by:jcourtes
ID: 18038007
Here is where I am at now, the compose page is working fine, but my values that i am storing in the user properties are not getting written to the read page controls (ex: Rtxfullname as opposed to txfullname which is on compose page). It's as if the user properties are forgotten or something.

Here's my code, i've flagged the problematic section with a comment:


Public ipaddarr()
Public nCounter
Public usrarray()
Public pbFullName
Public pbUsername
Public pbTelNumber
Public pbLocation
Public pbHostname
Public pbIPAddress1
Public pbIPAddress2
Public itemreadq

Function ShowsComposeLayout()
    Const olMail = 43
    If Item.Class = olMail Then
        If Not Item.Sent Then
            ShowComposeLayout = True
            'msgbox "not sent"
        End If
    ElseIf Not Item.Saved Then
        ShowComposeLayout = True
      'msgbox "not item.saved"
    Else
        ShowComposeLayout = False
        ShowReadLayout = True
      'msgbox "scl = f srl = t"
    End If
End Function

Function Item_Open()
      ShowsComposeLayout
      
      Dim       objFullname
      Dim       objUsername
      Dim       objTelephone
      Dim       objLocation
      Dim       objHostname
      Dim       objIpaddress1
      Dim       objIpaddress2
      Dim       transfullname
      Dim       transtelephone
      Dim       translocation

      set       objFullname = Item.UserProperties("fullname")
      set       objUsername = Item.UserProperties("jcusername")
      set       objTelephone = Item.UserProperties("telephone")
      set       objLocation = Item.UserProperties("location")
      set       objHostname = Item.UserProperties("hostname")
      set       objIpaddress1 = Item.UserProperties("ipaddress1")
      set       objIpaddress2 = Item.UserProperties("ipaddress2")

      Dim      Rtxhostname
      Dim      Rtxusername
      Dim      Rtxipaddress1
      Dim      Rtxipaddress2
      Dim      Rtxfullname
      Dim      Rtxtelephone
      Dim      Rtxlocation

      Dim      HDUsername      
      Dim      HDHostname
      Dim      WshNetwork
      Dim      Testvalue
      Dim      txhostname
      Dim      txusername
      Dim      txipaddress1
      Dim      transipaddress
      Dim      CNUsers
      Dim      pthUsername
      Dim      chkLoginname
      Dim      xfertest
      Dim      rsDetails

      Set WshNetwork = CreateObject("WScript.Network")      'Object decs.
      Set oPage = Item.GetInspector.ModifiedFormPages
      Set IPConfigSet = GetObject("winmgmts:").ExecQuery("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

      If Item.Sent Then
            'MsgBox "Sent, reading READ PAGE controls"
            Set Rtxhostname = oPage("Message").Controls("Rtxhostname")
            Set Rtxusername = oPage("Message").Controls("Rtxusername")
            Set Rtxipaddress1 = oPage("Message").Controls("Rtxipaddress1")
            Set Rtxipaddress2 = oPage("Message").Controls("Rtxipaddress2")
            Set Rtxfullname = oPage("Message").Controls("Rtxfullname")
            Set Rtxtelephone = oPage("Message").Controls("Rtxtelephone")
            Set Rtxlocation = oPage("Message").Controls("Rtxlocation")
      Else
            'MsgBox "Not Sent, reading COMPOSE PAGE controls"
            Set txhostname = oPage("Message").Controls("txhostname")
            Set txusername = oPage("Message").Controls("txusername")
            Set txipaddress1 = oPage("Message").Controls("txipaddress1")
            Set txipaddress2 = oPage("Message").Controls("txipaddress2")
            Set txfullname = oPage("Message").Controls("txfullname")
            Set txtelephone = oPage("Message").Controls("txtelephone")
            Set txlocation = oPage("Message").Controls("txlocation")
            HDHostname = WshNetwork.ComputerName      'Associations
            HDUsername = WshNetwork.UserName
            Set WshNetwork = Nothing
      End If

      If Not Item.Sent Then
            nCounter = 0
            For Each IPConfig In IPConfigSet
                      If Not IsNull(IPConfig.IPAddress) Then
                                For I = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
                              nCounter = nCounter+1
                              Redim Preserve ipaddarr(nCounter)
                              ipaddarr(nCounter) = IPConfig.IPAddress(I)
                                Next
                      End If
            Next

            Set rsDetails = CreateObject("ADODB.Recordset")
            rsDetails.ActiveConnection = "Provider=ADSDSOObject"
            rsDetails.Source = "SELECT ADsPath, displayName, l, givenName, sn, telephoneNumber FROM 'LDAP://server' WHERE objectClass='user' AND objectCategory='Person' AND samAccountName='" & HDUsername & "'"
            rsDetails.CursorType = 0
            rsDetails.CursorLocation = 2
            rsDetails.LockType = 1
            rsDetails.Open()
            If Not rsDetails.EOF Then
                      With rsDetails
                        transfullname = .Fields("givenName") & " " & .Fields("sn")
                        If Not IsNull(.Fields("telephoneNumber")) Then
                              transtelephone = .Fields("telephoneNumber")
                        End If
                        If Not IsNull(.Fields("l")) Then
                              translocation = .Fields("l")
                        End If
                      End With
            end if
            rsDetails.Close
            Set rsDetails = Nothing
      End If

      'TODO: If in read page, pull the values instead of set them
      If Not Item.Sent Then
            objFullname = transfullname
            objUsername = HDUsername
            objHostname      = HDHostname
            objTelephone = transtelephone
            objLocation = translocation
            objIpaddress1 = ipaddarr(1)
            If nCounter = 2 Then
                  objIpaddress2 = ipaddarr(2)
            End If
      Else
            ''''''Error at the next line: "Object variable not set"                <----------
            transfullname = objFullname
            HDUsername = objUsername
            HDHostname = objHostname
            transtelephone = objTelephone
            translocation = objLocation
            ipaddarr(1) = objIpaddress1
            If nCounter = 2 Then
                  ipaddarr(2) = objIpaddress2
            End If
      End If

      msgbox " " & transfullname & " " & HDUsername & " " & HDHostname & " " & transtelephone & " " & translocation
      'This works when composing'

      If Not Item.Sent Then
            txfullname.text = transfullname
            txusername.text = HDUsername
            txtelephone.text = transtelephone
            txlocation.text = translocation
            txhostname.text = HDHostname
            txipaddress1.text = ipaddarr(1)
            txipaddress2.text = ipaddarr(2)
            Item.Save
      Else
            Rtxfullname.text = transfullname
            Rtxusername.text = HDUsername
            Rtxtelephone.text = transtelephone
            Rtxlocation.text = translocation
            Rtxhostname.text = HDHostname
            Rtxipaddress1.text = ipaddarr(1)
            Rtxipaddress2.text = ipaddarr(2)
      End If

End Function
   
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18044241
Why go through the process of reading the user properties and setting them into the controls instead of binding the control directly to the user property?  
0
 
LVL 1

Author Comment

by:jcourtes
ID: 18044460
what would the syntax for something like that be?

Can i bind more than one control to a user property?

How would that fix anything if I'm getting an object error near the end?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18044482
> what would the syntax for something like that be?
There is no syntax.  Open the form in the forms editor.  Go to the page containing your control.  Right-click on the control and select Properties.  Click the Vlaue tab.  Click the Choose Field pulldown and select your field.  It's now bound to that control.

> Can i bind more than one control to a user property?
If you mean binding a control on the read and compose pages to the same control, then yes.

> How would that fix anything if I'm getting an object error near the end?
You wouldn't need any of those objects.
0
 
LVL 1

Author Comment

by:jcourtes
ID: 18045137
I don't have any of my controls in the menus.

I created my labels/text boxes from scratch, and did not make fields.

I assume this would mean I would have to scrap them and make them again but this time creating custom fields for each one?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18050846
> I don't have any of my controls in the menus.
Refer back to my initial post on this question.

"For data to be saved and therefore available on the Read page it has to be stored in User Properties, not in the controls.  The controls are purely visual constructs.  They do not retain anything between form usages."

If the form does not have a user-property to store each control's content, then the content of those controls is lost the instant the form closes.  You don't have to scrap the controls.  You just need to create a user-property for each one to store the control's value.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now