Consulting

Results 1 to 6 of 6

Thread: Copy (with Format) from Word, Paste in Outlook

  1. #1
    VBAX Newbie
    Joined
    Jun 2008
    Posts
    5
    Location

    Copy (with Format) from Word, Paste in Outlook

    Hi
    From Access, I am opening a Word file, finding and replacing words, copying the contents of it (Select All) and pasting it in New Message in Outlook, however, I want the format to be pasted as well.

    I posted this msg earlier but it seems its died down, so I have to post it again. Here is the code

    [vba]
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Set wordApp = CreateObject("Word.Application")
    With wordApp
    .Visible = False
    Set wordDoc = .Documents.Open("filename")
    End With

    Dim wordSelection As Selection
    Set wordSelection = wordApp.Selection

    With wordSelection.Find
    .Text = "SOMETHING"
    .Forward = True
    .MatchWholeWord = True
    .Replacement.Text = Trim(txtSomething.Value)
    .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
    End With

    ' I go on replacing more texts

    wordDoc.Select

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem

    Set objOutlook = CreateObject("Outlook.Application")

    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg
    .Subject = "Subject"
    .BodyFormat = olFormatRichText
    .Body = wordSelection.Text
    .Display
    End With

    Set objOutlook = Nothing

    'closes the word document, does not save changes and does not prompt to save changes
    wordDoc.Close (False)
    wordApp.Quit ' close the Word application
    Set wordDoc = Nothing
    Set wordApp = Nothing

    [/vba]

    In objOutlookMsg.Body using both wordSelection.Text or just wordSelection pastes the contents but not the format.

    I tried using objOutlookMsg.Body = wordSelection.PasteFormat, after using wordSelection.copy above but the PasteFormat gives an error.

    I would really appreciate some help.

  2. #2
    Instead of wordSelection.Text try wordSelection.Range.FormattedText.

    Regards

  3. #3
    VBAX Newbie
    Joined
    Jun 2008
    Posts
    5
    Location
    Hi Nelvicus,

    wordSelection.Range.FormattedText does paste the contents in Outlook BUT does NOT apply the format that is in the Word File. Thanks for trying.

  4. #4
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,675
    Location
    Instead of forcing the document into Outlook, have you considered e-mailing the document via Word itself?

    I know that:[vba]Options.SendMailAttach = False
    ActiveDocument.SendMail[/vba]
    Will open a message window for sending the specified document through Microsoft Exchange with the document text as the e-mai body text (instead of as an attachment).

    Could this solve your problem?
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  5. #5
    Hmm, well I've done a bit of playing around without success - the problem is really an Outlook one, i.e. "how do I programatically insert formatted content into an Outlook message". I found a thread and FAQ on Tek-Tips where someone achieves this by creating the Outlook mail directly from the Word doc using 'wordDoc.MailEnvelope.Item', although I couldn't get this to work in Office 2007.

    You can read the FAQ here: http://www.tek-tips.com/faqs.cfm?fid=6950

    Regards

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi vbToto,

    What you want is possible but it depends which version of Outlook you're working with.

    In Office xp SP3 the Outlook object guard was introduced and a lot of code is threaded as dangerous eversince. (outlook will throw a msg that a program is trying to...)

    If your version is before the above version you can try to get a handle to the ActiveInspector of outlook and check it it is the WordEditor. After this get a handle to that inspector and threat it like a normal Word document.

    Something like:
    [vba]
    Sub FillOutlookBody()
    ActiveDocument.Content.Copy
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objInspector As Outlook.Inspector
    Dim objDoc As Word.Document
    Set objOutlook = CreateObject("Outlook.Application")

    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    With objOutlookMsg
    .Subject = "Subject"
    .BodyFormat = olFormatHTML
    .Display
    End With
    Set objInspector = objOutlook.ActiveInspector
    If Not objInspector Is Nothing And objInspector.EditorType = olEditorWord Then
    Set objDoc = objInspector.WordEditor
    objDoc.Range.Paste
    End If
    Set objDoc = Nothing
    Set objOutlookMsg = Nothing
    Set objInspector = Nothing
    Set objOutlook = Nothing
    End Sub
    [/vba]

    So perhaps this will work for you.

    To recap the tip CreganTur gave you (SendMailAttach) is a well know trick to do this. Offcourse it's not as full featured as using the full Outlook object model but if it's enough for you I'd go for it.

    HTH
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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