3

不确定我是否做得对。请给我提意见。

我正在尝试在新实例中打开一个工作簿。但是有些地方不能正常工作。以下是供您参考的代码。我正在尝试在新实例中打开名为“Loginfrm”的表单。

假设如果另一个工作簿已经打开,那么当前代码也会冻结该工作簿。理想情况下,这不应该发生。

Private Sub Workbook_Open()
Call New_Excel

Dim xlWrkBk As Excel.Workbook
Dim xlApp As New Excel.Application

Set xlWrkBk = xlApp.ActiveWorkbook
xlApp.Visible = True
'ThisWorkbook.Windows(1).Visible = False
LoginFrm.Show

End Sub
Sub New_Excel()
  'Create a Microsoft Excel instance via code
  'using late binding. (No references required)
  Dim xlApp As Object
  Dim wbExcel As Object

  'Create a new instance of Excel
  Set xlApp = CreateObject("Excel.Application")

  'Open workbook, or you may place here the
  'complete name and path of the file you want
  'to open upon the creation of the new instance
  Set wbExcel = xlApp.Workbooks.Add

  'Set the instance of Excel visible. (It's been hiding until now)
  xlApp.Visible = True

  'Release the workbook and application objects to free up memory
  Set wbExcel = Nothing
  Set xlApp = Nothing
End Sub
4

3 回答 3

2

我将向您展示如何在另一个 excel 实例中运行宏 ,在您的情况下将显示一个 1)创建一个新工作簿 2)打开VBEVisual Basic 编辑器)- 3)插入新的和(右键单击然后项目浏览器)。您的屏幕应该类似于下图: 4)添加 注释的引用:我的代码中已经有这个,但您必须确保已正确附加它UserForm1



ALT + F11
UserFormModule Insert

第 3 步概述

Microsoft Visual Basic for Applications Extensibility 5.3

5)在新创建Module1的插入代码

Sub Main()
    AddReferences
    AddComponent "UserForm1", "UserForm1.frm"
End Sub

Private Sub AddReferences()
    '   Name:            VBIDE
    '   Description:     Microsoft Visual Basic for Applications Extensibility 5.3
    '   GUID:            {0002E157-0000-0000-C000-000000000046}
    '   Major:           5
    '   Minor:           3
    '   FullPath:        C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB
    On Error Resume Next
    ThisWorkbook.VBProject.References.AddFromGuid GUID:="{0002E157-0000-0000-C000-000000000046}", _
                                                  Major:=5, Minor:=3
End Sub

Sub AddComponent(theComponent$, fileName$)

    ' export
    Application.VBE.ActiveVBProject.VBComponents(theComponent).Export ThisWorkbook.Path & "\" & fileName

    Dim xApp As Excel.Application
    Set xApp = New Excel.Application
    xApp.Visible = True

    Dim wb As Excel.Workbook
    Set wb = xApp.Workbooks.Add

    wb.VBProject.VBComponents.Import ThisWorkbook.Path & "\" & fileName

    CreateAModule wb
    xApp.Run "MacroToExecute"

    xApp.DisplayAlerts = False
    wb.Save
    wb.Close
    Set wb = Nothing
    xApp.Quit
    Set xApp = Nothing
    Application.DisplayAlerts = True
End Sub

Sub CreateAModule(ByRef wb As Workbook)

    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.vbComponent
    Dim CodeMod As VBIDE.CodeModule

    Set VBProj = wb.VBProject
    Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
    Set CodeMod = VBComp.CodeModule

    With CodeMod
        .DeleteLines 1, .CountOfLines
        .InsertLines 1, "Public Sub MacroToExecute()"
        .InsertLines 2, "    UserForm1.Show"
        .InsertLines 3, "End Sub"
    End With
End Sub


6)现在,运行Main宏,您将看到Userform1

于 2013-06-06T10:08:21.963 回答
1

我只是一个新手,但这对我有用。此代码似乎在 Excel 的新实例中打开您的文件。将此 vba 代码复制并粘贴到 ThisWorkBook 对象中:

Option Explicit

Dim objExcel As Excel.Application

Dim FileName As String

Public Sub workbook_open()
    FileName = ThisWorkbook.FullName
    If vbReadOnly <> 0 Then
        Exit Sub
    Else
        objExcel.Workbooks.Open FileName:=FileName
        ThisWorkbook.Saved = True
        ThisWorkbook.Close
        objExcel.Quit
    End If
End Sub
于 2013-12-21T02:29:42.297 回答
0

Microsoft 支持 - Microsoft Office 产品的命令行开关 - Excel

使用 Shell,您不必等待 Excel 的新实例完成启动/打开所需文件。

使用命令行开关 /x 将在新的 Excel 实例中打开文件。

如果您不想再次打开 XLSTART 目录中的文件(应用程序显示只读提示),您可以包括 /s 或 /safemode 开关以在安全模式下打开。

Sub OpenFileInNewInstanceOfExcel(ByVal pathToFile as String)
    Call Shell("excel.exe /x """ & pathToFile & """"
End Sub
于 2022-01-03T23:40:02.913 回答