• inserting word doc text into an email (access 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » inserting word doc text into an email (access 2003)

    Author
    Topic
    #448446

    Using the code below to send an email from my db. Works great except my users would like the stBody string to be the text in a word document they update periodically on a local shared drive (drive =F). Is there a way to make the text from there word doc the string I’m looking for? Thanks..

    Dim stBody as String = Fdrive word Doc?

    With MailOutLook
    .To = Nz(srt, “”)
    ‘.To = SQL
    .Subject = stSubject
    .HTMLBody = StBody

    Viewing 0 reply threads
    Author
    Replies
    • #1096600

      Does the Word doc contain HTML code or formatted text?

      • #1096601

        Just a regular word doc hans. I would say just text.

        • #1096604

          Edited by HansV to correct mistake

          I would use Automation to start Word (instead of Outlook), open the document, then use the MailEnvelope property of the document to send it. Let’s say that objDoc is a Word.Document variable. After opening the document into this variable:

          With objDoc.MailEnvelope.Item
          .To = Nz(srt, “”)
          .Subject = strSubject
          .Send
          End With

          • #1096610

            So if my word doc is c:word.doc where would I call that in the code? Not sure if I’m doing this right…

            dim objDoc As String

            objDoc = “c:word.doc”

            With objDoc.MailEnvelope.Send
            .To = Nz(srt, “”)
            ‘.To = SQL
            .Subject = stSubject
            .HTMLBody = stBody

            • #1096614

              A Word document is not a string. You’d have to do something like this:
              – Set a reference to the Microsoft Word 11.0 Object Library
              – Code snippet:

              Sub SendIt()
              Dim objWord As Word.Application
              Dim objDoc As Word.Document
              Dim blnStart As Boolean


              On Error Resume Next
              Set objWord = GetObject(, “Word.Application”)
              If objWord Is Nothing Then
              Set objWord = CreateObject(“Word.Application”)
              If objWord Is Nothing Then
              MsgBox “Cannot start Word.”, vbExclamation
              Exit Sub
              End If
              blnStart = True
              End If
              On Error GoTo ErrHandler

              Set objDoc = objWord.Documents.Open(“C:Word.doc”)
              With objDoc
              .To = Nz(srt, “”)
              .Subject = strSQL
              .Send
              .Close SaveChanges:=False
              End With

              ExitHandler:
              On Error Resume Next
              objDoc.Close SaveChanges:=False
              Set objDoc = Nothing
              If blnStart Then
              objWord.Quit
              End If
              Set objWord = Nothing
              Exit Sub

              ErrHandler:
              MsgBox Err.Description, vbExclamation
              Resume ExitHandler
              End Sub

              As you see there is no line to set the body of the e-mail; the Word document is used as body.

            • #1096629

              Hans I created a sample db with the code on a button on a form and when I ran this is it gave an error saying object does not support this property or method . I set the referenece like you said but not luck. Can you take a look at this? Thanks…

            • #1096631

              Sorry, it was just air code, it contained a couple of errors.

              Change the line

              With objDoc

              to

              With objDoc.MailEnvelope.Item

              and remove the line

              .Close SaveChanges:=False

              (Leave the line objDoc.Close SaveChanges:=False, that is still needed)

              The code will still not run in the sample database that you attached since srt and strSQL have not been assigned a value. When you provide a valid e-mail address, the code should run.

            • #1096737

              I got it working great. Thanks!! One question is there a way we can have this not automatically send the email. Is there a way they can preview the email and send it themselves.? Thanks

            • #1096739

              If you change the line

              .Send

              to

              .Display

              the e-mail will be displayed on-screen; the user can edit it if necessary, then send it (or not).

            • #1096746

              I tired that before I posted and it just hangs now. Can you take a look? Thanks..

            • #1096756

              You’re correct, .Display doesn’t work. You could try the attached version, but I think that has undesirable side effects. As you will see, the To address and Subject are not filled in, and the document remains open after the e-mail has been sent off.

              It’s probably better – and easier – to store the text of the letter in a table in Access.

            • #1096998

              Hey Hans.

              I got this to work in my production db but I did have another question on this sample db. I added a combo box to with a yes or no drop down. If the user select yes I want it to use word.doc on my c:temp folder. If they select no I want it to grab word1.doc for the c:temp folder. Can this be done? Thanks..

            • #1097008

              You can use code like this:

              If Me.txt = "yes" Then
              strDoc = "C:tempWord.doc"
              Else
              strDoc = "C:tempWord1.doc"
              End If
              Set objDoc = objWord.Documents.Open(strDoc)

              The value of strDoc is set depending on the choice in the combo box txt (off choice of a name for a combo box, BTW), and this is used as the name of the document to open.

    Viewing 0 reply threads
    Reply To: inserting word doc text into an email (access 2003)

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

    Your information: