Click here to Skip to main content
15,666,844 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Updated 17-Feb-19 21:32pm
Ralf Meier 18-Feb-19 2:28am    
Without seeing anything from your methods and declaration I'm not able to give you any information - sorry.
Your Message means that you try to access something what is not assigned (has no content) at that time ...

1 solution

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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900