如何关闭在其他子站点中打开的子工作簿? [英] How do I close workbooks in a sub that were opened in other subs?

查看:68
本文介绍了如何关闭在其他子站点中打开的子工作簿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个visual basic(VS 2017)程序,该程序将打开多达六个不同的Excel工作簿。当程序加载时,它将打开两个单独的工作簿,用于参考信息。另一个按钮(子)将根据列表框中的用户选择打开多达四个单独的工作簿。



之后,数据将直接输入到四个中的每一个中单独的视觉基本程序之外的工作簿。当所有数据都输入到各个工作簿中后,我将点击visual basic表单上的一个按钮(sub),它将关闭所有打开的电子表格。



当我运行应该关闭文件的sub时,我收到一条消息:对象引用未设置为对象的实例。



我需要做些什么才能完成这项工作?



我的尝试:


我偶然发现想要改变昏暗的陈述,例如



Dim wb as new Excel.workbook等等

我不知道还有什么可以尝试。

I am trying to write a visual basic (VS 2017) program which will open as many as six different Excel workbooks. When the program loads it will open two separate workbooks which will be used for reference information. Another button (sub) will open as many as four separate workbooks based on user selection from a listbox.

After that, data will be entered directly into each of the four workbooks individually outside of the visual basic program. When all of the data have been entered into the individual workbooks, I will click a button (sub) on the visual basic form which will close all of the open spreadsheets.

When I run the sub that is supposed to close the files, I get a message that says: "Object reference not set to an instance of an object."

What do I have to do to make this work?

What I have tried:

I have stumbled around trying to change the dim statements, such as

Dim wb as new Excel.workbook, etc, etc
I don't know what else to try.

推荐答案

你需要保持你打开的工作簿的句柄。



如果是我,我会有一个处理工作簿打开和关闭的类,它会存储对其中工作簿对象的引用。



然后可以将该类对象的集合存储为表单的私有属性。你的关闭按钮只需要遍历那个调用你班级关闭方法的集合。



正如@Ralf-Meier已经指出的那样,你没有给我们足够的信息更具体。
You need to keep the handles to the workbooks you open.

If it was me I would have a class that handled the opening and closing of the workbooks which would store a reference to the workbook object within it.

A collection of objects of that class could then be stored as a private property of your form. Your close button just needs to iterate through that collection calling the close method of your class.

As @Ralf-Meier has already pointed out, you haven't given us enough information to be more specific.


这篇关于如何关闭在其他子站点中打开的子工作簿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆