Looking for code to wait/pause for input...

A

adimax

This code is a few months old (and was put together here with the help
of some very awesome posters), and I've been recently commissioned to
upgrade it.

How it's working now is to watch the Inbox for any emails (with a
specific Subject and with a .wav attachment), and then it fires an
InputBox and if the field is empty, the user clicks cancel, or the
user clicks the X in the corner, the message is not marked as read and
is moved to Unsaved.

or

If the user inputs any data then the attached .wav file is saved with
the entered data, the message is marked read, and its then moved to
the Saved folder.

The code that does the above is working and follows:

---start code---

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub

' version 1.6
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

Dim objAttFld1 As MAPIFolder
Dim objAttFld2 As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim strAttFldName1 As String
Dim strAttFldName2 As String
Dim strProgExt As String
Dim arrExt() As String
Dim objAtt As Attachment
Dim intPos As Integer
Dim I As Integer
Dim strExt As String

On Error Resume Next

Dim strTimeStamp As String
Dim objDes As String
Dim objFilename As String

' set the Inbox subfolders where messages w/ .wav attachments will
be moved to
strAttFldName1 = "[Saved Recordings]"
strAttFldName2 = "[Unsaved Recordings]"

Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objAttFld1 = objInbox.Parent.Folders(strAttFldName1)
Set objAttFld2 = objInbox.Parent.Folders(strAttFldName2)

' set delimited list of extensions to trap
strProgExt = "wav"

' 1st check if the correct folders are in place (non-triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
End If


' check to see if the item is an email prior to executing timestamp
method
If Item.Class = olMail Then

' set the timestamp method & extract it from the email
strTimeStamp = Format(Item.ReceivedTime, "hh.mm AM/PM")

End If

' set destination folder for saved files
objDes = "W:\"

' checks subject of incoming email
If Left(Item.Subject, 39) = "New Sound Recording: " Then

' 2nd check if the correct folders are in place (triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
If Not objAttFld1 Is Nothing Then
' convert delimited list of extensions to array
arrExt = Split(strProgExt, ",")

For Each objAtt In Item.Attachments
intPos = InStrRev(objAtt.FileName, ".")
If intPos > 0 Then
' check attachment extension against array
strExt = LCase(Mid(objAtt.FileName, intPos + 1))
For I = LBound(arrExt) To UBound(arrExt)
If strExt = Trim(arrExt(I)) Then

' sets the popup box properties

StartPrompt:
objFilename = InputBox("Please type a filename below. You do not
have to include the .wav extension:", "Saving recording...", "")
If objFilename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If

' save to destination folder with inputted ticket # as a filename
+ timestamp
objAtt.SaveAsFile objDes & _
objFilename & " @ " & strTimeStamp & ".wav"

Item.UnRead = False
Item.Move objAttFld1

Exit For
End If
Next
Else
' no extension; unknown type; clicked Cancel or left
filename field blank
End If
Next
End If
End If
End If

' clear variables
Set objInbox = Nothing
Set objNS = Nothing
Set objAtt = Nothing

Exit Sub

' ErrorHandling:
' display the error's description
' MsgBox Err.Description, vbExclamation

End Sub

---end code---

The problem I am having is that we want to switch over to a userform
and add a few more options to the filename that gets generated and
saved (like what type of recording, inbound or outbound, etc). I think
I can code all of that pretty easily, but its getting the userform to
trigger in the above code and then wait for input/make a decision
based on that input (to save and mark read and move or to not save,
not mark read, and move).

Right now the code looks like (with objFilename being adjusted to
Filename):

---start code---

' sets the popup box properties

Call OpenUserForm
If Filename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If

(and the code to OpenUserForm, though its pretty simple)

Private Sub OpenUserForm()
Show.PopUpBox
End Sub

---end code---

This actually does execute when I do a test by moving a message into
the Inbox with a .wav, but it immediately takes the message and marks
it read and puts it in the Unsaved folder. I never even see the
userform... and yet I get no errors, either... oddly.

I guess what I am looking for is some advice on getting the PopUpBox
userform to open based on the filename field it exectues the rest of
the moving/marking/saving code.

Thanks in advance, I know this is (or seems to me!) a complicated one.

Benjamin
 
K

Ken Slovak - [MVP - Outlook]

I would set up a Public variable to take the return value from the UserForm
that would be filled or set to a null string when the UserForm was closing.
To call the UserForm I'd use code like this to prevent the code from
continuing to execute after the form was opened, until it's closed:

PopUpBox.Show vbModal

That opens the form modally and prevents the next lines of code from
executing until the form is closed.

If you have Public popupResult As String declared in a code module or in
ThisOutlookSession it will be available to the UserForm code to set or null
when its UserForm_Terminate() event fires.




This code is a few months old (and was put together here with the help
of some very awesome posters), and I've been recently commissioned to
upgrade it.

How it's working now is to watch the Inbox for any emails (with a
specific Subject and with a .wav attachment), and then it fires an
InputBox and if the field is empty, the user clicks cancel, or the
user clicks the X in the corner, the message is not marked as read and
is moved to Unsaved.

or

If the user inputs any data then the attached .wav file is saved with
the entered data, the message is marked read, and its then moved to
the Saved folder.

The code that does the above is working and follows:

---start code---

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub

' version 1.6
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

Dim objAttFld1 As MAPIFolder
Dim objAttFld2 As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim strAttFldName1 As String
Dim strAttFldName2 As String
Dim strProgExt As String
Dim arrExt() As String
Dim objAtt As Attachment
Dim intPos As Integer
Dim I As Integer
Dim strExt As String

On Error Resume Next

Dim strTimeStamp As String
Dim objDes As String
Dim objFilename As String

' set the Inbox subfolders where messages w/ .wav attachments will
be moved to
strAttFldName1 = "[Saved Recordings]"
strAttFldName2 = "[Unsaved Recordings]"

Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objAttFld1 = objInbox.Parent.Folders(strAttFldName1)
Set objAttFld2 = objInbox.Parent.Folders(strAttFldName2)

' set delimited list of extensions to trap
strProgExt = "wav"

' 1st check if the correct folders are in place (non-triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
End If


' check to see if the item is an email prior to executing timestamp
method
If Item.Class = olMail Then

' set the timestamp method & extract it from the email
strTimeStamp = Format(Item.ReceivedTime, "hh.mm AM/PM")

End If

' set destination folder for saved files
objDes = "W:\"

' checks subject of incoming email
If Left(Item.Subject, 39) = "New Sound Recording: " Then

' 2nd check if the correct folders are in place (triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
If Not objAttFld1 Is Nothing Then
' convert delimited list of extensions to array
arrExt = Split(strProgExt, ",")

For Each objAtt In Item.Attachments
intPos = InStrRev(objAtt.FileName, ".")
If intPos > 0 Then
' check attachment extension against array
strExt = LCase(Mid(objAtt.FileName, intPos + 1))
For I = LBound(arrExt) To UBound(arrExt)
If strExt = Trim(arrExt(I)) Then

' sets the popup box properties

StartPrompt:
objFilename = InputBox("Please type a filename below. You do not
have to include the .wav extension:", "Saving recording...", "")
If objFilename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If

' save to destination folder with inputted ticket # as a filename
+ timestamp
objAtt.SaveAsFile objDes & _
objFilename & " @ " & strTimeStamp & ".wav"

Item.UnRead = False
Item.Move objAttFld1

Exit For
End If
Next
Else
' no extension; unknown type; clicked Cancel or left
filename field blank
End If
Next
End If
End If
End If

' clear variables
Set objInbox = Nothing
Set objNS = Nothing
Set objAtt = Nothing

Exit Sub

' ErrorHandling:
' display the error's description
' MsgBox Err.Description, vbExclamation

End Sub

---end code---

The problem I am having is that we want to switch over to a userform
and add a few more options to the filename that gets generated and
saved (like what type of recording, inbound or outbound, etc). I think
I can code all of that pretty easily, but its getting the userform to
trigger in the above code and then wait for input/make a decision
based on that input (to save and mark read and move or to not save,
not mark read, and move).

Right now the code looks like (with objFilename being adjusted to
Filename):

---start code---

' sets the popup box properties

Call OpenUserForm
If Filename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If

(and the code to OpenUserForm, though its pretty simple)

Private Sub OpenUserForm()
Show.PopUpBox
End Sub

---end code---

This actually does execute when I do a test by moving a message into
the Inbox with a .wav, but it immediately takes the message and marks
it read and puts it in the Unsaved folder. I never even see the
userform... and yet I get no errors, either... oddly.

I guess what I am looking for is some advice on getting the PopUpBox
userform to open based on the filename field it exectues the rest of
the moving/marking/saving code.

Thanks in advance, I know this is (or seems to me!) a complicated one.

Benjamin
 
A

adimax

Ken,

Adding the 'vbModal' to showing the userform PopUpBox worked
perfectly. It gets loaded and the code stops until the form is dealt
with. Thank you for that. ;)

I also went ahead and tried your suggestion about using a Public
popupResult As String, but this part I can not seem to get working. No
matter if I leave the textbox (named Filename) in the form blank or
enter in data, it executes after the userform as if it had been blank.
Here's a few more snippets from the code:

Private Sub OKButton_Click()
Filename.Value = popupResult
(then some code to clear the userform and close it)
End Sub

(and here is the code I added to ThisOutlookSession)

Public Sub Results()
Dim popupResult As String
End Sub

I've never really played with Public vs. Private before, this will be
a first I believe. Does that look correct? I'm then just going ahead
and trying to do the code thats basically a If/Then on popupResult =
"" (or not).

Thanks for your guidance thus farm
Benjamin


I would set up a Public variable to take the return value from the UserForm
that would be filled or set to a null string when the UserForm was closing.
To call the UserForm I'd use code like this to prevent the code from
continuing to execute after the form was opened, until it's closed:

PopUpBox.Show vbModal

That opens the form modally and prevents the next lines of code from
executing until the form is closed.

If you have Public popupResult As String declared in a code module or in
ThisOutlookSession it will be available to the UserForm code to set or null
when its UserForm_Terminate() event fires.

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm


This code is a few months old (and was put together here with the help
of some very awesome posters), and I've been recently commissioned to
upgrade it.
How it's working now is to watch the Inbox for any emails (with a
specific Subject and with a .wav attachment), and then it fires an
InputBox and if the field is empty, the user clicks cancel, or the
user clicks the X in the corner, the message is not marked as read and
is moved to Unsaved.

If the user inputs any data then the attached .wav file is saved with
the entered data, the message is marked read, and its then moved to
the Saved folder.
The code that does the above is working and follows:
---start code---
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub
' version 1.6
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim objAttFld1 As MAPIFolder
Dim objAttFld2 As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim strAttFldName1 As String
Dim strAttFldName2 As String
Dim strProgExt As String
Dim arrExt() As String
Dim objAtt As Attachment
Dim intPos As Integer
Dim I As Integer
Dim strExt As String
On Error Resume Next
Dim strTimeStamp As String
Dim objDes As String
Dim objFilename As String
' set the Inbox subfolders where messages w/ .wav attachments will
be moved to
strAttFldName1 = "[Saved Recordings]"
strAttFldName2 = "[Unsaved Recordings]"
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objAttFld1 = objInbox.Parent.Folders(strAttFldName1)
Set objAttFld2 = objInbox.Parent.Folders(strAttFldName2)
' set delimited list of extensions to trap
strProgExt = "wav"
' 1st check if the correct folders are in place (non-triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
End If
' check to see if the item is an email prior to executing timestamp
method
If Item.Class = olMail Then
' set the timestamp method & extract it from the email
strTimeStamp = Format(Item.ReceivedTime, "hh.mm AM/PM")
' set destination folder for saved files
objDes = "W:\"
' checks subject of incoming email
If Left(Item.Subject, 39) = "New Sound Recording: " Then
' 2nd check if the correct folders are in place (triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
If Not objAttFld1 Is Nothing Then
' convert delimited list of extensions to array
arrExt = Split(strProgExt, ",")
For Each objAtt In Item.Attachments
intPos = InStrRev(objAtt.FileName, ".")
If intPos > 0 Then
' check attachment extension against array
strExt = LCase(Mid(objAtt.FileName, intPos + 1))
For I = LBound(arrExt) To UBound(arrExt)
If strExt = Trim(arrExt(I)) Then
' sets the popup box properties
StartPrompt:
objFilename = InputBox("Please type a filename below. You do not
have to include the .wav extension:", "Saving recording...", "")
If objFilename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
' save to destination folder with inputted ticket # as a filename
+ timestamp
objAtt.SaveAsFile objDes & _
objFilename & " @ " & strTimeStamp & ".wav"
Item.UnRead = False
Item.Move objAttFld1
Exit For
End If
Next
Else
' no extension; unknown type; clicked Cancel or left
filename field blank
End If
Next
End If
End If
End If
' clear variables
Set objInbox = Nothing
Set objNS = Nothing
Set objAtt = Nothing
' ErrorHandling:
' display the error's description
' MsgBox Err.Description, vbExclamation
---end code---
The problem I am having is that we want to switch over to a userform
and add a few more options to the filename that gets generated and
saved (like what type of recording, inbound or outbound, etc). I think
I can code all of that pretty easily, but its getting the userform to
trigger in the above code and then wait for input/make a decision
based on that input (to save and mark read and move or to not save,
not mark read, and move).
Right now the code looks like (with objFilename being adjusted to
Filename):
---start code---
' sets the popup box properties
Call OpenUserForm
If Filename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
(and the code to OpenUserForm, though its pretty simple)
Private Sub OpenUserForm()
Show.PopUpBox
End Sub
---end code---
This actually does execute when I do a test by moving a message into
the Inbox with a .wav, but it immediately takes the message and marks
it read and puts it in the Unsaved folder. I never even see the
userform... and yet I get no errors, either... oddly.
I guess what I am looking for is some advice on getting the PopUpBox
userform to open based on the filename field it exectues the rest of
the moving/marking/saving code.
Thanks in advance, I know this is (or seems to me!) a complicated one.
 
A

adimax

Ken,

Adding the 'vbModal' to showing the userform PopUpBox worked
perfectly. It gets loaded and the code stops until the form is dealt
with. Thank you for that. ;)

I also went ahead and tried your suggestion about using a Public
popupResult As String, but this part I can not seem to get working. No
matter if I leave the textbox (named Filename) in the form blank or
enter in data, it executes after the userform as if it had been blank.
Here's a few more snippets from the code:

Private Sub OKButton_Click()
Filename.Value = popupResult
(then some code to clear the userform and close it)
End Sub

(and here is the code I added to ThisOutlookSession)

Public Sub Results()
Dim popupResult As String
End Sub

I've never really played with Public vs. Private before, this will be
a first I believe. Does that look correct? I'm then just going ahead
and trying to do the code thats basically a If/Then on popupResult =
"" (or not).

Thanks for your guidance thus farm
Benjamin


I would set up a Public variable to take the return value from the UserForm
that would be filled or set to a null string when the UserForm was closing.
To call the UserForm I'd use code like this to prevent the code from
continuing to execute after the form was opened, until it's closed:

PopUpBox.Show vbModal

That opens the form modally and prevents the next lines of code from
executing until the form is closed.

If you have Public popupResult As String declared in a code module or in
ThisOutlookSession it will be available to the UserForm code to set or null
when its UserForm_Terminate() event fires.

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm


This code is a few months old (and was put together here with the help
of some very awesome posters), and I've been recently commissioned to
upgrade it.
How it's working now is to watch the Inbox for any emails (with a
specific Subject and with a .wav attachment), and then it fires an
InputBox and if the field is empty, the user clicks cancel, or the
user clicks the X in the corner, the message is not marked as read and
is moved to Unsaved.

If the user inputs any data then the attached .wav file is saved with
the entered data, the message is marked read, and its then moved to
the Saved folder.
The code that does the above is working and follows:
---start code---
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub
' version 1.6
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim objAttFld1 As MAPIFolder
Dim objAttFld2 As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim strAttFldName1 As String
Dim strAttFldName2 As String
Dim strProgExt As String
Dim arrExt() As String
Dim objAtt As Attachment
Dim intPos As Integer
Dim I As Integer
Dim strExt As String
On Error Resume Next
Dim strTimeStamp As String
Dim objDes As String
Dim objFilename As String
' set the Inbox subfolders where messages w/ .wav attachments will
be moved to
strAttFldName1 = "[Saved Recordings]"
strAttFldName2 = "[Unsaved Recordings]"
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objAttFld1 = objInbox.Parent.Folders(strAttFldName1)
Set objAttFld2 = objInbox.Parent.Folders(strAttFldName2)
' set delimited list of extensions to trap
strProgExt = "wav"
' 1st check if the correct folders are in place (non-triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
End If
' check to see if the item is an email prior to executing timestamp
method
If Item.Class = olMail Then
' set the timestamp method & extract it from the email
strTimeStamp = Format(Item.ReceivedTime, "hh.mm AM/PM")
' set destination folder for saved files
objDes = "W:\"
' checks subject of incoming email
If Left(Item.Subject, 39) = "New Sound Recording: " Then
' 2nd check if the correct folders are in place (triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
If Not objAttFld1 Is Nothing Then
' convert delimited list of extensions to array
arrExt = Split(strProgExt, ",")
For Each objAtt In Item.Attachments
intPos = InStrRev(objAtt.FileName, ".")
If intPos > 0 Then
' check attachment extension against array
strExt = LCase(Mid(objAtt.FileName, intPos + 1))
For I = LBound(arrExt) To UBound(arrExt)
If strExt = Trim(arrExt(I)) Then
' sets the popup box properties
StartPrompt:
objFilename = InputBox("Please type a filename below. You do not
have to include the .wav extension:", "Saving recording...", "")
If objFilename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
' save to destination folder with inputted ticket # as a filename
+ timestamp
objAtt.SaveAsFile objDes & _
objFilename & " @ " & strTimeStamp & ".wav"
Item.UnRead = False
Item.Move objAttFld1
Exit For
End If
Next
Else
' no extension; unknown type; clicked Cancel or left
filename field blank
End If
Next
End If
End If
End If
' clear variables
Set objInbox = Nothing
Set objNS = Nothing
Set objAtt = Nothing
' ErrorHandling:
' display the error's description
' MsgBox Err.Description, vbExclamation
---end code---
The problem I am having is that we want to switch over to a userform
and add a few more options to the filename that gets generated and
saved (like what type of recording, inbound or outbound, etc). I think
I can code all of that pretty easily, but its getting the userform to
trigger in the above code and then wait for input/make a decision
based on that input (to save and mark read and move or to not save,
not mark read, and move).
Right now the code looks like (with objFilename being adjusted to
Filename):
---start code---
' sets the popup box properties
Call OpenUserForm
If Filename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
(and the code to OpenUserForm, though its pretty simple)
Private Sub OpenUserForm()
Show.PopUpBox
End Sub
---end code---
This actually does execute when I do a test by moving a message into
the Inbox with a .wav, but it immediately takes the message and marks
it read and puts it in the Unsaved folder. I never even see the
userform... and yet I get no errors, either... oddly.
I guess what I am looking for is some advice on getting the PopUpBox
userform to open based on the filename field it exectues the rest of
the moving/marking/saving code.
Thanks in advance, I know this is (or seems to me!) a complicated one.
 
A

adimax

Ken,

Adding the 'vbModal' to showing the userform PopUpBox worked
perfectly. It gets loaded and the code stops until the form is dealt
with. Thank you for that. ;)

I also went ahead and tried your suggestion about using a Public
popupResult As String, but this part I can not seem to get working. No
matter if I leave the textbox (named Filename) in the form blank or
enter in data, it executes after the userform as if it had been blank.
Here's a few more snippets from the code:

Private Sub OKButton_Click()
Filename.Value = popupResult
(then some code to clear the userform and close it)
End Sub

(and here is the code I added to ThisOutlookSession)

Public Sub Results()
Dim popupResult As String
End Sub

I've never really played with Public vs. Private before, this will be
a first I believe. Does that look correct? I'm then just going ahead
and trying to do the code thats basically a If/Then on popupResult =
"" (or not).

Thanks for your guidance thus farm
Benjamin


I would set up a Public variable to take the return value from the UserForm
that would be filled or set to a null string when the UserForm was closing.
To call the UserForm I'd use code like this to prevent the code from
continuing to execute after the form was opened, until it's closed:

PopUpBox.Show vbModal

That opens the form modally and prevents the next lines of code from
executing until the form is closed.

If you have Public popupResult As String declared in a code module or in
ThisOutlookSession it will be available to the UserForm code to set or null
when its UserForm_Terminate() event fires.

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm


This code is a few months old (and was put together here with the help
of some very awesome posters), and I've been recently commissioned to
upgrade it.
How it's working now is to watch the Inbox for any emails (with a
specific Subject and with a .wav attachment), and then it fires an
InputBox and if the field is empty, the user clicks cancel, or the
user clicks the X in the corner, the message is not marked as read and
is moved to Unsaved.

If the user inputs any data then the attached .wav file is saved with
the entered data, the message is marked read, and its then moved to
the Saved folder.
The code that does the above is working and follows:
---start code---
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub
' version 1.6
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim objAttFld1 As MAPIFolder
Dim objAttFld2 As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim strAttFldName1 As String
Dim strAttFldName2 As String
Dim strProgExt As String
Dim arrExt() As String
Dim objAtt As Attachment
Dim intPos As Integer
Dim I As Integer
Dim strExt As String
On Error Resume Next
Dim strTimeStamp As String
Dim objDes As String
Dim objFilename As String
' set the Inbox subfolders where messages w/ .wav attachments will
be moved to
strAttFldName1 = "[Saved Recordings]"
strAttFldName2 = "[Unsaved Recordings]"
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objAttFld1 = objInbox.Parent.Folders(strAttFldName1)
Set objAttFld2 = objInbox.Parent.Folders(strAttFldName2)
' set delimited list of extensions to trap
strProgExt = "wav"
' 1st check if the correct folders are in place (non-triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
End If
' check to see if the item is an email prior to executing timestamp
method
If Item.Class = olMail Then
' set the timestamp method & extract it from the email
strTimeStamp = Format(Item.ReceivedTime, "hh.mm AM/PM")
' set destination folder for saved files
objDes = "W:\"
' checks subject of incoming email
If Left(Item.Subject, 39) = "New Sound Recording: " Then
' 2nd check if the correct folders are in place (triggered)
If Item.Class = olMail Then
If objAttFld1 Is Nothing Then
' create [Saved] folder if needed
Set objAttFld1 = objInbox.Parent.Folders.Add(strAttFldName1)
End If
If objAttFld2 Is Nothing Then
' create [Unsaved] folder if needed
Set objAttFld2 = objInbox.Parent.Folders.Add(strAttFldName2)
End If
If Not objAttFld1 Is Nothing Then
' convert delimited list of extensions to array
arrExt = Split(strProgExt, ",")
For Each objAtt In Item.Attachments
intPos = InStrRev(objAtt.FileName, ".")
If intPos > 0 Then
' check attachment extension against array
strExt = LCase(Mid(objAtt.FileName, intPos + 1))
For I = LBound(arrExt) To UBound(arrExt)
If strExt = Trim(arrExt(I)) Then
' sets the popup box properties
StartPrompt:
objFilename = InputBox("Please type a filename below. You do not
have to include the .wav extension:", "Saving recording...", "")
If objFilename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
' save to destination folder with inputted ticket # as a filename
+ timestamp
objAtt.SaveAsFile objDes & _
objFilename & " @ " & strTimeStamp & ".wav"
Item.UnRead = False
Item.Move objAttFld1
Exit For
End If
Next
Else
' no extension; unknown type; clicked Cancel or left
filename field blank
End If
Next
End If
End If
End If
' clear variables
Set objInbox = Nothing
Set objNS = Nothing
Set objAtt = Nothing
' ErrorHandling:
' display the error's description
' MsgBox Err.Description, vbExclamation
---end code---
The problem I am having is that we want to switch over to a userform
and add a few more options to the filename that gets generated and
saved (like what type of recording, inbound or outbound, etc). I think
I can code all of that pretty easily, but its getting the userform to
trigger in the above code and then wait for input/make a decision
based on that input (to save and mark read and move or to not save,
not mark read, and move).
Right now the code looks like (with objFilename being adjusted to
Filename):
---start code---
' sets the popup box properties
Call OpenUserForm
If Filename = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
(and the code to OpenUserForm, though its pretty simple)
Private Sub OpenUserForm()
Show.PopUpBox
End Sub
---end code---
This actually does execute when I do a test by moving a message into
the Inbox with a .wav, but it immediately takes the message and marks
it read and puts it in the Unsaved folder. I never even see the
userform... and yet I get no errors, either... oddly.
I guess what I am looking for is some advice on getting the PopUpBox
userform to open based on the filename field it exectues the rest of
the moving/marking/saving code.
Thanks in advance, I know this is (or seems to me!) a complicated one.
 
K

Ken Slovak - [MVP - Outlook]

If you want the public variable set by the form before it closes you have
that line reversed, it should be:

Private Sub OKButton_Click()
popupResult = Filename.Value
 
A

adimax

Ken,

Argh! I cant believe I had that inverted. However, still suck. I know
we are getting closer... I can feel it. :)

popupResult = Filename.Value

That totally makes sense. Make this String this Value. Got it.
However, even with that set it's still executing the code after the
userform closes as if Filename's value was "" (blank). I thought it
might have been caused by my code to clear all of the fields in the
userform, but that wasnt it.

Then I recalled an old trick on here and added this:

Call OpenUserForm
MsgBox popupResult
MsgBox strTimeStamp
If popupResult = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If

When run: it calls the userform. I enter 'test' into the Filename
textbox and click Ok. 1st popup is blank, only has an OK button. 2nd
popup does have the strTimeStamp correctly showing in it (in this
case, 04.45 PM) and the OK button... so it really does think Filename
is blank.

To review,

Private Sub OKButton_Click()
popupResult = Filename.Value
PopUpBox.Hide
End Sub

That should be a Hide at the end, right? There isnt some other command
I am missing that writes/saves changes entered into the userform?

I'm almost tempted to add another textbox to the userform with another
Public string and do a 3rd MsgBox to see if it shows up!

Benjamin

If you want the public variable set by the form before it closes you have
that line reversed, it should be:

Private Sub OKButton_Click()
popupResult = Filename.Value

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm


Adding the 'vbModal' to showing the userform PopUpBox worked
perfectly. It gets loaded and the code stops until the form is dealt
with. Thank you for that. ;)
I also went ahead and tried your suggestion about using a Public
popupResult As String, but this part I can not seem to get working. No
matter if I leave the textbox (named Filename) in the form blank or
enter in data, it executes after the userform as if it had been blank.
Here's a few more snippets from the code:
Private Sub OKButton_Click()
Filename.Value = popupResult
(then some code to clear the userform and close it)
End Sub
(and here is the code I added to ThisOutlookSession)
Public Sub Results()
Dim popupResult As String
End Sub
I've never really played with Public vs. Private before, this will be
a first I believe. Does that look correct? I'm then just going ahead
and trying to do the code thats basically a If/Then on popupResult =
"" (or not).
Thanks for your guidance thus far,
Benjamin
 
K

Ken Slovak - [MVP - Outlook]

If Filename is a textbox control have you tried using the Filename.Text
property to feed into popupResult? (although for a textbox the Text and
Value properties should be identical)

You shouldn't have to be jumping through hoops on this, it's pretty simple.
A public string variable is populated with the contents of a textbox when
the OK button is clicked. If there's text in the textbox either
Filename.Text or Filename.Value should return the text.

Just out of curiosity, have you tried tabbing out of the textbox before you
click OK?
 
A

adimax

Ken,

Believe me, I've totally confused and running out of hair to pull on
this one.

I have tried Filename.Text and .Value, neither populate it.

When I add this prior to the check for popupResult's current value,
the code executes perfectly:

' popupResult = "HelpMe"

(it shows up in the MsgBox test and moves the file to the correct
folder/saves it with the filename HelpMe... etc)

I've tried placing:

Public Sub Strings()
Dim popupResult As String
End Sub

In ThisOutlookSession, its own module, inside the userform's code, and
none of it makes a difference (it was based off of what Howard said in
http://groups.google.com/group/micr...=en&lnk=gst&q=userform+value#f91f04b09ece9ee9).

I even went so far as to remake everything: the userform (UserForm1)
the text box (TextBox1) the button (CommandButton1).

I plugged it all in to the code, and still no luck.

I've never played with Property Get/Let, but I'm reading the Help
files on it now.

Thanks for all your words and insight,
Benjamin

If Filename is a textbox control have you tried using the Filename.Text
property to feed into popupResult? (although for a textbox the Text and
Value properties should be identical)

You shouldn't have to be jumping through hoops on this, it's pretty simple.
A public string variable is populated with the contents of a textbox when
the OK button is clicked. If there's text in the textbox either
Filename.Text or Filename.Value should return the text.

Just out of curiosity, have you tried tabbing out of the textbox before you
click OK?

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm


Argh! I cant believe I had that inverted. However, still suck. I know
we are getting closer... I can feel it. :)
popupResult = Filename.Value
That totally makes sense. Make this String this Value. Got it.
However, even with that set it's still executing the code after the
userform closes as if Filename's value was "" (blank). I thought it
might have been caused by my code to clear all of the fields in the
userform, but that wasnt it.
Then I recalled an old trick on here and added this:
Call OpenUserForm
MsgBox popupResult
MsgBox strTimeStamp
If popupResult = "" Then
Item.UnRead = True
Item.Move objAttFld2
Exit Sub
End If
When run: it calls the userform. I enter 'test' into the Filename
textbox and click Ok. 1st popup is blank, only has an OK button. 2nd
popup does have the strTimeStamp correctly showing in it (in this
case, 04.45 PM) and the OK button... so it really does think Filename
is blank.
To review,
Private Sub OKButton_Click()
popupResult = Filename.Value
PopUpBox.Hide
End Sub
That should be a Hide at the end, right? There isnt some other command
I am missing that writes/saves changes entered into the userform?
I'm almost tempted to add another textbox to the userform with another
Public string and do a 3rd MsgBox to see if it shows up!
 
K

Ken Slovak - [MVP - Outlook]

OK, I tested this here and with proper qualification of the popupResult
string variable in ThisOutlookSession it works just fine.

' In ThisOutlookSession:
Public popupResult As String

' In cmdOK.Click() in the UserForm:
Private Sub CommandButton1_Click()
ThisOutlookSession.popupResult = Me.TextBox1.Text
End Sub

'In a code module to call the user form:
Public Sub StartUserForm()
MsgBox ThisOutlookSession.popupResult

UserForm1.Show vbModal

MsgBox ThisOutlookSession.popupResult
End Sub

If I put the declaration for popupResult in the code module instead of in
ThisOutlookSession I can refere to popupResult without the class qualifier
for ThisOutlookSession, just as popupResult.
 

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