不确定我是否正确行事。请告诉我。
我正在尝试在新实例中打开一个工作簿。但有些地方工作不顺利。以下是供您参考的代码。我试图在新实例中打开名为“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
答案 0 :(得分:1)
我将向您展示如何在另一个excel实例中运行宏 ,在您的情况下将显示 UserForm1
< /强>
1)创建新工作簿
2)打开 VBE ( Visual Basic编辑器) - ALT + F11
3)插入新的UserForm
和Module
(右键单击 项目浏览器 然后{{1 }} 的)。您的屏幕应类似于下图:
4)Add References for the Microsoft Visual Basic for Applications Extensibility 5.3
注意:我已经在我的代码中使用了这个,但你必须确保已正确连接它
5)在新创建的 Insert
中插入代码
Module1
6)现在,运行 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
宏,您将看到 Main
答案 1 :(得分: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