Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 68
Default Event (BeforeSave) - How to test VBA code? Dave P. can you hear me now?

2003

Trying to understand an excellent procedure posted by Dave Petersen in
2005.

How do I test the flow of the code (variables etc)? When I run the
code by "saving" a file, the VBE window does not to appear to get
focus, nor can I pause the code.

Hopefully and most likely the answer is simple? The code is below.

As is, the code does not stop Excel from quering "Do you want to save
....." I would like to have the code intercept the XL standard queries
if possible. BTY, the code below is saved in an xla file which in turn
is "linked" as a XL Addin.

Also, the file is saves as .xlk which is not really a problem as I
assume that xlk is the default XL "Backup" file suffix (which would
explain where in the code the "k" came from).

TIA

Eagle One

************************************************** ***********

Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub Workbook_Open()
Set xlApp = Application
End Sub
Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub
Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, _
ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim myFileName As Variant
Dim testStr As String
Dim resp As Long

myFileName = Wb.FullName

If SaveAsUI Then
myFileName = Application.GetSaveAsFilename _
(InitialFileName:=Wb.FullName, _
filefilter:="Excel file, *.xls")
If myFileName = False Then
Cancel = True
Exit Sub
Else
testStr = ""
On Error Resume Next
testStr = Dir(myFileName)
On Error GoTo 0

resp = vbYes
If testStr = "" Then
'do nothing
Else
resp = MsgBox(Prompt:="Overwrite Existing File?", _
Buttons:=vbYesNo)
If resp = vbNo Then
Cancel = True
Exit Sub
End If
End If
End If
End If

'do the actual save
With Application
.StatusBar = "Saving " & myFileName
.DisplayAlerts = False
.EnableEvents = False
End With

On Error Resume Next
Wb.SaveAs myFileName, FileFormat:=xlWorkbookNormal,
CreateBackup:=True
If Err.Number < 0 Then
MsgBox "Something went wrong. File not saved" & vbLf _
& Err.Number & "--" & Err.Description
Err.Clear
Else
MsgBox "Saved as an xl workbook as: " & myFileName
End If
With Application
.StatusBar = False
.DisplayAlerts = True
.EnableEvents = True
End With

Cancel = True 'we did the work, don't let excel do it again.

[I AM NOT SURE THAT THE ABOVE LINE IS WORKING AS IT STATES???]

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Event (BeforeSave) - How to test VBA code? Dave P. can you hear menow?

First, you put this code into the ThisWorkbook module, right?

And second, you enabled macros when you opened this workbook? If you just added
the code, the workbook_open event has to run. You can close and reopen or just
run that event manually.

Then you can insert a breakpoint on a line with in the workbookbeforesave event.

Then saving the file should cause the application event to fire and you should
be able to step through the code.

EagleOne wrote:

2003

Trying to understand an excellent procedure posted by Dave Petersen in
2005.

How do I test the flow of the code (variables etc)? When I run the
code by "saving" a file, the VBE window does not to appear to get
focus, nor can I pause the code.

Hopefully and most likely the answer is simple? The code is below.

As is, the code does not stop Excel from quering "Do you want to save
...." I would like to have the code intercept the XL standard queries
if possible. BTY, the code below is saved in an xla file which in turn
is "linked" as a XL Addin.

Also, the file is saves as .xlk which is not really a problem as I
assume that xlk is the default XL "Backup" file suffix (which would
explain where in the code the "k" came from).

TIA

Eagle One

************************************************** ***********

Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub Workbook_Open()
Set xlApp = Application
End Sub
Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub
Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, _
ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim myFileName As Variant
Dim testStr As String
Dim resp As Long

myFileName = Wb.FullName

If SaveAsUI Then
myFileName = Application.GetSaveAsFilename _
(InitialFileName:=Wb.FullName, _
filefilter:="Excel file, *.xls")
If myFileName = False Then
Cancel = True
Exit Sub
Else
testStr = ""
On Error Resume Next
testStr = Dir(myFileName)
On Error GoTo 0

resp = vbYes
If testStr = "" Then
'do nothing
Else
resp = MsgBox(Prompt:="Overwrite Existing File?", _
Buttons:=vbYesNo)
If resp = vbNo Then
Cancel = True
Exit Sub
End If
End If
End If
End If

'do the actual save
With Application
.StatusBar = "Saving " & myFileName
.DisplayAlerts = False
.EnableEvents = False
End With

On Error Resume Next
Wb.SaveAs myFileName, FileFormat:=xlWorkbookNormal,
CreateBackup:=True
If Err.Number < 0 Then
MsgBox "Something went wrong. File not saved" & vbLf _
& Err.Number & "--" & Err.Description
Err.Clear
Else
MsgBox "Saved as an xl workbook as: " & myFileName
End If
With Application
.StatusBar = False
.DisplayAlerts = True
.EnableEvents = True
End With

Cancel = True 'we did the work, don't let excel do it again.

[I AM NOT SURE THAT THE ABOVE LINE IS WORKING AS IT STATES???]

End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 68
Default Event (BeforeSave) - How to test VBA code? Dave P. can you hear me now?

Thanks Dave,

Yes I did place the code in This Workbook. Buuuut I did not close and
re-open.


Dave Peterson wrote:
First, you put this code into the ThisWorkbook module, right?

And second, you enabled macros when you opened this workbook? If you just added
the code, the workbook_open event has to run. You can close and reopen or just
run that event manually.

Then you can insert a breakpoint on a line with in the workbookbeforesave event.

Then saving the file should cause the application event to fire and you should
be able to step through the code.

EagleOne wrote:

2003

Trying to understand an excellent procedure posted by Dave Petersen in
2005.

How do I test the flow of the code (variables etc)? When I run the
code by "saving" a file, the VBE window does not to appear to get
focus, nor can I pause the code.

Hopefully and most likely the answer is simple? The code is below.

As is, the code does not stop Excel from quering "Do you want to save
...." I would like to have the code intercept the XL standard queries
if possible. BTY, the code below is saved in an xla file which in turn
is "linked" as a XL Addin.

Also, the file is saves as .xlk which is not really a problem as I
assume that xlk is the default XL "Backup" file suffix (which would
explain where in the code the "k" came from).

TIA

Eagle One

************************************************** ***********

Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub Workbook_Open()
Set xlApp = Application
End Sub
Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub
Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, _
ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim myFileName As Variant
Dim testStr As String
Dim resp As Long

myFileName = Wb.FullName

If SaveAsUI Then
myFileName = Application.GetSaveAsFilename _
(InitialFileName:=Wb.FullName, _
filefilter:="Excel file, *.xls")
If myFileName = False Then
Cancel = True
Exit Sub
Else
testStr = ""
On Error Resume Next
testStr = Dir(myFileName)
On Error GoTo 0

resp = vbYes
If testStr = "" Then
'do nothing
Else
resp = MsgBox(Prompt:="Overwrite Existing File?", _
Buttons:=vbYesNo)
If resp = vbNo Then
Cancel = True
Exit Sub
End If
End If
End If
End If

'do the actual save
With Application
.StatusBar = "Saving " & myFileName
.DisplayAlerts = False
.EnableEvents = False
End With

On Error Resume Next
Wb.SaveAs myFileName, FileFormat:=xlWorkbookNormal,
CreateBackup:=True
If Err.Number < 0 Then
MsgBox "Something went wrong. File not saved" & vbLf _
& Err.Number & "--" & Err.Description
Err.Clear
Else
MsgBox "Saved as an xl workbook as: " & myFileName
End If
With Application
.StatusBar = False
.DisplayAlerts = True
.EnableEvents = True
End With

Cancel = True 'we did the work, don't let excel do it again.

[I AM NOT SURE THAT THE ABOVE LINE IS WORKING AS IT STATES???]

End Sub


--

Dave Peterson


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Event (BeforeSave) - How to test VBA code? Dave P. can you hear menow?

Some way that "Set xlApp = Application" line has to run--either by closing and
reopening or just manually rerunning.

And when you're testing, you may find that you kill the xlApp variable (maybe
hitting the reset button??). Just rerunning that workbook_open procedure will
be the easiest way to fix that problem.

EagleOne wrote:

Thanks Dave,

Yes I did place the code in This Workbook. Buuuut I did not close and
re-open.

Dave Peterson wrote:
First, you put this code into the ThisWorkbook module, right?

And second, you enabled macros when you opened this workbook? If you just added
the code, the workbook_open event has to run. You can close and reopen or just
run that event manually.

Then you can insert a breakpoint on a line with in the workbookbeforesave event.

Then saving the file should cause the application event to fire and you should
be able to step through the code.

EagleOne wrote:

2003

Trying to understand an excellent procedure posted by Dave Petersen in
2005.

How do I test the flow of the code (variables etc)? When I run the
code by "saving" a file, the VBE window does not to appear to get
focus, nor can I pause the code.

Hopefully and most likely the answer is simple? The code is below.

As is, the code does not stop Excel from quering "Do you want to save
...." I would like to have the code intercept the XL standard queries
if possible. BTY, the code below is saved in an xla file which in turn
is "linked" as a XL Addin.

Also, the file is saves as .xlk which is not really a problem as I
assume that xlk is the default XL "Backup" file suffix (which would
explain where in the code the "k" came from).

TIA

Eagle One

************************************************** ***********

Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub Workbook_Open()
Set xlApp = Application
End Sub
Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub
Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, _
ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim myFileName As Variant
Dim testStr As String
Dim resp As Long

myFileName = Wb.FullName

If SaveAsUI Then
myFileName = Application.GetSaveAsFilename _
(InitialFileName:=Wb.FullName, _
filefilter:="Excel file, *.xls")
If myFileName = False Then
Cancel = True
Exit Sub
Else
testStr = ""
On Error Resume Next
testStr = Dir(myFileName)
On Error GoTo 0

resp = vbYes
If testStr = "" Then
'do nothing
Else
resp = MsgBox(Prompt:="Overwrite Existing File?", _
Buttons:=vbYesNo)
If resp = vbNo Then
Cancel = True
Exit Sub
End If
End If
End If
End If

'do the actual save
With Application
.StatusBar = "Saving " & myFileName
.DisplayAlerts = False
.EnableEvents = False
End With

On Error Resume Next
Wb.SaveAs myFileName, FileFormat:=xlWorkbookNormal,
CreateBackup:=True
If Err.Number < 0 Then
MsgBox "Something went wrong. File not saved" & vbLf _
& Err.Number & "--" & Err.Description
Err.Clear
Else
MsgBox "Saved as an xl workbook as: " & myFileName
End If
With Application
.StatusBar = False
.DisplayAlerts = True
.EnableEvents = True
End With

Cancel = True 'we did the work, don't let excel do it again.

[I AM NOT SURE THAT THE ABOVE LINE IS WORKING AS IT STATES???]

End Sub


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Show do I share a dynamic list for data validation? KarenF Excel Discussion (Misc queries) 16 August 1st 06 10:51 PM
Lost my Paste Mike R Excel Discussion (Misc queries) 11 August 29th 05 12:22 AM
Macro for changing text to Proper Case JPriest Excel Worksheet Functions 3 August 8th 05 09:31 PM
Macro to simply bring up the Find dialogue box?? marika1981 Excel Discussion (Misc queries) 14 January 14th 05 10:47 PM
Worksheet Event Code chequer - ExcelForums.com Excel Worksheet Functions 0 November 10th 04 06:30 AM


All times are GMT +1. The time now is 01:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"