Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using forms for questionares in outlook 2

Status
Not open for further replies.

smithcza

Programmer
Jan 7, 2005
80
ZA
Hi. I have been asked to create a "user friendly" questionare that can be easily send via mail, completed and then returned to sender. It will the type of "select 1 to 5", so radio buttons will be the way to go. The data must preferable be extractable in CSV / excell format. Does anybody have any good suggetions, I am still a bit clumsy in VBA for Outlook.

SmithcZA
 
SmithcZA,
Since nobody has input I will chime in.

I have never had muck luck with Outlook forms (too much work, too little return) so here is a solution based around an HTML form that you can email. The functionality is all in Outlook and does three things:[ol]
[li]Create a new Email.[/li]
[li]Set the [tt]HTMLBody[/tt] of the new email by grabbing the text in a 'template'.[/li]
[li]Sends the email.[/li][/ol]

Once the recipient responds, the email response can then be parsed into a CSV/Excel format (see note below).

There are two pieces, the routines that you will need in Outlook, and the HTML template.

Here are the routines for Outlook:
Code:
Sub NewHTMLMessage()
Dim MyMail As MailItem
Set MyMail = CreateItem(olMailItem)
With MyMail
  .HTMLBody = HTMLFormData
  .Subject = "Your outbound subject here"
  .To = "Someone@Somewhere.com"
  'Saving the message in Development, change to Send in production
  .Save
End With
Set MyMail = Nothing
End Sub


Function HTMLFormData() As String
Dim intFile As Integer
Dim strTemplateName As String
intFile = FreeFile
'This is the loaction of the template file
strTemplateName = "C:\FormTemplate.htm"
Open strTemplateName For Input As #intFile
HTMLFormData = Input(LOF(intFile), #intFile)
Close #intFile
End Function

Here is the HTML template, just copy and paste the follwing text into Notepad and save as [tt]C:\FormTemplate.htm[/tt]:
Code:
<HTML>
<BODY>
<FORM method="post" action="mailto:YourName@YourCompany.com?subject=Your Inbound Subject here" enctype="text/plain">
<INPUT TYPE="hidden" NAME="internalsubject" VALUE="hidden submit"><P>

Question 1:<P>
<INPUT TYPE=Radio Name=Question1 Value=1>One
<INPUT TYPE=Radio Name=Question1 Value=2>Two
<INPUT TYPE=Radio Name=Question1 Value=3>Three
<INPUT TYPE=Radio Name=Question1 Value=4>Four
<INPUT TYPE=Radio Name=Question1 Value=5>Five<P>

Question 2:<P>
<INPUT TYPE=Radio Name=Question2 Value=1>One
<INPUT TYPE=Radio Name=Question2 Value=2>Two
<INPUT TYPE=Radio Name=Question2 Value=3>Three
<INPUT TYPE=Radio Name=Question2 Value=4>Four
<INPUT TYPE=Radio Name=Question2 Value=5>Five<P>

(rest of form content)<P>
<INPUT Type=Submit VALUE=Respond>

</FORM>
</BODY>
</HTML>

A couple of notes:[ul]
[li][tt]C:\FormTemplate.htm[/tt] can be created and edited in your favorite HTML editor (Notepad, Front Page, Word, Excel...)[/li]
[li]The HTML form in the message will not work right until sent (I had to send it to myself to test).[/li]
[li]The recipient of the message will probably get a warning when they click 'Response'. You might want to put a heads up in your HTML template letting them know this is ok.[/li][/ul]

If you like this approach I can get you the code to cycle through an Outlook Inbox and convert the responses to Excel.

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Hi Please be patient with me, I am still new with VBA in outlook. How can I get Outlook to fire the macro?

SmithcZA
 
SmithcZA,
For testing I was calling [tt]NewHTMLMessage[/tt] from the Immediate window or placing the cursor somewhere in the routine and pressing the Play button (F5).

For a more permanent solution you could create a custom toolbar with a button that points to the routine.

How are you intending to use this? To send the messages one at a time or in batches?

I tend to develop more for batch processes where you have a list of recipients in say Excel or Access and send the messages en-masse.

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Hi

At this stage i will probably use it one at a time.

Problem, if I click the submit button with the HTML sheet as the body of a mail, it doesn't want to respond. But when I open the HTML in internet explorer, it will respond OK.

SmithcZA
 
SmithcZA,
CautionMP said:
The HTML form in the message will not work right until sent (I had to send it to myself to test).

I'm guessing that when you click on the Submit button Outlook acts like it's in design view (draws a rectangle with drag handles around the control). Once I actully sent the message it started to behave properly.

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Hi

Thanks, apologies, I realised that just after I've send the reply. It works fine, thx.

SmithcZA
 
Hi there

CautionMP, it is working fine this far, can you please send me the code to convert the responses to excell.

SmithcZA
 
smithcza,
Sure, I have a couple of questions for you.[ol]
[li]Will you be doing the conversion one email at a time, or as a batch?[/li]
[li]Do you want all the replies in one Excel worksheet that all new responses get added to?[/li]
[li]Do you want to run the routine from Outlook (push), or from Excel (pull)?[/li][/ol]

I have a bunch more but this is enough to get going in the right direction.

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
smithcza,
It looks like the week is starting to get busy so here is a starting point since I dln't know if I will get back to it this week. I wrote the output as a CSV file since it was easier and Excel will do the conversion automatically.

There are four functions, these are the two that actually process the messages and can be fired from a custom menu bar:[ul]
[li][tt]SingleSurveyExport()[/tt] which will import the highlighted message in a folder.[/li]
[li][tt]BatchSurveyExport()[/tt] to import all the items in the Inbox.[/li][/ul]
Main functions:
Code:
Sub SingleSurveyExport()
'This is the text of Subject line from the survey email
Const SurveySubject As String = "Survey Response"
'This is filename that the results are output to
Const OutputFileName As String = "C:\" & SurveySubject & ".csv"
Dim MyExplorer As Explorer
Dim MyMailItem As MailItem
Dim blnWriteHeader As Boolean
Dim strOutput As String

Set MyExplorer = ActiveExplorer
If MyExplorer.Selection.Item(1).Class = olMail Then
  Set MyMailItem = MyExplorer.Selection.Item(1)
  If MyMailItem.Subject = SurveySubject Then
    'Check to see if output file exists
    If Dir(OutputFileName) = "" Then
      blnWriteHeader = True
    End If
    If blnWriteHeader Then
      'The file did not exist so create the header row and file
      strOutput = Chr(34) & "SenderName" & Chr(34) & "," & _
                  Chr(34) & "SentOn" & Chr(34) & "," & _
                  ParseSurveyBody(MyMailItem.Body, True)
      AppendSurveyResults OutputFileName, strOutput
      blnWriteHeader = False
    End If
    'Now write the data from the email
    
    'The next two lines grab information from the email
    strOutput = Chr(34) & MyMailItem.SenderName & Chr(34) & ","
    strOutput = strOutput & Chr(34) & MyMailItem.SentOn & Chr(34) & ","
    'This calls the function that converts the body to a CSV record
    strOutput = strOutput & ParseSurveyBody(MyMailItem.Body, False)
    'This will output to the file
    AppendSurveyResults OutputFileName, strOutput
  End If
End If
Set MyMailItem = Nothing
Set MyExplorer = Nothing
End Sub

Public Sub BatchSurveyExport()
'This is the text of Subject line from the survey email
Const SurveySubject As String = "Survey Response"
'This is filename that the results are output to
Const OutputFileName As String = "C:\" & SurveySubject & ".csv"
Dim MyNameSpace As NameSpace
Dim MyInbox As MAPIFolder
Dim MyMailItem As MailItem
Dim blnWriteHeader As Boolean
Dim strOutput As String

'Check to see if output file exists
If Dir(OutputFileName) = "" Then
  blnWriteHeader = True
End If

Set MyNameSpace = GetNamespace("MAPI")
Set MyInbox = MyNameSpace.GetDefaultFolder(olFolderInbox)
For Each MyMailItem In MyInbox.Items
  If MyMailItem.Subject = SurveySubject Then
    If blnWriteHeader Then
      'The file did not exist so create the header row and file
      strOutput = Chr(34) & "SenderName" & Chr(34) & "," & _
                  Chr(34) & "SentOn" & Chr(34) & "," & _
                  ParseSurveyBody(MyMailItem.Body, True)
      AppendSurveyResults OutputFileName, strOutput
      blnWriteHeader = False
    End If
    'Now write the data from the email
    
    'The next two lines grab information from the email
    strOutput = Chr(34) & MyMailItem.SenderName & Chr(34) & ","
    strOutput = strOutput & Chr(34) & MyMailItem.SentOn & Chr(34) & ","
    'This calls the function that converts the body to a CSV record
    strOutput = strOutput & ParseSurveyBody(MyMailItem.Body, False)
    'This will output to the file
    AppendSurveyResults OutputFileName, strOutput
  End If
Next MyMailItem
Clean_Up:
Set MyMailItem = Nothing
Set MyInbox = Nothing
Set MyNameSpace = Nothing
End Sub

Support functions:
Code:
Public Function ParseSurveyBody(BodyText As String, GetHeader As Boolean) As String
Dim lngField As Long
Dim strFields() As String, strField() As String
Dim strOutput As String
'The text in the body is Carriage Return/Line Feed delimited so split
'to get each field
strFields = Split(BodyText, vbCrLf)
For lngField = 0 To UBound(strFields)
  'Make sure the field has data
  If strFields(lngField) <> "" Then
    'The field name and value are seperated by '=' so split
    strField = Split(strFields(lngField), "=")
    If GetHeader Then
      'Calling routine wants the header row 0
      strOutput = strOutput & Chr(34) & strField(0) & Chr(34) & ","
    Else
      strOutput = strOutput & Chr(34) & strField(1) & Chr(34) & ","
    End If
  Else
    'IF it's not the last field write a place holder
    If lngField <> UBound(strFields) Then
      strOutput = strOutput & Chr(34) & Chr(34) & ","
    End If
  End If
Next lngField
ParseSurveyBody = strOutput
End Function

Sub AppendSurveyResults(FullFileName As String, CSVRecordInfo As String)
Dim intOutputFile As Integer
intOutputFile = FreeFile
Open FullFileName For Append As #intOutputFile
Print #intOutputFile, CSVRecordInfo
Close #intOutputFile
End Sub

NOTE: If you are doing this as a batch import [tt]AppendSurveyResults()[/tt] will require a little extra overhead since it opens, writes, and then closes the output file for each matching item in the inbox, but it allows the flexibility of processing the messages one at a time.

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Thanks folks - one to try

I assume there is a

.From =

property.

I have numerous e-mails and change them when spammed. That way I know who is giving away the addresses or getting viruses. They never admit it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top