Auto-Publishing form with script, cmdbtn won't function

B

Bob S

Hello-

I'm working w/Outlook 2000 and Outlook 2003. OSs are
Windows 2000 & XP respectively. I've created a custom
Task form with a custom page called "Task Details." This
page has an auto-numbering text box, formatted as
follows: the 2-digit year, a dash and then then a 4 digit
number (ex. 04-0001 for the first task). To get the auto
numbering to work, I've used the following code:

olFolderRegistry = 3
olFoldertasks = 13
myFormName = "XP Tasker" 'The name of the task form
Sub Item_Open()
' Set the Outlook NameSpace.
Set olns = Application.GetNameSpace("MAPI")
' Set the folder to the Contacts folder
Set myFolder = olns.GetDefaultFolder(olFolderTasks)
' #1/1/4501# is the internal representation of an
' empty data value in Outlook.
If Item.LastModificationTime = #1/1/4501# Then
' Item is a brand new item.
dim formpage 'As page
Set FormPage = Item.GetInspector.ModifiedFormPages("Task
Details")
Set control = formpage.controls("TxtAutonmbr")
'The control for the autonumbering
If control.text= cstr(right(date(),2))& "-9999" then
'I don't want more than 4 digits following "04-" so,
'if I hit 9999 I want to set it back to 0001 again
control.text = cstr(right(date(),2))&"-"&"0001"
Else
' Increment the number in the field.
control.text=cstr(cint(mid(control.text,4,len
(control.text)-3)))
'Get the last 4 digits
Control.text=CStr(CInt(control.text) + 1)
'add 1 & then fill in the zeros as necessary to make it
'4 digits in length
If len(control.text)= 1 then
control.text= "000" & control.text
end if
If len(control.text)= 2 then
control.text= "00" & control.text
end if
If len(control.text)= 3 then
control.text= "0" & control.text
end if
control.text= cstr(right(date(),2))& "-" & control.text
'add the 2 digit year & "-" to the front of the number
End If
'Now publish the form
Set myForm = Item.FormDescription myForm.Name= _
myFormName
myForm.PublishForm olFolderRegistry, myFolder
End If
End Sub

However, I also have command buttons on this form. And
once I've run this code, the command buttons will only
work when the form is first run. If I save and close the
form, then re-open the task, the command buttons stop
working.

If I remove the autonumbering/autoform-publish code and
publish the form "manually," the command buttons work
fine (but, of course, I lose the autonumber feature).
The autonumber/autoform-publish code seems to disable the
command buttons after the form is saved and closed.

Did I mess up the code for publishing this form? Any
help would be greatly appreciated.

The code for the command button is below. It opens Word,
puts data into a word document at selected bookmarks,
prints the doc and exits without saving. It works fine
when published "manually" as the only text in the
VBscript editor.

Thanks for taking the time to read this message, any help
would be appreciated.

Bob

Code for command button
Sub CMDOpenWD_Click
Dim objWord 'As Word.Application
Dim objDoc 'As Word.Document
Dim objSelect 'As Word.Selection
Dim objDocItem 'As DocumentItem
Dim control'As outlook control
Dim control2'As outlook control
Dim Control3'As outlook control
Dim ctlSubject'As outlook control subject Dim
formpage 'As page
Set FormPage = Item.GetInspector.ModifiedFormPages("Task
Details")
Set control = formpage.controls("textbox6")
Set control2 = formpage.controls("textbox7")
Set control3 = formpage.controls("textbox2")
Set objWord = CreateObject("Word.Application")
ctlSubject = item.subject
Set objDoc = objWord.Documents.open("d:\XP Cover
sheet1.doc" )
Set objSelect = objWord.Selection
objdoc.bookmarks("b1").select
objSelect.TypeText control.text
objdoc.bookmarks("b2").select
objSelect.TypeText control2.text
objdoc.bookmarks("b3").select
objSelect.TypeText control3.text
objdoc.bookmarks("b4").select
objSelect.TypeText ctlsubject
'objword.visible = true
objdoc.application.printout Msgbox "File printing"
Const wdDoNotSaveChanges = 0
objdoc.close wdDoNotSaveChanges
Set objdoc = nothing
objword.quit
Set objWord = Nothing
End Sub
 
S

Sue Mosher [MVP-Outlook]

So you're altering the controls and republishing the form every time the
user creates a new item in order to implement an autonumbering scheme? This
simply is not a good idea. You are causing the current item to one-off --
i.e. for the form definition to become embedded in the item.

The data for any autonumbering scheme needs to be external to the form
definition. Depending on the application, it can be in a text file, in a
database, or in an Outlook item.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
B

Bob S

Thanks for the reply. I am doing this in accordance with
the Microsoft Knowledge Base Article - 180764 on the
subject. Only the article increments the mileage field.

So you are saying this article is recommending a bad
procedure and you are recommending have another program,
excel for example, perform the autonumbering and tie this
back to the form.

I'm new to the community news groups and knowledge base,
so I'm trying to figure out if I'm wasting my time in the
KB. I spent some time looking at this, figuring it out,
and applying it. And now it looks like I did it all for
naught. Thanks again

Bob
 
S

Sue Mosher [MVP-Outlook]

You are actually taking a different approach. The article sets a data
property. You are setting control properties. They're not the same thing.

That said, getting the number from a database or Outlook item, as also
described in that article, is the preferred approach. Constantly
republishing the form isn't a good idea.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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