vba 打开文件时,在新的 Excel 实例中打开特定的 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18775969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:18:08  来源:igfitidea点击:

Open specific Excel file in new instance of Excel when file is opened

excelexcel-vbavba

提问by user2292941

Info:

信息:

I have an Excel file named Demo.xlsm

我有一个名为 Demo.xlsm 的 Excel 文件

This file contains a userform named UserForm1 which automatically loads when the file is opened.

该文件包含一个名为 UserForm1 的用户窗体,它会在文件打开时自动加载。

The workbook named Demo.xlsm is also visible in the background behind the userform when the file is opened.

当文件打开时,名为 Demo.xlsm 的工作簿也在用户窗体后面的背景中可见。

Problem:

问题:

I often have an Excel application already open on my desktop with various workbooks and worksheets containing information I use on a daily basis. As it stands, if I go to open the Demo.xlsm file, it will open in the current Excel application along with all of the other workbook/worksheets I am using.

我经常在我的桌面上打开一个 Excel 应用程序,其中包含各种工作簿和工作表,其中包含我每天使用的信息。就目前而言,如果我打开 Demo.xlsm 文件,它将与我正在使用的所有其他工作簿/工作表一起在当前 Excel 应用程序中打开。

First: I would like for the Demo.xlsm file to automatically open in an entirely separate instance/Excel application than my other works.

第一:我希望 Demo.xlsm 文件在一个完全独立的实例/Excel 应用程序中自动打开,而不是我的其他作品。

Second: I would like for only the userform to be visible. (I have no need/use for the workbook/worksheets to be visible in the background.)

第二:我希望只有用户表单可见。(我不需要/使用在后台可见的工作簿/工作表。)

Third: If its possible to have the 2nd instance of the Excel application to be minimized while still displaying the userform, that would be perfect. (Currently, if I attempt to minimize the 2nd instance of the Excel application, the userform is also minimized)

第三:如果可以在仍然显示用户表单的同时最小化 Excel 应用程序的第二个实例,那将是完美的。(目前,如果我尝试最小化 Excel 应用程序的第二个实例,用户窗体也会最小化)

  Private Sub Workbook_Open()
  Dim objExcel
  Set objExcel = CreateObject("Excel.Application")
  objExcel.Visible = True

  UserForm1.Show

  Application.ScreenUpdating = False
  Set newBook = Workbooks.Open(Demo.xlsm)
  Windows(Demo.xlsm).Visible = False
  Application.ScreenUpdating = True 

  End Sub

I feel that I am not going about this the right way...

我觉得我没有以正确的方式解决这个问题......

Any help with this would be greatly appreciated!

对此的任何帮助将不胜感激!

回答by juriemagic

This is what I do, and it works lovely. I have a sheet with a nice big picture, and the picture has a macro, which is:

这就是我所做的,而且效果很好。我有一张带有漂亮大图的工作表,并且该图片有一个宏,即:

Dim appXL As New Excel.Application

appXL.Workbooks.Open "G:\All Users\Jurie\Test\Hotel Maintenance V1SE.xlsm"
appXL.ActiveWorkbook.Windows(1).Visible = True
appXL.Visible = True
Workbooks("Hotel Stay Easy Maintenance Program.xlsm").Close   SaveChanges:=True
ThisWorkbook.Activate

This opens my file in its own instance and I can close it as I want, it does not interfere with any other open files.

这会在自己的实例中打开我的文件,我可以根据需要关闭它,它不会干扰任何其他打开的文件。

回答by PatricK

For your Workbook_Open, do something like below should get you close.

对于您的Workbook_Open,执行以下操作应该会让您接近。

If Application.Workbooks.Count > 1 Then
    Call Shell(Application.Path & Application.PathSeparator & "excel.exe " & ThisWorkbook.FullName)
    ThisWorkbook.Close SaveChanges:=False
Else
    UserForm1.Show
End If

I tested this and there is another "Excel.exe" in Task Manager, so it should be right.

我对此进行了测试,任务管理器中还有另一个“Excel.exe”,所以应该是正确的。

回答by Dave

This worked for me. Copy paste this code into the ThisWorkbook object:

这对我有用。将此代码复制粘贴到 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

回答by Boltie

Copy a shortcut to Excel and then right-click, select properties and add to the end of the target " - "C:\Demo.xlsm"" (exclude the first and last quotation marks and replace with the real filepath for your file.

将快捷方式复制到 Excel,然后右键单击,选择属性并添加到目标的末尾“-“C:\Demo.xlsm””(排除第一个和最后一个引号,并替换为文件的实际文件路径。