打开userform时无法从资源管理器中打开Excel

时间:2015-03-31 11:02:32

标签: excel vba excel-vba excel-vba-mac

您好我正在开发一个项目,我必须让用户在打开Userform时打开Excel。我可以浏览其他excel文件,但不能浏览Explorer.Please help.It对我有很大的帮助。

    Option Explicit 
    Private Sub Workbook_Open() 
    Application.OnTime Now, "ThisWorkbook.OnlyOneOfMe" 
    Dim wks As Worksheet 
For Each wks In ActiveWorkbook.Worksheets 
    wks.Protect Password:="Nothing", _ 
    UserInterfaceOnly:=True 
Next wks 
    End Sub 
    Private Sub Workbook_BeforeClose(Cancel As Boolean) 
 'important to reset this
Application.IgnoreRemoteRequests = False 
    End Sub 

 Private Sub OnlyOneOfMe() 
Dim XlApp As Excel.Application 
On Error Goto BAD 
With Application 
    If Me.ReadOnly Or .Workbooks.Count > 1 Then 
        Me.ChangeFileAccess Mode:=xlReadOnly 
        Set XlApp = New Excel.Application 
        XlApp.Visible = True 
        XlApp.Workbooks.Open (Me.FullName) 
        Goto BAD 
    Else 
         'stop opening from explorer (but not from excel)
        .Visible = False 
        .IgnoreRemoteRequests = True 
        UserForm1.Show 
        .Visible = True 
        .Quit 
    End If 
    Exit Sub 
End With 
BAD:     If Err Then MsgBox Err.Description, vbCritical, "ERROR" 
Set XlApp = Nothing 
Me.Close False 
 End Sub 

3 个答案:

答案 0 :(得分:0)

如果UserForm是模态的,它将锁定excel的实例,直到表单关闭。 您需要使用户窗体非模态或在打开工作簿后关闭它。

或者您可以在打开工作簿之前禁用事件,这将阻止弹出UserForm。

将它放在打开工作簿的行之前 Application.EnableEvents = False

在开场线后再次启用均衡器     Application.EnableEvents = True

您需要为正确的实例/应用程序启用/禁用事件,因为您正在打开新的。 像这样: XlApp.EnableEvents = False XlApp.Workbooks.Open (Me.FullName) XlApp.EnableEvents = True

但是如果可行的话,你可能不需要打开新的excel实例。

还将事件启用行放入ErrorHandling If Err Then Application.EnableEvents = True: MsgBox Err.Description, vbCritical, "ERROR"

答案 1 :(得分:0)

您还可以尝试隐藏所有打开的用户表单。 打开工作簿后立即执行此操作:

For Each Object In VBA.UserForms Object.Hide Next

答案 2 :(得分:0)

我宁愿使用visual basic脚本来打开VBA项目中的userform。将以下代码放在纯文本文件中,并使用“.vbs”扩展名保存。(这必须与包含userform的excel文件位于同一文件夹中)

Option Explicit

Dim fso, curDir
Dim xlObj, file
Dim fullPath

Const xlMaximized = -4137 'constant to maximizes the background excel window

On Error Resume next
Set fso = CreateObject("Scripting.FileSystemObject")

curDir = fso.GetAbsolutePathName(".")
file ="\~$YourExcelFileName.xlsm"             
fullPath = curdir & File

If fso.FileExists(fullPath) Then ' checks if the project is open or not

    MsgBox "The project is in use!",64, "Notificación"

Else

    file ="\YourExcelFileName.xlsm"           
    fullPath = curdir & file

    Set fso = Nothing       
    Set xlObj = CreateObject("Excel.Application")

    With xlObj

        .WindowState = xlMaximized
        .Visible = False
        .Workbooks.Open fullPath
        .IgnoreRemoteRequests = True
        .Run "mainMethod"   

    End With

    set xlObj=Nothing       

End If

...然后在你的vba项目中添加一个公共子程序来监听前一个VBScript的调用(如上所述命名子程序,我称之为mainMethod)

Public Sub mainMethod()

   UserForm1.Show vbModeless

End Sub

...您还必须附加一个userform_terminate事件,以指示当您关闭userform时它必须退出当前和活动的excel实例:

Private Sub UserForm_Terminate()

   Application.Quit

End Sub

...当然你必须写一个工作簿的before_close事件来将.IgnoreRemoteRequests重置为false,如下所示(你也可以在之前的userform_terminate事件处理程序中写这个,但我相信这是一个更整洁的方法来实现它):

Private Sub Workbook_BeforeClose(Cancel As Boolean)

  ThisWorkbook.Saved = True 'if needed
  Application.IgnoreRemoteRequests = False

End Sub

一旦你设法做到这一点,你将有一个非常干净的独立应用程序,没有人会注意到它来自excel文件,它不会干扰任何其他excel实例。祝你好运。

AndrésAlejandroGarcíaHurtado

相关问题