如何从Microsoft Word粘贴到Outlook中 [英] How to paste into Outlook from Microsoft Word

查看:677
本文介绍了如何从Microsoft Word粘贴到Outlook中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一些VBA代码,这些代码将根据RTF文档自动创建新电子邮件.我正在使用以下程序: 1. Microsoft Word 2013 2. Microsoft Outlook 2013

I want to write some VBA code that will automatically create a new email from an RTF document. I'm using the following programs: 1. Microsoft Word 2013 2. Microsoft Outlook 2013

除了如何将复制的内容粘贴到电子邮件正文之外,我已经设法完成了所有想要做的事情.

I have managed to do everything I want except how to paste the content that I copied into the body of the email.

我已经在网上搜索了如何执行此操作,但是我没有找到任何简单的方法来执行此操作.另外,我发现的所有示例都与Microsoft Excel有关.我注意到使用Microsoft Word会有区别.

I have searched all over the web for how to do this however I have not found any simple way of doing this. In addition, all of the examples that I have found were related to Microsoft Excel. I have noticed that there is a difference when using Microsoft Word.

下面是我编写的代码:

Sub SendDocAsMail()

Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim TheUser As String
Dim Subject As String
Dim ClientRef As String
Dim Body As String
Dim Signature As String
Dim SigString As String
Dim i As Integer
Dim Pos As Integer
Dim myAttachments As Outlook.Attachments

TheUser = Environ("UserName")

On Error Resume Next

'Start Outlook if it isn't running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
    Set oOutlookApp = CreateObject("Outlook.Application")
End If

'Create a new message
Set oItem = oOutlookApp.CreateItem(olMailItem)

'Copy the open document to subject and body

'Change only Mysig.htm to the name of your signature
    SigString = Environ("appdata") & _
                "\Microsoft\Signatures\" & TheUser & ".htm"

Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Subject = Selection.Text
Subject = Left(Subject, Len(Subject) - 1)
ClientRef = Subject
ClientRef = Right(ClientRef, Len(ClientRef) - 1)
For i = 1 To Len(ClientRef)
    If Mid(ClientRef, i, 1) = "|" Then
        Pos = i
    End If
Next i
ClientRef = Left(ClientRef, Pos - 1)

Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Selection.InsertFile (SigString)

Selection.WholeStory
Selection.Copy

oItem.To = "xxxx@xxxx.co.il; xxxx@xxxx.co.il"
oItem.BCC = "xxxx@xxxx.co.uk"
oItem.Subject = Subject
'oItem.Body = 'NEED HELP
'Selection.PasteAndFormat (wdFormatOriginalFormatting)

oItem.Display

Set myAttachments = oItem.Attachments
'myAttachments.Add.PathName = "C:\Users\" & TheUser & "\Dropbox\PATENT\Bressler\" & ClientRef & "\"
'Clean up
'    Word.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'    Word.Application.Quit SaveChanges:=wdDoNotSaveChanges


End Sub

对于以原始格式粘贴复制文本的所有帮助,将不胜感激.

All help in pasting the copy text with the original formatting would be greatly appreciated.

推荐答案

获取MailItem的检查器上的句柄,该检查器具有.WordEditor(基本上是MS Word文档实例)

Get a handle on the MailItem's Inspector which has a .WordEditor (basically MS Word Document instance)

https://msdn.microsoft.com/en-us/library/office/ff868098.aspx

这应该可以解决问题:

oItem.To = "xxxx@bxxxx.co.il; xxxx@xxxx.co.il"
oItem.BCC = "xxxx@docs.xxxx.co.uk"
oItem.Subject = Subject
'oItem.Body = 'NEED HELP

Dim mailWord as Object 'WordEditor
oItem.Display
Set mailWord = oItem.GetInspector.WordEditor
mailWord.Range(0).PasteAndFormat (wdFormatOriginalFormatting)

说明

解释很简单.为了使用类似PasteSpecial的方法,您需要使用具有该方法可用的对象. MailItem类不直接具有该类,但确实包含Inspector.WordEditor,它是一个单词Document,因此您在word中使用的任何方法都应该对olItem.Inspector.WordEditor可用.

The explanation is fairly simple. In order to use a method like PasteSpecial you need to be working with an object that has that method available. The MailItem class does not directly have this, but it does contain the Inspector.WordEditor which is a word Document -- so any method you would use in word should be available to the olItem.Inspector.WordEditor.

跟进:

我只会使用FileDialog选择要附加的文件,就像这样:

I would just use the FileDialog to select files to attach, like so:

Dim filePicker As FileDialog
Dim fileName As Variant

Set filePicker = Application.FileDialog(msoFileDialogFilePicker)
filePicker.AllowMultiSelect = True

'### specify the folder default for the fileDialog object
filePicker.InitialFileName = "C:\Path\to\your\folder\" 
filePicker.Show

For Each fileName In filePicker.SelectedItems
    oItem.Attachments.Add (fileName)
Next

或者,这可能更简单或更麻烦,如将线程移交给另一个应用程序时的情况:

Alternatively, and this may be simpler or it may be more problematic as is sometimes the case when you hand the thread over to another application:

olItem.GetInspector.CommandBars.ExecuteMSO "AttachFile"

我更喜欢FileDialog方法,因为它可以让您更好地控制结果选择.

I would prefer the FileDialog method just because it gives you more control over the resulting selections.

这篇关于如何从Microsoft Word粘贴到Outlook中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆