How to open an existing Excel file?

 ̄綄美尐妖づ 提交于 2019-12-24 18:29:57

问题


I am copying a table from Outlook to Excel. The code I have found online copies the table in a new Excel file.

I want to copy the table into an existing Excel file.

Here is the code I am running in 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:


The code here

Set wkb = xlApp.Workbooks.Add

is what opens the new workbook. Try replacing this line with something like

Set wkb = xlApp.Workbooks.Open("C:\PathToExcel\File.xlsx")  


来源:https://stackoverflow.com/questions/50607265/how-to-open-an-existing-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!