Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何从Microsoft Word粘贴到Outlook中_Vba_Ms Word_Outlook - Fatal编程技术网

Vba 如何从Microsoft Word粘贴到Outlook中

Vba 如何从Microsoft Word粘贴到Outlook中,vba,ms-word,outlook,Vba,Ms Word,Outlook,我想写一些VBA代码,它将自动从RTF文档创建新的电子邮件。我正在使用以下程序: 1.Microsoft Word 2013 2.Microsoft Outlook 2013 除了如何将我复制的内容粘贴到电子邮件正文中之外,我已经设法做了所有我想做的事情 我已经在网上搜索过如何做到这一点,但是我没有找到任何简单的方法。此外,我发现的所有示例都与Microsoft Excel相关。我注意到使用Microsoft Word时存在差异 下面是我编写的代码: Sub SendDocAsMail() D

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

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

我已经在网上搜索过如何做到这一点,但是我没有找到任何简单的方法。此外,我发现的所有示例都与Microsoft Excel相关。我注意到使用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
Sub SendDocAsMail()
Dim oOutlookApp作为Outlook.Application
将oItem设置为Outlook.MailItem
将用户设置为字符串
模糊主题为字符串
如细绳般的暗色ClientRef
暗体为弦
作为字符串的数字签名
字符串作为字符串
作为整数的Dim i
作为整数的Dim Pos
将myAttachments设置为Outlook。附件
TheUser=Environ(“用户名”)
出错时继续下一步
'如果Outlook未运行,则启动它
设置oOutlookApp=GetObject(,“Outlook.Application”)
如果错误为0,则
设置oOutlookApp=CreateObject(“Outlook.Application”)
如果结束
'创建新消息
设置oItem=oOutlookApp.CreateItem(olMailItem)
'将打开的文档复制到主题和正文
'仅将Mysig.htm更改为签名的名称
SigString=Environ(“appdata”)&_
“\Microsoft\Signatures\”&TheUser&.htm”
Selection.MoveDown单位:=wdLine,计数:=1,扩展:=wdExtend
主题=选择。文本
主题=左(主题,透镜(主题)-1)
ClientRef=主题
ClientRef=右(ClientRef,Len(ClientRef)-1)
对于i=1到Len(ClientRef)
如果Mid(ClientRef,i,1)=“|”,那么
Pos=i
如果结束
接下来我
ClientRef=左(ClientRef,位置-1)
选择。删除单位:=wdCharacter,计数:=1
选择。删除单位:=wdCharacter,计数:=1
Selection.EndKey单位:=wdStory
选择.类型段落
选择.类型段落
Selection.InsertFile(SigString)
健康的选择
选择,复制
oItem.To=”xxxx@xxxx.co.il; xxxx@xxxx.co.il"
oItem.BCC=”xxxx@xxxx.co.uk"
oItem.Subject=主题
'oItem.Body='需要帮助吗
'Selection.PasteAndFormat(WDFormatOriginalFormat)
oItem.显示器
设置myAttachments=oItem.Attachments
'myAttachments.Add.PathName=“C:\Users\”&TheUser&“\Dropbox\PATENT\Bressler\”&ClientRef&“
“清理
'Word.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Word.Application.Quit SaveChanges:=wdDoNotSaveChanges
端接头

如果您能帮助粘贴带有原始格式的复制文本,我们将不胜感激。

请掌握邮件项目的检查器,它有一个
.WordEditor
(基本上是MS Word文档实例)

这应该可以做到:

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
,这是一个word文档,因此您在word中使用的任何方法都应该可供
olItem.Inspector.WordEditor
使用

跟进:

我只需要使用
FileDialog
选择要附加的文件,如下所示:

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
或者,这可能更简单,也可能更成问题,有时将线程移交给另一个应用程序时就是这样:

olItem.GetInspector.CommandBars.ExecuteMSO "AttachFile"

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

获得邮件项目检查器的句柄,该检查器具有
.WordEditor
(基本上是MS Word文档实例)

这应该可以做到:

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
,这是一个word文档,因此您在word中使用的任何方法都应该可供
olItem.Inspector.WordEditor
使用

跟进:

我只需要使用
FileDialog
选择要附加的文件,如下所示:

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
或者,这可能更简单,也可能更成问题,有时将线程移交给另一个应用程序时就是这样:

olItem.GetInspector.CommandBars.ExecuteMSO "AttachFile"

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

非常感谢您给我如此快速的响应。我已经讨论这个问题三天了。你能不能给我一个快速的解释,为什么这是有效的,这样我就可以学习背后的代码的推理。此外,在我看来,您是提出以下问题的合适人选:我希望宏打开特定文件夹的“附件”对话框。我找不到合适的方法。什么是我实现上述目标的最佳方式?请参阅上面对我答案的修改:)非常感谢您的帮助!!!出于好奇,为什么文件对话框会工作,而不是使用附件对象中的内容?我寻找方法的第一个地方是Attachments对象。在我打算用错误的方式编码时,
FileDialog
方法使用了
olItem.Attachments
对象,并添加了每个附件——这显然是VBA代码的一部分。如果您依赖
.ExecuteMSO
方法,那么该方法也应该有效。它们都应该起作用。我更喜欢显式的解决方案,而不是将过程交给程序执行代码之外的东西。非常感谢您给我如此快速的响应。我已经讨论这个问题三天了。你能不能给我一个快速的解释,为什么这是如此有效