如何打开现有的Excel文件?

时间:2018-05-30 14:28:49

标签: excel-vba outlook outlook-vba vba excel

我正在将表格从Outlook复制到Excel。我在网上找到的代码将表格复制到一个新的Excel文件中。

我想将表复制到现有的Excel文件中。

这是我在Outlook中运行的代码。

Sub dd()
Dim item As MailItem, x%
Dim r As Object  'As Word.Range
Dim doc As Object 'As Word.Document
Dim xlApp As Object, wkb As Object
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True

Dim wks As Object
Set wks = wkb.Sheets(1)

For Each item In Application.ActiveExplorer.Selection
    Set doc = item.GetInspector.WordEditor
    For x = 1 To doc.Tables.Count
        Set r = doc.Tables(x)
        r.Range.Copy
        wks.Paste
        wks.Cells(wks.Rows.Count, 1).End(3).Offset(1).Select
    Next
Next
End Sub

1 个答案:

答案 0 :(得分:0)

这里的代码

Set wkb = xlApp.Workbooks.Add

是打开新工作簿的原因。尝试用

之类的东西替换这一行
Set wkb = xlApp.Workbooks.Open("C:\PathToExcel\File.xlsx")