473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Closing an excel workbook fires Workbook_BeforeClose

this code opens the workbook.. either protects or unprotects a sheet..

then saves.. so can write to it..

it unprotects then saves fine.. then writes to it no prob.. it resets
the password again no prob.. but on close of the workbook object.. it
goes into the workbook_beforeclose event within the vba of the file!!
at this point it fails for no apparent reason as this event works
perfectly when closing through excel !!

=============================================

Sub CreateTemp()

Dim str As String = Server.MapPath("Downloads\" &
(Session("mCallCentreID") & "_download"))
Dim DirNew = New DirectoryInfo(str)

Dim MasterFile As String = Server.MapPath("Downloads\Return.xls")
Dim FileNew As New FileInfo(MasterFile)

Try
If Not DirNew.Exists Then
DirNew.Create()
str = str & "\Return.xls"
FileNew.CopyTo(str, True)
End If

UnlockXLSheet(True, str)
WriteIDtoXL(str)
UnlockXLSheet(False, str)

Catch ex As Exception
Throw New Exception("Error : " + ex.ToString())
End Try

End Sub

Private Function xlConn(ByVal FilePath As String) As String

xlConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
FilePath & ";Extended Properties=""Excel 8.0;HDR=YES;MAXSCANROWS=4"""

End Function

Private Sub WriteIDtoXL(ByVal str)
Dim conn1 As New System.Data.OleDb.OleDbConnection(xlConn(str))
conn1.Open()

Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = conn1
cmd.CommandText = "INSERT INTO [CCID] (ID) values (" &
Session("mCallCentreID") & ")"
cmd.ExecuteNonQuery()
conn1.Close()

End Sub

Private Sub UnlockXLSheet(ByVal UnlockSheet As Boolean, ByVal
strPath As String)

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Application")
oBook = oExcel.workbooks.open(strPath)

'Add headers to the worksheet on row 1.
oSheet = oBook.sheets("Instructions")
If UnlockSheet Then
oSheet.unprotect(Password:="pw")
Else
oSheet.protect(Password:="pw", DrawingObjects:=True,
Contents:=True, Scenarios:=True)
End If
oBook.Save()

NAR(oSheet)
oBook.close(True)
NAR(oBook)
oExcel.Quit()

NAR(oExcel)

GC.Collect()
GC.WaitForPendingFinalizers()

End Sub
Nov 18 '05 #1
0 1568

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

Similar topics

2
by: Robert Scheer | last post by:
Hi. One of the pages of my application opens an Excel 2000 workbook in a new window using Javascript. The workbook is read only and I use a window.open command to open the workbook. On the...
0
by: Sania | last post by:
Hi, We have an application that create an excel object on the very beginning of the application: objExcel = New Excel.Application Then I am passing the excel reference to the one of forms...
7
by: rdemyan via AccessMonster.com | last post by:
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...
2
by: ridawg | last post by:
Hey, I have some code where I open up Excel then loop through several cases to update several workbooks. Basically something like this (not showing all the code just basic structure). Try...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
6
by: pleaseexplaintome_2 | last post by:
Help please. The excel instance is removed from task manager when I run the code below. If I uncomment the lines pertaining to a workbook, the instance of excel is not removed from task manager. ...
1
by: fakehitswizard | last post by:
this is the correct way to close excel with C#. I've seen alot of other bogus posts ALL over the web that don't work, how frustrating. string savepath; bool foundPID; int ourPID = 0; int...
2
by: prakashsakthivel | last post by:
What I want to know is that while one excel application is closed by VBA code from module, another excel application has to be opened. That means, I have written code to open new excel sheet in the...
2
by: Silgd1 | last post by:
Hi All.... I'm using python 2.4, Win XP Pro v.2002 sp3, and I use pyscripter 1.9.9.2 as my editor. I have written code to open an existing excel file and grab some data. The problem I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.