【问题标题】:VBA keeps trying to open Worksheet, but don't want it toVBA 不断尝试打开工作表,但不希望它打开
【发布时间】:2017-10-07 16:59:54
【问题描述】:

我有一个 VBA,可以将电子邮件从 Outlook 写入 Excel。但是,我希望此 Excel 表保持打开状态。目前我已经让工作表保持打开状态(并且只是在电子邮件进入后保存),但是每次我在我的工作簿中收到一封新电子邮件时,它都会要求我重新打开工作簿,因为 VBA 是告诉它打开工作簿。

代码如下:

Sub ExportToExcel(MyMail As MailItem)

    Dim strID As String, olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim strFileName As String

    '~~> Excel Variables
    Dim oXLApp As Object, oXLwb As Object, oXLws As Object
    Dim lRow As Long, fRow As Long

    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)

    '~~> Establish an EXCEL application object
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")

    '~~> If not found then create new instance
    If Err.Number <> 0 Then
        Set oXLApp = CreateObject("Excel.Application")
    End If
    Err.Clear
    On Error GoTo 0

    '~~> Show Excel
    oXLApp.Visible = True

    '~~> Open the relevant file
    Set oXLwb = oXLApp.Workbooks.Open("\\C:\Rachael\VBAs\Control Panels.xlsm")

    '~~> Set the relevant output sheet. Change as applicable
    Set oXLws = oXLwb.Sheets("Ash Data")

    '~~> Write to outlook
    With oXLws
        '~~> Code here to output data from email to Excel File
        '~~> For example

        '* insert into last row (old alternative)
        '* you can remove this and the declare of lRow (at the top) if you don't need the old last row insert anymore.
        'lRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1 'next new row
        '.Range("A" & lRow).Value = olMail.Body 'write into last row

        '* insert into first row
        fRow = 1 'first row
        .Rows(fRow).Insert Shift:=xlDown
        .Range("A" & fRow).Value = olMail.Body 'write into first row
    End With

    '~~> Close and Clean up Excel
    oXLwb.Save
    Set oXLws = Nothing
    Set oXLwb = Nothing
    Set oXLApp = Nothing

    Set olMail = Nothing
    Set olNS = Nothing

End Sub

我不确定这段代码哪里出了问题,但也许有人知道解决这个问题的方法?

【问题讨论】:

  • 下次您使用我们其中一位用户的代码时,至少要提及它并向我们发送该帖子的链接。无论如何,“它要求我重新打开工作簿”,因为您告诉它:Set oXLwb = oXLApp.Workbooks.Open("\\C:\Rachael\VBAs\Control Panels.xlsm")
  • 嗨,我认为这是我使用的原件:stackoverflow.com/questions/11876549/… 但我如何更改该行而不是打开它,它只会寻找它,因为这张表将始终保持打开状态?我尝试删除 .open 但它给了我一个错误?
  • 在下面看我的答案,看看它对你有用

标签: excel vba outlook


【解决方案1】:

或者,您可以使用其他方法检查您的工作簿是否已经打开,没有错误陷阱。您可以循环浏览打开的 Excel 工作簿,并将它们与您正在寻找的 FullName ("\\C:\Rachael\VBAs\Control Panels.xlsm") 进行比较。

如果有匹配项 >> 则 Set oXLwb 到该工作簿。

如果不匹配>>然后Open相关工作簿。

代码

Dim oXLApp As Object, oXLwb As Object, oXLws As Object
Dim XLopenWB As Object
Dim Flag As Boolean

Flag = False
For Each XLopenWB In oXLApp.Workbooks
    If XLopenWB.FullName Like "\\C:\Rachael\VBAs\Control Panels.xlsm" Then
        Flag = True
        Set oXLwb = XLopenWB
        Exit For
    End If
Next XLopenWB

If Not Flag Then
    ' open the relevant workbook
    Set oXLwb = oXLApp.Workbooks.Open("\\C:\Rachael\VBAs\Control Panels.xlsm")
End If

【讨论】:

  • 谢谢 Shai,我不太擅长代码方面的东西,我这周刚被介绍给它,很抱歉,如果这听起来很愚蠢,但是,该代码是否高于“打开相关工作簿”?还是这会进入一个全新的vba?谢谢!
  • @Rachael 你应该删除行 '~~&gt; Open the relevant fileSet oXLwb = oXLApp.Workbooks.Open("\\C:\Rachael\VBAs\Control Panels.xlsm") ,而是粘贴我的代码
  • 感谢 Shai,这太棒了,非常感谢你们!
  • @Rachael 您只能将 1 个答案标记为“ANSWER”,因此请明智地选择;)任何一种方式都很酷。您可以支持其他答案
  • 我知道!内脏,我只是试图点击你们两个,但无法:(我要去 eenie-meenie-miney-mo 它!
【解决方案2】:

正如@Shai 在 cmets 中所提到的,问题是您的宏在每次运行时都会打开 Workbook,而不管 Workbook 是否已经打开。 Siddharth Rout 对this 问题的回答提供了一个IsWorkBookOpen 函数,该函数将检查工作簿是否打开,如果返回False,您可以打开工作簿:

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

然后您可以将代码更改为以下内容:

'~~> Open the relevant file
If IsWorkBookOpen("\\C:\Rachael\VBAs\Control Panels.xlsm") Then
    Set oXLwb = oXLApp.Workbooks("Control Panels.xlsm")
Else
    Set oXLwb = oXLApp.Workbooks.Open("\\C:\Rachael\VBAs\Control Panels.xlsm")
End If

【讨论】:

  • 嗨,乔丹,谢谢。我已经在“打开相关文件”下更改了我的代码,但是我不确定将您提供的最高代码“函数 IsWorkBookOpen(FileName As String)”放在哪里
  • 你可以把它放在 Sub 之前或之后(例如,在 Sub ExportToExcel(MyMail As MailItem) 行之前或 End Sub 之后)
  • 谢谢乔丹,这对我来说非常有用!不能感谢你们俩!
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多