2015-05-05 89 views
0

更新
我创建了一个数组来循环访问某些行以获取数据。然后我想插入到我已经在Outlook模板中创建的表中。从Excel中将数据插入到Outlook中的表中

VBA代码

Sub employeeArray() 

    Dim managerEmployees() As Variant 
    Dim r As Long, c As Long 
    Dim objWord 
    Dim objDoc 
    Dim objRange 
    Dim objTable 
    useractivity.Activate 

    r = sheet1.ListObjects("Table1").ListRows.Count 
    c = 8 

    ReDim managerEmployees(1 To r, 1 To c) 

    Set objWord = CreateObject("Word.Application") 
    objWord.Visible = True 
    Set objDoc = objWord.Documents.Add 
    Set objRange = objDoc.Range 
    Set objTable = objDoc.Tables.Add(objRange, r, c) 

    For r = LBound(managerEmployees, 1) To UBound(managerEmployees, 1) 
     For c = LBound(managerEmployees, 2) To UBound(managerEmployees, 2) 
     managerEmployees(r, c) = Range("A2").Offset(r, c).Value 
     Next c 
    Next r 


    For r = LBound(managerEmployees, 1) To UBound(managerEmployees, 1) 
     For c = LBound(managerEmployees, 2) To UBound(managerEmployees, 2) 
     objTable.Cell(r, c).Range.Text = managerEmployees(r, c) 
     Next c 
    Next r 

    Erase managerEmployees 

    End Sub 

实例 - Outlook模板

enter image description here

实例 - Excel工作表名称为sheet1和表名为Table

Table of data

目前,我试图在Word中打开表(只是因为我不知道如何做到这一点的前景,还)

的阵列从表保存的数据,然后创建一个在Word中的表格来粘贴数据。这是现在工作,虽然我现在需要得到这个在Outlook而不是字

有没有人有任何建议吗?

+0

您需要我们提供一个简单的例子。 –

+0

我为您添加了一个示例 – StackUser2014

+0

您在哪里存储您的信息? Excel中?字?文本? –

回答

0

完成工作最简单的方法是使用Word对象模型来修改邮件正文。

Outlook对象模型提供了与项目机构合作三种不同的方式:

  1. 身体 - 一个纯文本。
  2. HTMLBody - 一个HTML标记。
  3. Word编辑器。 Outlook使用Word作为电子邮件编辑器,因此您可以使用它来格式化电子邮件。 Inspector类的WordEditor属性返回表示消息正文的Document类的实例。

您可以在MSDN的Chapter 17: Working with Item Bodies中阅读有关所有这些方法的更多信息。

例如:

mail.GetInspector().WordEditor 
0

请务必前往Tools > References > Microsoft Excel 14.0库对象,包括它(14.0您的Excel版本煤矿是在2010年。)。

一旦做完这些后,就可以进行如下:

Sub GetInfo() 
'Define an Excel object 
Dim ExcelApp As New Excel.Application 
'Define objects 
Dim WB As Workbook 
Dim WS As Worksheet 
Dim r As Object 
Dim L As Long 
'Visible = false meaning Excel will be running invisible 
ExcelApp.Visible = False 
Set WB = ExcelApp.Workbooks.Open("Path to Excel file") 
Set WS = WB.Worksheets(1) '1 is the first sheet 
L = WS.Cells(WS.Rows.Count, 1).End(-4162).Row 'finding the lastrow of data considering (1 means column A of your table. If A (1) is not in your table, change it to whatever letter) 
Set r = WS.Range("A1:C" & L) 'Adjust it to the columns of your data 
Dim Table As Variant 'Table is 1-based 
Table = r.Value 
'Quit Excel 
WB.Close 
ExcelApp.Quit 

'Outlook goes go here 

End Sub