1
我在嘗試學習VBA,並試圖將其解析出來,並將該數組的元素保存到excel文件中。我這樣做,我的問題是,我似乎無法得到它更新文件中已有的內容。它只是重寫新的電子郵件信息並保存。 我的問題是:我的代碼需要什麼才能繼續寫入我的Excel文件,而無需擺脫已有內容。使用outlook vba更新現有的excel文件
Sub FMK(Item As Outlook.MailItem)
Const PathName = "C:\Users\carter\Desktop\fmk.xlsx"
Dim arrLines As Variant, _
varLines As Variant, _
RowNext As Integer, _
xlApp As Excel.Application, _
ExcelWkBk As Excel.Workbook, _
excWkb As Object, _
excWks As Object, _
temp As String
arrLines = Split(Item.Body, vbCrLf)
Set xlApp = Application.CreateObject("Excel.Application")
Set excWkb = xlApp.Workbooks.Add()
Set excWks = excWkb.ActiveSheet
temp = Trim(arrLines(32))
RowNext = 1
With excWks
excWks.Cells(RowNext, 1) = temp
End With
RowNext = RowNext + 1
excWkb.SaveAs PathName
excWkb.Close
'this is just for testing purposes I will remove later
MsgBox (arrLines(32))
End Sub
謝謝你的時間!
您「保存爲」即覆蓋現有文件(如果存在)。如果你想添加一些東西到現有的文件,你必須先打開它。 –
哦。好的,讓我試試看。謝謝! – user1462038
而不是'set excWkb = xlApp.Workbooks.Add()',嘗試'設置excWkb = xlApp.Workbooks.open(pathName)'。您目前正在創建一個新的工作簿,而不是打開現有的工作簿。您很可能需要更新目標表單。 – nutsch