Results 1 to 14 of 14
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    inserting word doc text into an email (access 2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    Does the Word doc contain HTML code or formatted text?

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: inserting word doc text into an email (access 2003)

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

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: inserting word doc text into an email (access 2003)

    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

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    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.

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: inserting word doc text into an email (access 2003)

    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...
    Attached Files Attached Files

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    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.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    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

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: inserting word doc text into an email (access 2003)

    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

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    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).

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: inserting word doc text into an email (access 2003)

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

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    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.
    Attached Files Attached Files

  13. #13
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: inserting word doc text into an email (access 2003)

    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..
    Attached Files Attached Files

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: inserting word doc text into an email (access 2003)

    You can use code like this:
    <code>
    If Me.txt = "yes" Then
    strDoc = "C:tempWord.doc"
    Else
    strDoc = "C:tempWord1.doc"
    End If
    Set objDoc = objWord.Documents.Open(strDoc)
    </code>
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •