• Recurring e-mail message (Outlook 2002 )

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Recurring e-mail message (Outlook 2002 )

    • This topic has 17 replies, 7 voices, and was last updated 23 years ago.
    Author
    Topic
    #368374

    I am using the following code from MS’s website. How to Create a Recurring E-mail Message. Where I type the text for “NewItem.Body” I want to be able to type more than one paragraph, so I need to know how to indicate a hard return, if possible. Also, I want the recurring e-mail to have the signature that automatically inserted on new messages. Is there a way to do those two additional things? TIA.

    Sub Item_PropertyChange (ByVal Name)
    Select Case Name
    Case “Status”
    if Item.Status = 2 then ‘2 = Completed
    Set NewItem = Application.CreateItem(O)
    NewItem.To = “myemailaddress@myisp.com”
    NewItem.Bcc = “bccemailaddress@theirisp.com”
    NewItem.Recipients.ResolveAll
    NewItem.Subject = “This is the message subject text.”
    NewItem.Body = “This is text that will appear in the body of the message.”
    NewItem.Display
    End IF
    End Select
    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #576985

      The vb constant for a paragraph break is vbCrLf, and you can & that in to your strings without ” marks.

      Unfortunately, when you assign a string to the .Body, it completely replaces what was there before. Thus, I think you have to add the signature into your string assignment. I haven’t looked at the Outlook object model to see how you would access a saved signature, but if it’s just as easy to add the signature text into your code, that probably would be just as fast.

      • #576986

        Can you give me an example of how I would use vbCrLf and the &’s? I’m extremely new, but wanting to learn!

        Thanks!

        • #576990
          Sub Item_PropertyChange (ByVal Name)
          dim strMsg as String
          
              strMsg = "Here is the first paragraph of my message"
              strMsg = strMsg &  vbCrLf  & "Here is the next paragraph"
              strMsg = strMsg & vbCrLf & vbCrLf & "And here is the signature"
          
              Select Case Name
                  Case "Status"
                      if Item.Status = 2 then '2 = Completed
                          Set NewItem = Application.CreateItem(O)
                          NewItem.To = "myemailaddress@myisp.com"
                          NewItem.Bcc = "bccemailaddress@theirisp.com"
                          NewItem.Recipients.ResolveAll
                          NewItem.Subject = "This is the message subject text."
                          NewItem.Body = strMsg
                          NewItem.Display
                      End If
                  End Select
          End Sub
          
          • #577108

            I’ve seen this method of building up in strings in VB howto guides – wonder if it dates to versions prior to the introduction of the line continuation character. Doing it like this:

            Dim strMsg As String
            strMsg = "Here is the first paragraph of my messages" _
                    & vbCrLf & "Here is the next paragraph" _
                    & vbCrLf & vbCrLf & "And here is the signature"
            

            seems to me to have two advantages:
            1) it’s a bit quicker to type since you don’t have to repeat “strMsg” each time
            2) the code is more efficient/requires less overhead since you’re only assigning a value to strMsg once.

            Gary

            • #577135

              Hi Gary,
              For shorter strings, the line continuation character is my preference but there is a limit as to how many of those you can use. If I’m building complex SQL statements in code, I tend to use Stuart’s method of strSQL = strSQL & “…”
              FWIW.

            • #577138

              hello Gary,

              You missed
              3) The code is much easier to read.

              I used this format when I was adding real paragraphs to a letter, I tend to use continuation lines within each paragraph and add paragraphs together with the syntax I showed.

              StuartR

            • #577174

              I use it whenever possible, but I believe it is actually less efficient than the individual string assignments, although I can’t remember where I read and explanation that supported that claim. Seems like it was something about each line continuation forcing the entire previous string to be reevaluated or rereferenced or something …

            • #577376

              The article Efficiently Reading Large Text Files on the 4GuysFromRolla ASP site reported some results of different ways of making strings using the FileSystemObject. They found that concatenation was quite slow compared with other methods tested, perhaps because Windows allocates new memory and copies the earlier string each time? Assuming that the same thing would happen here, then the line continuation method should be faster, although with 5 lines, I assume it’s an unmeasurable advantage.

              If anyone needs to quickly scan text files in excess of 100Kb using VBA or VBScript, I recommend the above article. Is Chris Greaves listening?

            • #577501

              Hi All,

              I’ve used a very similar method to create a recurring email where the bulk of the text was a constant but I wanted to add paragraphs at the end of the email that changed.

              To do this, I created a text file and typed in the additional paragraphs that I wanted to add. Then opened the text file and read the file in (cannot remember if it was one big string or lots of little ones that I concatenated together) using VB.

              Then assigned this string (the standard stuff plus the variable stuff from the text file) to the body of the email. Worked like a dream.

            • #577402

              My recollected reading (can’t remember specifically where I read it either) was that VB is inefficient with the repeated assignments of a value to “strMsg” – something about a new memory address being allocated for each assignment. What you’re recollecting also sounds likely (that internally the line continuation character is forcing some kind of reevaluation or reassignment). May be six of one half dozen of the other….

          • #577257

            Thank you!

            If I wanted a blank line would I use this?

            strMsg = strMsg & vbCrLf & ” ”

            Or is there a better way?

            • #577280

              The constant vbCrLf gets its name from the old typewriter action of “Carriage Return” followed by a “Line Feed”

              Insert one vbCrLf to get a newline
              Insert two in a row to get a blank line

              strMsg = strMsg & vbCrLf & vbCrLf
              

              StuartR

            • #577292

              Thanks for the help. I tried typing the code and am getting a script error. It reads [indent]


              Expected end of statement Line No: 2


              [/indent]

              I tried copying and pasting in the code and am getting the same message. Any suggestions?

              Sub Item_PropertyChange (ByVal Name)
              dim strMsg as String

              strMsg = “Here is the first paragraph of my message”
              strMsg = strMsg & vbCrLf & “Here is the next paragraph”
              strMsg = strMsg & vbCrLf & vbCrLf & “And here is the signature”

              Select Case Name
              Case “Status”
              if Item.Status = 2 then ‘2 = Completed
              Set NewItem = Application.CreateItem(O)
              NewItem.To = “solberg@firesideop.com”
              NewItem.Bcc = “dacksolberg@firesideop.com”
              NewItem.Recipients.ResolveAll
              NewItem.Subject = “This is the message subject text.”
              NewItem.Body = strMsg
              NewItem.Display
              End If
              End Select
              End Sub

            • #577305

              I’d never looked at the VB Script for these forms before, it seems to be a strange sub set of Visual Basic.

              Just replace

              Dim strMsg as String
              

              with

              Dim strMsg
              

              and it will work as advertised

              StuartR

            • #577372

              That’s right, in VBScript, all variables (even objects) are variants and cannot be dimensioned as a particular type. Hard to undo the habit when posting sample code here!

            • #578869

              Got it working; thanks for the help!

              What makes the two lines different?

            • #578942

              > What makes the two lines different?

              Dim strMsg tells Visual Basic that you will be using a variable called strMsg but doesn’t say anything about the sort of thing you will store in the variable.

              Dim strMsg as String tells Visual Basic that you will be using a variable called strMsg and that this variable will only be used to store Strings (a string is a sequence of text characters). This syntax is usually better because it enables VB to help you find mistakes and errors more easily and earlier. This works in Visual Basic (VB) and Visual Basic for Applications (VBA) but the outlook form that you are using uses VBScript, which only supports the other form of the Dim.

              StuartR

    Viewing 0 reply threads
    Reply To: Recurring e-mail message (Outlook 2002 )

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: