An easier way to sort the contents for the combo box would be to return the Items collection from the folder and then call its Sort method *before* you iterate the Items collection. Maybe just simplifying the way you load the rows would solve the problem.
Ok you did some nice reverse engineering ;-) If I run the program like this
it works, when press "save" an item it's created and it appears between the
others in de structure folder. But I have a problem with the combobox. The
value of the new created item occurs in the combobox, but the previous item
is also replaced. With previous I mean: the value from the last open item.
Also If i select an item from the combobox it opens the right corresponding
item but if a create a new one, again the new item is added to the combobox
but the previous item in the combobox is again replaced... If I do it again,
the item in the combobox that was wrong is corrected but again the last one
is added and de previous one is wrong...and so on.
The arrays in the program is to order the contents in the combobox.
:
With that code, you're creating an item in the Structure folder using the IPM.Contact.structure form and then setting some values for the new item that you got from the old item. (You got them the hard way -- reading control values when you should have been reading property values.)
This section has the statements out of order:
Set myInspector = application.ActiveInspector
myInspector.SetCurrentFormPage ("General")
itm.display
Since you closed the original item, there may be no ActiveInspector at all, much less an Inspector for the newly created item. Instead, display the item first, then set its current form page. However, unless you've done a heavy redesign and renamed pages, there is no need to set the "General" page as current. The first page is always the one displayed by default.
The code then fills a combo box on the item with the names of companies in the Structure folder, but I'm not sure I understand the use of two arrays.
What specific problem are you encountering?
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Here's some code I use
sub save_click()
Set myItem = Application.ActiveInspector.CurrentItem
Set nms = Application.GetNameSpace("MAPI")
Set FormPage = Item.GetInspector.ModifiedFormPages("General")
t_companyname=FormPage.Controls("companyname").value
t_BusinessAddressStreet= FormPage.Controls("BusinessAddressStreet").value
t_BusinessAddressCity= FormPage.Controls("BusinessAddressCity").value
t_BusinessAddressState= FormPage.Controls("BusinessAddressState").value
t_BusinessAddressPostalCode=
FormPage.Controls("BusinessAddressPostalCode").value
t_BusinessAddressCountry= FormPage.Controls("BusinessAddressCountry").value
t_BusinessTelephoneNumber=
FormPage.Controls("BusinessTelephoneNumber").value
t_BusinessFaxNumber= FormPage.Controls("BusinessFaxNumber").value
myitem.close 1
strFolder = "Structure"
fFound = FindFolder(nms.Folders("Mailbox - PZ").folders("PZ").Folders, 0,
strFolder)
If fFound = True Then
Set fld = nms.Folders("Mailbox - PZ").folders("PZ").Folders(strFolder)
ElseIf fFound = False Then
Set fld = nms.Folders("Mailbox - PZ").folders("PZ").Folders.Add(strFolder,
olFolderContacts)
End If
Set itms = fld.Items
set itm = itms.find("[companyname] = '" & t_companyname & "'")
if itm is nothing then
Set itm = itms.Add("IPM.Contact.structure")
with itm
.companyname = t_companyname
.BusinessAddressStreet= t_BusinessAddressStreet
.BusinessAddressCity= t_BusinessAddressCity ...BusinessAddressState=
t_BusinessAddressState .BusinessAddressPostalCode=
t_BusinessAddressPostalCode .BusinessAddressCountry=
t_BusinessAddressCountry .BusinessTelephoneNumber=
t_BusinessTelephoneNumber .BusinessFaxNumber= t_BusinessFaxNumber
end with
itm.save
Set myInspector = application.ActiveInspector
myInspector.SetCurrentFormPage ("General")
itm.display
else
end if
call fill_combobox
end sub
function fill_combobox()
Set nms1 = Application.GetNameSpace("MAPI")
Set FormPage1 = Item.GetInspector.ModifiedFormPages("General")
Set Control1 = FormPage1.Controls("scompany")
FormPage1.controls("scompany").clear
strFolder = "Structure"
fFound = FindFolder(nms1.Folders("Mailbox - PZ").folders("PZ").Folders, 0,
strFolder)
If fFound = True Then
Set fld1 = nms1.Folders("Mailbox - PZ").folders("PZ").Folders(strFolder)
ElseIf fFound = False Then
Set fld1 = nms1.Folders("Mailbox -
PZ").folders("PZ").Folders.Add(strFolder, olFolderContacts)
End If
tel=0
Set itms1 = fld1.Items
numitems = itms1.count
numcontacts=0
ReDim FullArray(NumItems-1)
For I = 1 to numitems
set itm1 = itms1(i)
NumContacts = NumContacts + 1
FullArray(NumContacts-1) = itm1.companyname
Next
for i = UBound(FullArray) - 1 To 0 Step -1
for j= 0 to i
if FullArray(j)>FullArray(j+1) then
temp=FullArray(j+1)
FullArray(j+1)=FullArray(j)
FullArray(j)=temp
end if
next
next
control1.ColumnCount = 1
If NumItems = NumContacts Then
control1.List() = FullArray
Else
Dim ConArray()
ReDim ConArray(NumContacts-1,2)
For I = 0 to NumContacts - 1
ConArray(I,1) = FullArray(I)
Next
control1.List() = ConArray
End If
set nms1=nothing
set fld1=nothing
set formpage1=nothing
set control1=nothing
set itm1=nothing
set itms1=nothing
end function
:
Did you try the code I suggested for creating a new item using a custom form?
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Hi Sue,
Form and item is exactly like you discribed. The combobox in the form
should be filled dynamicaly when the form is started the form should read a
field of all the other forms and add this field to the combobox. When the
loop is done the combobox is filled and you should be able to select and item
from it and this wil result in displaying the corresponding form.
:
I'm confused over whether you mean "form" and "item" in your description of the contents of the combo box. A form is a code/UI template. It contains no data, although it may have default values for certain fields. An item is the unit of data storage. It is linked to a particular form through the value of its MessageClass property.
So are you talking about published custom forms? Or are you talking about creating new items from existing items (which may or may not be using a custom form)? Or something else completely?
To create a new item that uses a custom form for its UI and code, use the Add method on the target folder's Items collection:
Set newItem = targetFolder.Items.Add("IPM.Post.YourFormName")
If it's a message form, use the Drafts folder as the target. If the target is a default folder, you can use the Namespace.GetDefaultFolder method to return it as a MAPIFolder object. To create an item in another person's mailbox, use Namespace.GetSharedDefaultFolder to get the MAPIFolder Otherwise, you can use the code at
http://www.outlookcode.com/d/code/getfolder.htm to walk the folder hierarchy and return the MAPIFolder corresponding to a given path string.
To create a new item that is a copy of an existing item, call the Copy method on the item you want to copy:
Set newItem = oldItem.Copy
OK then what happens if you're running a form from a folder and you want to
add a new one from within this form. If I do this the same data is in the
new form and the first form. What am I doing wrong.
This is what I want.
A folder with a collection of custom forms (ie modified contact-items).
Each form contains vbs and has a combobox and contact fields. This combobox
contains all the forms in the folder. If I select an item from this combobox
the corresponding form should be openend showing corresponding data in the
fields. If I chage a value in one of the fields and press a save button the
corresponding form shown in the combobox should be updated. Sounds easy,
but is not ;-)
Regards
Boein
:
You can't. Outlook provides no method for selecting a particular item in a folder view.
Hi,
Using modified contact forms with vbs. After adding a contact from a
contact the focus is still set on the previous contact if you look at the
selected address cards. How can you set the focus on the added contact.
Thanks
Boein