473,550 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing an opened Excel spreadsheet correctly.

I want to make sure that I'm closing an opened spreadsheet correctly. I've
been having some locking up problems. The following code adds a dummy row to
the spreadsheet to ensure that that the data gets transferred in the correct
format. It seems to be working correctly, but if anyone has any comments on
that I'd appreciate it. Right now, I'm mostly concerned about opening and
closing the spreadsheet. Here's the pertinent part of the code:
Private Sub AddDummyRow()

On Error GoTo Err_Ctrl

Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
Dim mNumRows As Long, i As Integer
Dim boolXL As Boolean

'************** *************** *************** *************** ****
'************** *************** *************** *************** ****
'First, add the dummy row
'************** *************** *************** *************** ****
'************** *************** *************** *************** ****

'Check to see if Excel is running
If fIsAppRunning(" Excel") Then
Set xlsApp = GetObject(, "Excel.Applicat ion")
boolXL = False
Else
Set xlsApp = CreateObject("E xcel.Applicatio n")
boolXL = True
End If
'Open the workbook
Set xlsWB = xlsApp.Workbook s.Open(Me.Impor tFile)

'Open the selected sheet number
Set xlsWS = xlsWB.Worksheet s(CInt(Me.Sheet Number))
'Add the dummy row as the 2nd row in the spreadsheet
'First row is column headers and they will be field names in table
xlsWS.Range("A2 ").EntireRow.In sert
xlsWS.Cells(2, 1) = "DUMMY"
xlsWS.Cells(2, 2) = "DUMMY"
xlsWS.Cells(2, 3) = "DUMMY"
xlsWS.Cells(2, 4) = "DUMMY"
xlsWS.Cells(2, 5) = "DUMMY"
xlsWS.Cells(2, 6) = 0
xlsWS.Cells(2, 7) = 0
xlsWS.Cells(2, 8) = 0
xlsWS.Cells(2, 9) = 0
xlsWS.Cells(2, 10) = "DUMMY"
xlsWS.Cells(2, 11) = 0
xlsWS.Cells(2, 12) = 0
xlsWS.Cells(2, 13) = "DUMMY"
xlsWS.Cells(2, 14) = "DUMMY"
xlsWS.Cells(2, 15) = "DUMMY"
xlsWS.Cells(2, 16) = "DUMMY"
mNumRows = xlsWS.UsedRange .Columns.Count
'************** *************** *************** *************** ****
'************** *************** *************** *************** ****
'Second, transfer the spreadsheet to the holding table
'************** *************** *************** *************** ****
'************** *************** *************** *************** ****
Call TransferTheSpre adsheet
Exit_Sub: On Error Resume Next

'Close the instance of Excel and don't save changes
Set xlsWS = Nothing
xlsWB.Close False 'Don't save changes to the workbook.
Set xlsWB = Nothing

If boolXL Then
xlsApp.Applicat ion.Quit
End If

Set xlsApp = Nothing

DoCmd.SetWarnin gs True
DoCmd.Hourglass False
DoCmd.Echo True
Exit Sub

--
Message posted via http://www.accessmonster.com

Jul 31 '06 #1
7 2427
I've read that any time you use a SET statement, you should dereference
and destroy that pointer/variable. Basically, make sure you clean up
your own mess (the stuff you open/set).

Aug 3 '06 #2
I've read that any time you use a SET statement, you should dereference
and destroy that pointer/variable. Basically, make sure you clean up
your own mess (the stuff you open/set). For each of those you should
close the object and dereference (Set objThing = nothing).

Aug 3 '06 #3
"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
<6416064b5f5f9@ uwe>:
I want to make sure that I'm closing an opened spreadsheet correctly.
I've been having some locking up problems. The following code adds a
dummy row to the spreadsheet to ensure that that the data gets
transferred in the correct format. It seems to be working correctly,
but if anyone has any comments on that I'd appreciate it. Right now,
I'm mostly concerned about opening and closing the spreadsheet.
Here's the pertinent part of the code:
Private Sub AddDummyRow()

On Error GoTo Err_Ctrl

Dim xlsApp As Object, xlsWB As Object, xlsWS As Object
Dim mNumRows As Long, i As Integer
Dim boolXL As Boolean

'************** *************** *************** *************** ****
'************** *************** *************** *************** ****
'First, add the dummy row
'************** *************** *************** *************** ****
'************** *************** *************** *************** ****

'Check to see if Excel is running
If fIsAppRunning(" Excel") Then
Set xlsApp = GetObject(, "Excel.Applicat ion")
boolXL = False
Else
Set xlsApp = CreateObject("E xcel.Applicatio n")
boolXL = True
End If
'Open the workbook
Set xlsWB = xlsApp.Workbook s.Open(Me.Impor tFile)

'Open the selected sheet number
Set xlsWS = xlsWB.Worksheet s(CInt(Me.Sheet Number))
'Add the dummy row as the 2nd row in the spreadsheet
'First row is column headers and they will be field names in table
xlsWS.Range("A2 ").EntireRow.In sert
xlsWS.Cells(2, 1) = "DUMMY"
xlsWS.Cells(2, 2) = "DUMMY"
xlsWS.Cells(2, 3) = "DUMMY"
xlsWS.Cells(2, 4) = "DUMMY"
xlsWS.Cells(2, 5) = "DUMMY"
xlsWS.Cells(2, 6) = 0
xlsWS.Cells(2, 7) = 0
xlsWS.Cells(2, 8) = 0
xlsWS.Cells(2, 9) = 0
xlsWS.Cells(2, 10) = "DUMMY"
xlsWS.Cells(2, 11) = 0
xlsWS.Cells(2, 12) = 0
xlsWS.Cells(2, 13) = "DUMMY"
xlsWS.Cells(2, 14) = "DUMMY"
xlsWS.Cells(2, 15) = "DUMMY"
xlsWS.Cells(2, 16) = "DUMMY"
mNumRows = xlsWS.UsedRange .Columns.Count
'************** *************** *************** *************** ****
'************** *************** *************** *************** ****
'Second, transfer the spreadsheet to the holding table
'************** *************** *************** *************** ****
'************** *************** *************** *************** ****
Call TransferTheSpre adsheet
Exit_Sub: On Error Resume Next

'Close the instance of Excel and don't save changes
Set xlsWS = Nothing
xlsWB.Close False 'Don't save changes to the workbook.
Set xlsWB = Nothing

If boolXL Then
xlsApp.Applicat ion.Quit
End If

Set xlsApp = Nothing

DoCmd.SetWarnin gs True
DoCmd.Hourglass False
DoCmd.Echo True
Exit Sub

I think what is perhaps causing the problem here, is that you access/
open the same workbook both through automation, and through the
transfer
thingie at the same time.

I think you should try to ensure that when you invoke the Transfer
thingie, no instance of Excel holds the same file open, so either do
the
automation before the transfer thingie (if it's needed at all, is there
any reason this dummy row, if needed, couldn't be stuffed into the file
at the same time as the header row?), and ensure the workbook is closed
and saved (you might perhaps need a DoEvents or two) before firing the
Transfer thingie, or do the automation afterwards.

--
Roy-Vidar
Aug 3 '06 #4
RoyVidar,

The reason for the dummy line is really very simple.

When importing, Access will read a number of rows - somewhere between
20 and 30 - and determine whether the column is to be alphabetic or
numeric based on what it found in those 20+ lines. If you have a column
that has a mixture of data AND sometimes the first 50 lines or so
happen to be ALL numeric, Access will declare the column as numeric and
NOT import any and all of the cells that contain alphabetic data in
them for that column. The simple workaround is to create a DUMMY row
that forces Access to analyse the column correctly and then run a query
to delete the row.

Ron (been there, done that)

Aug 3 '06 #5
Thanks for the response.

I just want to make sure I understand. You're suggesting that I close all of
the automation variables before I call the subroutine
"TransferTheSpr eadsheet".

Seems to make sense. I'll give it a try.

Thanks.

RoyVidar wrote:
>"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
<6416064b5f5f9 @uwe>:
>I want to make sure that I'm closing an opened spreadsheet correctly.
I've been having some locking up problems. The following code adds a
[quoted text clipped - 81 lines]
> DoCmd.Echo True
Exit Sub

I think what is perhaps causing the problem here, is that you access/
open the same workbook both through automation, and through the
transfer
thingie at the same time.

I think you should try to ensure that when you invoke the Transfer
thingie, no instance of Excel holds the same file open, so either do
the
automation before the transfer thingie (if it's needed at all, is there
any reason this dummy row, if needed, couldn't be stuffed into the file
at the same time as the header row?), and ensure the workbook is closed
and saved (you might perhaps need a DoEvents or two) before firing the
Transfer thingie, or do the automation afterwards.
--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200608/1

Aug 3 '06 #6
"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
<6438f6bbc77c4@ uwe>:
Thanks for the response.

I just want to make sure I understand. You're suggesting that I
close all of the automation variables before I call the subroutine
"TransferTheSpr eadsheet".

Seems to make sense. I'll give it a try.

Thanks.
We'll, now I'm not too sure. I got a bit hung up in the name of the
function or sub you're calling, and assumed you were doing a
DoCmd.TransferS preadSheet.

Now I realize there's no information about what methods you're using
in your sub or function.

If you're using the .TransferSpread sheet method of the DoCmd object,
then yes, my suggestion is to close and save the workbook, and release
all the variables before this is performed.

If you're not using any of the Transfer.. methods, then I don't know.
Perhaps we would need to see the TransferTheSpre adsheet sub or
function?
Perhaps comment the on error resume next, and see if any errors raise
when you get one of your "locks".

--
Roy-Vidar
Aug 3 '06 #7
Roy,

Yes the DoCmd.TransferS preadSheet is used in the subroutine that is called.
I've already implemented your suggestion, but haven't had the chance to
really test it out.

I'll report back if I continue to have issues.

Thanks.

RoyVidar wrote:
>"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
<6438f6bbc77c4 @uwe>:
>Thanks for the response.
[quoted text clipped - 5 lines]
>>
Thanks.

We'll, now I'm not too sure. I got a bit hung up in the name of the
function or sub you're calling, and assumed you were doing a
DoCmd.Transfer SpreadSheet.

Now I realize there's no information about what methods you're using
in your sub or function.

If you're using the .TransferSpread sheet method of the DoCmd object,
then yes, my suggestion is to close and save the workbook, and release
all the variables before this is performed.

If you're not using any of the Transfer.. methods, then I don't know.
Perhaps we would need to see the TransferTheSpre adsheet sub or
function?
Perhaps comment the on error resume next, and see if any errors raise
when you get one of your "locks".
--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200608/1

Aug 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6514
by: David Gerstman | last post by:
I've written an application for reading information from a spreadsheet and allowing the user to add and update information in the spreadsheet. One spreadsheet I accessed in this fashion is now unreadable and get an application error when I open it. I also can't delete it. I'm assuming that I didn't properly close it down once. Is there...
1
47731
by: Jim, N2VX | last post by:
I'd like to create/display an Excel spreadsheet from javascript. We have an HTML page with results of a search and it can be reasonably large. The first attempt was to format the data into an HTML table and send it to an ASP page. The ASP page has: Response.AddHeader ("Content-Disposition", "inline"); Response.ContentType =...
15
4189
by: daniroy | last post by:
Hello everybody, your help will be very very welcome on a very classic question... I have some Excell Macro background, a little bit of VBA knowledge. I import daily from Internet series of data. They are setup as follow, with a few more columns on the rights as stock prices, dividend and so on as: Date Code Country Adjust...
9
3898
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. There is no further explanation. What are the kinds of things that make this happen? Thanks from an obvious rookie. Gordon
1
8906
by: Roger Twomey | last post by:
I have a form that allows a user to upload a microsoft Excel Spreadsheet. (I am testing with one created in Excel 2000) The spreadsheet is uploaded correctly. It is then supposed to be read and display in a datagrid. When the code executes I get: System.Data.OleDb.OleDbException: Could not find installable ISAM
14
5761
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do...
5
1819
by: Dave | last post by:
In a VB.NET application, I have a datagrid that I export to an Excel spreadsheet like this: ' Set the content type to Excel Response.ContentType = "application/vnd.ms-excel" Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' Get the HTML for the control.
5
43410
by: barbara_dave | last post by:
Hi All, I need to read data from a Excel spreadsheet, but I got the problem when I tried the code below: StringBuilder sbConn = new StringBuilder(); sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" ); sbConn.Append(excelFile); sbConn.Append(";Extended Properties="); sbConn.Append(Convert.ToChar(34));
0
2783
by: f.oualid | last post by:
Hi, Would anyone know how I could open an already opened excel workbook. I have Windows Form with a button, and when clicked it opens the spreadsheet as follows string filename = @"C:\Documents\Test.xls";
0
7563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7490
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7520
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6104
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5129
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3533
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1980
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1096
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.