Trying to open the workbook in separate instance

后端 未结 2 1598
后悔当初
后悔当初 2020-12-02 02:28

Not sure whether I am doing it right. Please advise me.

I am trying to open one workbook in new instance. But some where it is not working fine. Below is the code fo

相关标签:
2条回答
  • 2020-12-02 03:00

    I'm just a newbie, but this worked for me. This code appears to open your file in a new instance of Excel. Copy and paste this vba code into the ThisWorkBook object:

    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
    
    0 讨论(0)
  • 2020-12-02 03:13

    I am going to show you how to run a macro in another instance of excel ,which in your case will display a UserForm1


    1) Create a new workbook
    2) Open the VBE (Visual Basic Editor) - ALT + F11
    3) Insert new UserForm and Module (right click in the project explorer then Insert). Your screen should look similar to the below picture:

    step 3 overview

    4) Add References for the Microsoft Visual Basic for Applications Extensibility 5.3
    note: I have this already in my code, but you have to make sure you have properly attached it

    5) In the newly created Module1 insert the code

    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) Now, run the Main Macro and you will be shown the Userform1

    0 讨论(0)
提交回复
热议问题