在Outlook VBA中将范围设置为当前位置

时间:2014-11-12 15:58:26

标签: outlook-vba

我有一个将表插入活动电子邮件的宏。现在我的问题是,我无法弄清楚如何添加用户在运行宏时键入的表。我想我必须定义一个范围,我已经研究了崩溃方法,但我被卡住了。

这是我的代码:

sub insertmytable() <br>
Dim oRng As Object <br> 
Dim wdDoc As Object<br>
  If TypeName(ActiveWindow) = "Inspector" Then<br>
   If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then<br>
    Set wdDoc = ActiveInspector.WordEditor<br>
    With wdDoc<br>
     Set oRng = wdDoc.Range<br>
     oRng.collapse<br>
     .tables.Add Range:=oRng, numrows:=2, numcolumns:=5, defaulttablebehavior:=1, autofitbehavior:=0<br>
    end with<br>
   end if<br>
 end if<br>
end sub<br>

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

尝试使用.InsertBefore

http://msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx

Sub PasteAtInsertionPoint()

Dim objOL As Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection

Dim strText As String

Set objOL = Outlook.Application
Set objDoc = objOL.ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection

strText = " Test"
objSel.InsertBefore strText

End Sub

编辑2014 11 17

Sub insertmytable()

Dim oRng As Object
Dim wdDoc As Object

Dim objSel As Word.Selection

If TypeName(ActiveWindow) = "Inspector" Then

    If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then

        Set wdDoc = ActiveInspector.WordEditor
        Set objSel = wdDoc.Windows(1).Selection
        Set oRng = objSel.Range
        oRng.Tables.Add Range:=oRng, NumRows:=2, NumColumns:=5, DefaultTableBehavior:=1, AutoFitBehavior:=0

    End If

End If

End Sub