打开第二个Excel

时间:2018-03-06 10:58:18

标签: vba excel-vba excel

经过数小时的研究后,我正在寻求帮助。 我的问题有点棘手,但...... 我正在为我的公司开发新的Excel-VBA应用程序。 我开发了一些代码来帮助我每次打开新工作簿时打开Excel的新实例。

当我的工作簿已经打开时,我尝试打开第二个Excel时出现问题。我得到一个不可理解的错误6溢出。

我开发了一些工具来打开新的Excel实例。

Option Explicit

Private WithEvents ThisExcelApplication As Application
Private WithEvents OtherExcelApplication As Application
Private l_blnSaveNewWorkbook As Boolean


'Procédure : Workbook_Open
'Event : opening workbook
Public Sub Workbook_Open()
    Debug.Print Time & " : ThisWorkbook\Workbook_Open()"
    If Application.Workbooks.Count > 1 Then
        Call ReOpenApplicationInNewExcelInstance
    Else
        l_blnSaveNewWorkbook = False
    End If

    Set ThisExcelApplication = Application

    Load ufConnection
    ufConnection.Show

End Sub

有我的Workbook_open,它会检查是否已打开现有工作簿,然后在新的excel实例上打开我的工作簿。但我的问题不是来自这里。

Private Sub ThisExcelApplication_WorkbookOpen(ByVal Wb As Workbook)
    Dim strFile As String

    If Wb.FullName = ThisWorkbook.FullName Then Exit Sub

    Set OtherExcelApplication = CreateObject("Excel.Application")

    strFichier = Wb.FullName
    Wb.Close False

    If g_blnApplicationVisibility Then
        ThisExcelApplication.Visible = True
    Else
        ThisExcelApplication.Visible = False
    End If

    OtherExcelApplication.Visible = True
    OtherExcelApplication.Workbooks.Open strFile
End Sub


Private Sub ReOpenApplicationInNewExcelInstance()
    Dim oExcel As Excel.Application
    Dim strFiler As String
    Dim strTemporaryFileName As String

    l_blnSaveNewWorkbook = True

    Set oExcel = CreateObject("Excel.Application")
    strFile = ThisWorkbook.FullName
    strTemporaryFileName = ThisWorkbook.Path & Application.PathSeparator 
& "TemporaryFileName.xlsb"

    'save current workbook on a temporary file to free the access of original workbook
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    ThisWorkbook.SaveAs strTemporaryFileName 
    Application.EnableEvents = True
    Application.DisplayAlerts = True

    'close the original workbook and re-open it on new instance
    oExcel.Workbooks.Open strFile

    'close the 1rst workbook and kill the temporary files
    ThisWorkbook.ChangeFileAccess xlReadOnly
    Kill strTemporaryFileName 
    ThisWorkbook.Close False
End Sub

当我在第一个工作簿已经打开时打开第二个工作簿时,将按逻辑方式调用ThisExcelApplication_WorkbookOpen方法。它检查已打开的工作簿的名称。如果它不是同一个名称,它会尝试打开一个新的excel实例并在其上打开此工作簿。 然后我在这一行得到错误6 Overflow:

OtherExcelApplication.Workbooks.Open strFichier

也许有人有想法帮助我?

非常感谢您的关注。

PS:对不起我的英文,可能不好!

0 个答案:

没有答案