Custom form not saving when fields changed programatically

K

karlman

Is there a way to force a form as "dirty" so Outlook attempts to save
it if someone clicks the 'X'?

I am changing the HTMLBody and even a custom field but the if I click
the 'X' then the form closes without saving. If I change any field
using the GUI then it does ask to save if I press the 'X'.

I have code that tells me if the form is dirty. Should I add something
in the Item_Close event and force the save at that point?

Thanks
Karl
 
K

karlman

Dirtying the Subject property should work.

--
Sue Mosher, Outlook MVP
   Author of Microsoft Outlook 2007 Programming:
     Jumpstart for Power Users and Administrators
   http://www.outlookcode.com/article.aspx?id=54








- Show quoted text -

I was attempting to use the Milage property to no effect. I tried the
subject and that did not work either. I even put the subject field on
the form and can watch it change but Outlook still thinks it is a
"clean" form.

Interestingly, if I dirty the subject field manually through the GUI
then the form becomes dirty. Form some reason changing a custom or
builtin property through code is not dirtying the form. Only if I do
it throught the GUI.

I am probably missing something simple here. Maybe a setting on the
form?

Thanks
Karl
 
S

Sue Mosher [MVP-Outlook]

No settings are involved. Maybe it's time you showed some code and explained whether you're writing code behind the form or in some other environment.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Dirtying the Subject property should work.






- Show quoted text -

I was attempting to use the Milage property to no effect. I tried the
subject and that did not work either. I even put the subject field on
the form and can watch it change but Outlook still thinks it is a
"clean" form.

Interestingly, if I dirty the subject field manually through the GUI
then the form becomes dirty. Form some reason changing a custom or
builtin property through code is not dirtying the form. Only if I do
it throught the GUI.

I am probably missing something simple here. Maybe a setting on the
form?

Thanks
Karl
 
K

karlman

No settings are involved. Maybe it's time you showed some code and explained whether you're writing code behind the form or in some other environment.

--
Sue Mosher, Outlook MVP
   Author of Microsoft Outlook 2007 Programming:
     Jumpstart for Power Users and Administrators
   http://www.outlookcode.com/article.aspx?id=54






I was attempting to use the Milage property to no effect. I tried the
subject and that did not work either. I even put the subject field on
the form and can watch it change but Outlook still thinks it is a
"clean" form.

Interestingly, if I dirty the subject field manually through the GUI
then the form becomes dirty. Form some reason changing a custom or
builtin property through code is not dirtying the form. Only if I do
it throught the GUI.

I am probably missing something simple here. Maybe a setting on the
form?

Thanks
Karl- Hide quoted text -

- Show quoted text -

I am just adding code behind the form. I do write the code in MS
Visual Studio so I get the nicer editor and IntelliSense but then I
just copy/paste in the the form's code window.

I used to always keep an audit trail in the Message field. It was
simple but since users needed to add things themselves it could be
changed. Methods of detecting tampering were crude. This time around I
decided to make the message field read only and then provide a textbox
and button for adding notes. When you type something in the textbox
and push the button it adds the text to the message. I am actually
inserting it into the top of an HTML table using the HTMLBody
property. It works fine other than not dirtying the form so if that is
the only change you make it does not want to save it. Of course, I can
flag that it was updated and force a save in the Item_Close event if
the Item_Write event has not fired yet. That works but that also means
that if someone presses the X button the form is saved and that does
not make sense.

Here is where I update the HTMLBody field (also Mileage and Subject)
Sub AddNote(sText)
' Check
If Trim(sText) = "" Then Exit Sub

' Check if first add
If Not bAddedNote Then
bAddedNote = True
Item.Mileage = Item.Mileage + 1
Item.Subject = Now
End If

' Get existing body
sHTML = Item.HTMLBody

' Search for end of header
iPos = InStr(sHTML, "</th></tr>")

' Highlight so each users notes are grouped
sHL = ""
If Item.Mileage / 2 = Int(Item.Mileage / 2) Then sHL = ";background-
color: lightgoldenrodyellow"

' Insert new line
sHTML2 = Left(sHTML, iPos + 9)
sHTML2 = sHTML2 & "<tr><td style='width:100px" & sHL & "'>" &
FormatDateTime(Now, 2) & " " & FormatDateTime(Now, 4) & "</td>"
sHTML2 = sHTML2 & "<td style='width:125px" & sHL & "'>" &
oPF.CurrentUser & "</td>"
sHTML2 = sHTML2 & "<td style='" & sHL & "'>" & sText & "</td></tr>"
sHTML2 = sHTML2 & Mid(sHTML, iPos + 10)

' Update body
Item.HTMLBody = sHTML2

End Sub


Here is what I have so far in the Item_Write and Item_Close events:
' If readonly then do not save
If oPF.ReadOnly Then
Item_Write = False
Item.Close(1)
Exit Function
End If

' Save If New
If oPF.IsNew Then Item.Save

' Update user types
oSM.AddUserType "LP", oPF.Field("LPName")
oSM.AddUserType "LC", oPF.Field("LCName")
oSM.AddUserType "DEUW", oPF.Field("DEUWName")
oSM.AddUserType "Funder", oPF.Field("FunderName")
oSM.AddUserType "Treasury", oPF.Field("TreasuryName")

' Send emails
'If Ubound(oSM.GetEmailRecipients(oPF.Field("CurrentStatus"))) > -1
Then
'For Each sEmail In
oSM.GetEmailRecipients(oPF.Field("CurrentStatus"))
' If Trim(sEmail) <> "" Then
' oPF.SendEmail sEmail,
oSM.GetEmailText(oPF.Field("CurrentStatus")),"TEST"
' End If
'Next
'End If

' Update subject
oPF.CreatePFItem(oPF.Controls("txtLoanNumber").Value)

bWriteFired = True
End Function


Function Item_Close()
' Unlock
oPF.UnlockItem(False)

End Function
 
S

Sue Mosher [MVP-Outlook]

What are oPF and oSM? What is the value of Saved after you change HTMLBody?

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


karlman said:
I was attempting to use the Milage property to no effect. I tried the
subject and that did not work either. I even put the subject field on
the form and can watch it change but Outlook still thinks it is a
"clean" form.

Interestingly, if I dirty the subject field manually through the GUI
then the form becomes dirty. Form some reason changing a custom or
builtin property through code is not dirtying the form. Only if I do
it throught the GUI.

This time around I
decided to make the message field read only and then provide a textbox
and button for adding notes. When you type something in the textbox
and push the button it adds the text to the message. I am actually
inserting it into the top of an HTML table using the HTMLBody
property. It works fine other than not dirtying the form so if that is
the only change you make it does not want to save it. Of course, I can
flag that it was updated and force a save in the Item_Close event if
the Item_Write event has not fired yet. That works but that also means
that if someone presses the X button the form is saved and that does
not make sense.

Here is where I update the HTMLBody field (also Mileage and Subject)
Sub AddNote(sText)
' Check
If Trim(sText) = "" Then Exit Sub

' Check if first add
If Not bAddedNote Then
bAddedNote = True
Item.Mileage = Item.Mileage + 1
Item.Subject = Now
End If

' Get existing body
sHTML = Item.HTMLBody

' Search for end of header
iPos = InStr(sHTML, "</th></tr>")

' Highlight so each users notes are grouped
sHL = ""
If Item.Mileage / 2 = Int(Item.Mileage / 2) Then sHL = ";background-
color: lightgoldenrodyellow"

' Insert new line
sHTML2 = Left(sHTML, iPos + 9)
sHTML2 = sHTML2 & "<tr><td style='width:100px" & sHL & "'>" &
FormatDateTime(Now, 2) & " " & FormatDateTime(Now, 4) & "</td>"
sHTML2 = sHTML2 & "<td style='width:125px" & sHL & "'>" &
oPF.CurrentUser & "</td>"
sHTML2 = sHTML2 & "<td style='" & sHL & "'>" & sText & "</td></tr>"
sHTML2 = sHTML2 & Mid(sHTML, iPos + 10)

' Update body
Item.HTMLBody = sHTML2

End Sub


Here is what I have so far in the Item_Write and Item_Close events:
' If readonly then do not save
If oPF.ReadOnly Then
Item_Write = False
Item.Close(1)
Exit Function
End If

' Save If New
If oPF.IsNew Then Item.Save

' Update user types
oSM.AddUserType "LP", oPF.Field("LPName")
oSM.AddUserType "LC", oPF.Field("LCName")
oSM.AddUserType "DEUW", oPF.Field("DEUWName")
oSM.AddUserType "Funder", oPF.Field("FunderName")
oSM.AddUserType "Treasury", oPF.Field("TreasuryName")

' Send emails
'If Ubound(oSM.GetEmailRecipients(oPF.Field("CurrentStatus"))) > -1
Then
'For Each sEmail In
oSM.GetEmailRecipients(oPF.Field("CurrentStatus"))
' If Trim(sEmail) <> "" Then
' oPF.SendEmail sEmail,
oSM.GetEmailText(oPF.Field("CurrentStatus")),"TEST"
' End If
'Next
'End If

' Update subject
oPF.CreatePFItem(oPF.Controls("txtLoanNumber").Value)

bWriteFired = True
End Function


Function Item_Close()
' Unlock
oPF.UnlockItem(False)

End Function
 
K

karlman

What are oPF and oSM? What is the value of Saved after you change HTMLBody?

--
Sue Mosher, Outlook MVP
   Author of Microsoft Outlook 2007 Programming:
     Jumpstart for Power Users and Administrators
   http://www.outlookcode.com/article.aspx?id=54









This time around I
decided to make the message field read only and then provide a textbox
and button for adding notes. When you type something in the textbox
and push the button it adds the text to the message. I am actually
inserting it into the top of an HTML table using the HTMLBody
property. It works fine other than not dirtying the form so if that is
the only change you make it does not want to save it. Of course, I can
flag that it was updated and force a save in the Item_Close event if
the Item_Write event has not fired yet. That works but that also means
that if someone presses the X button the form is saved and that does
not make sense.

Here is where I update the HTMLBody field (also Mileage and Subject)
Sub AddNote(sText)
  ' Check
  If Trim(sText) = "" Then Exit Sub

  ' Check if first add
  If Not bAddedNote Then
    bAddedNote = True
    Item.Mileage = Item.Mileage + 1
    Item.Subject = Now
  End If

  ' Get existing body
  sHTML = Item.HTMLBody

  ' Search for end of header
  iPos = InStr(sHTML, "</th></tr>")

  ' Highlight so each users notes are grouped
  sHL = ""
  If Item.Mileage / 2 = Int(Item.Mileage / 2) Then sHL = ";background-
color: lightgoldenrodyellow"

  ' Insert new line
  sHTML2 = Left(sHTML, iPos + 9)
  sHTML2 = sHTML2 & "<tr><td style='width:100px" & sHL & "'>" &
FormatDateTime(Now, 2) & " " & FormatDateTime(Now, 4) & "</td>"
  sHTML2 = sHTML2 & "<td style='width:125px" & sHL & "'>" &
oPF.CurrentUser & "</td>"
  sHTML2 = sHTML2 & "<td style='" & sHL & "'>" & sText & "</td></tr>"
  sHTML2 = sHTML2 & Mid(sHTML, iPos + 10)

  ' Update body
  Item.HTMLBody = sHTML2

End Sub

Here is what I have so far in the Item_Write and Item_Close events:
  ' If readonly then do not save
  If oPF.ReadOnly Then
    Item_Write = False
    Item.Close(1)
    Exit Function
  End If

  ' Save If New
  If oPF.IsNew Then Item.Save

  ' Update user types
  oSM.AddUserType "LP", oPF.Field("LPName")
  oSM.AddUserType "LC", oPF.Field("LCName")
  oSM.AddUserType "DEUW", oPF.Field("DEUWName")
  oSM.AddUserType "Funder", oPF.Field("FunderName")
  oSM.AddUserType "Treasury", oPF.Field("TreasuryName")

  ' Send emails
  'If Ubound(oSM.GetEmailRecipients(oPF.Field("CurrentStatus"))) > -1
Then
  'For Each sEmail In
oSM.GetEmailRecipients(oPF.Field("CurrentStatus"))
  '  If Trim(sEmail) <> "" Then
  '    oPF.SendEmail sEmail,
oSM.GetEmailText(oPF.Field("CurrentStatus")),"TEST"
  '  End If
  'Next
  'End If

  ' Update subject
  oPF.CreatePFItem(oPF.Controls("txtLoanNumber").Value)

  bWriteFired = True
End Function

Function Item_Close()
  ' Unlock
  oPF.UnlockItem(False)

End Function- Hide quoted text -

- Show quoted text -

oPF is a specialized class I created to perform a lot of the functions
required for these public folder apps. It access a SQL server to
perform functions like locking of the item, duplicate checking, data
lookups, and maintain original field values. oSM is a specialized
class for tracking what types of users are allowed to see certain
status and who receives emails.

The Item.Saved value stays true even when I update the HTMLBody via
code (same with Mileage and Subject). If I change anything via the GUI
then it changes to False.

Tried setting Item.Saved = False in code but it appears it is a read
only property.

Thank you for your time!

Karl
 
K

karlman

oPF is a specialized class I created to perform a lot of the functions
required for these public folder apps. It access a SQL server to
perform functions like locking of the item, duplicate checking, data
lookups, and maintain original field values. oSM is a specialized
class for tracking what types of users are allowed to see certain
status and who receives emails.

The Item.Saved value stays true even when I update the HTMLBody via
code (same with Mileage and Subject). If I change anything via the GUI
then it changes to False.

Tried setting Item.Saved = False in code but it appears it is a read
only property.

Thank you for your time!

Karl- Hide quoted text -

- Show quoted text -

I have come up with a work around. I added this code in my Save button
click event:

Sub cmdSave_Click()
' Close/Save item
If bAddedNote Then Item.Save

Item.Close(0)
End Sub

This way it forces a save if I changed the HTMLBody field in code. I
then added this:

Function Item_Close()

If (bAddedNote Or Not Item.Saved) And Not bWriteFired Then
If MsgBox("Save changes?", vbYesNo, "Save?") = vbYes Then
Item.Save
End If

' Unlock
oPF.UnlockItem(False)

End Function

If the form is dirty and the Item_Write event has not completed then
it will ask if you want to save and is yes, it saves.

Seems to work just fine. I will have to do more work later to find out
why I was having the problem. Maybe see if I can reproduce the problem
on a new custom form.

Thank you for all your help Sue!
Karl
 

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