【发布时间】:2014-05-10 16:39:03
【问题描述】:
我创建了一个将 Outlook 邮件复制到 Excel 工作表的 Outlook 宏。
当目标工作簿已经打开时,宏不会给出预期的结果。我想关闭已经打开的工作簿。
我知道如何使用 Excel VBA 做到这一点,但如何使用 Outlook VBA 来处理。
我正在使用以下代码检查 Excel 工作表是否打开。
请注意,我想使用 Outlook VBA 关闭打开的工作簿。
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
更新 - 1(我用来打开和填充工作簿的代码)
Dim xlWB As Object
Dim xlSheet As Object
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.workbooks.Open(xlPath)
Set xlSheet = xlWB.sheets("output")
NextRow = xlSheet.Range("A" & xlApp.Rows.Count).End(3).Row + 1
With xlSheet
.cells(NextRow, "A") = Item.Subject
.cells(NextRow, "B") = Item.ReceivedTime
.cells(NextRow, "C") = xAsset
.cells(NextRow, "D") = Item.SenderName
.cells(NextRow, "E") = Item.SenderEmailAddress
End With
xlWB.Save
xlWB.Close SaveChanges:=True
xlApp.Quit
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
更新 - 2(解决方案)
Dim wb As Object
Set wb = GetObject("C:\book1.xlsx")
If Not wb is Nothing then wb.close
【问题讨论】:
-
显示填充工作簿的代码会很有帮助。
-
@TimWilliams - 我添加了用于填充工作簿的代码。我希望这将帮助您获得解决方案。谢谢。