PC Review


Reply
Thread Tools Rate Thread

Avoid saving file with code in

 
 
Rob
Guest
Posts: n/a
 
      13th Mar 2008
Hi, using Excel 2000.

I want to ensure that a file with the code isn't saved in error or during
testing. The code below saves the active workbook which works the majority
of times but has on occasion, saved the file with the code. I have set up a
line of code to capture the active file name when the code is run but can't
get the code that saves the file to display a message if the file bring
saved is the one with the code and then closes the file without saving.

Thanks in advance of any help. Rob

' this line sets iFname to the active file name
iFname = Application.ActiveWorkbook.Name

'save the active file
Set Wb = ActiveWorkbook
Application.DisplayAlerts = False
Wb.Save
Application.DisplayAlerts = True


 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      13th Mar 2008
ActiveWorkBook is ver dangerous code as it depends on what is active at the
time. The workbook running the code is ThisWorkbook. You do not indicate how
the other workbook is created or accessed so it is hard to comment. In
general you want to create a reference to the new workbook as soon as it is
created or accessed.

As a quick and dirty you could check the name of WB against the name of
thisworkbook

this line sets iFname to the active file name
iFname = Application.ActiveWorkbook.Name

'save the active file
Set Wb = ActiveWorkbook
Application.DisplayAlerts = False
if Wb.name <> Thisworkbook.name then
Wb.Save
else
msgbox "Wrong file to save"
end if
Application.DisplayAlerts = True
--
HTH...

Jim Thomlinson


"Rob" wrote:

> Hi, using Excel 2000.
>
> I want to ensure that a file with the code isn't saved in error or during
> testing. The code below saves the active workbook which works the majority
> of times but has on occasion, saved the file with the code. I have set up a
> line of code to capture the active file name when the code is run but can't
> get the code that saves the file to display a message if the file bring
> saved is the one with the code and then closes the file without saving.
>
> Thanks in advance of any help. Rob
>
> ' this line sets iFname to the active file name
> iFname = Application.ActiveWorkbook.Name
>
> 'save the active file
> Set Wb = ActiveWorkbook
> Application.DisplayAlerts = False
> Wb.Save
> Application.DisplayAlerts = True
>
>
>

 
Reply With Quote
 
Rob
Guest
Posts: n/a
 
      14th Mar 2008
Thank you Jim, maybe quick and dirty but does the job really well.

Regards, Rob
"Jim Thomlinson" <James_Thomlinson@owfg-Re-Move-This-.com> wrote in message
news:BA04D8E6-6369-4555-B48F-(E-Mail Removed)...
> ActiveWorkBook is ver dangerous code as it depends on what is active at
> the
> time. The workbook running the code is ThisWorkbook. You do not indicate
> how
> the other workbook is created or accessed so it is hard to comment. In
> general you want to create a reference to the new workbook as soon as it
> is
> created or accessed.
>
> As a quick and dirty you could check the name of WB against the name of
> thisworkbook
>
> this line sets iFname to the active file name
> iFname = Application.ActiveWorkbook.Name
>
> 'save the active file
> Set Wb = ActiveWorkbook
> Application.DisplayAlerts = False
> if Wb.name <> Thisworkbook.name then
> Wb.Save
> else
> msgbox "Wrong file to save"
> end if
> Application.DisplayAlerts = True
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Rob" wrote:
>
>> Hi, using Excel 2000.
>>
>> I want to ensure that a file with the code isn't saved in error or during
>> testing. The code below saves the active workbook which works the
>> majority
>> of times but has on occasion, saved the file with the code. I have set
>> up a
>> line of code to capture the active file name when the code is run but
>> can't
>> get the code that saves the file to display a message if the file bring
>> saved is the one with the code and then closes the file without saving.
>>
>> Thanks in advance of any help. Rob
>>
>> ' this line sets iFname to the active file name
>> iFname = Application.ActiveWorkbook.Name
>>
>> 'save the active file
>> Set Wb = ActiveWorkbook
>> Application.DisplayAlerts = False
>> Wb.Save
>> Application.DisplayAlerts = True
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving from template to new file without the vb code MyD0j0 Microsoft Excel Discussion 0 21st Jan 2008 03:30 PM
VB Code for saving PDF Print File =?Utf-8?B?ZGFsbGlu?= Microsoft Excel Programming 1 15th Jun 2007 10:20 PM
Avoid blank lines when saving copied data from an excel sheet as a text file. Excel Beginner Microsoft Excel Programming 1 29th Nov 2004 05:30 PM
vba code for saving file Michael Microsoft Excel Discussion 2 17th Sep 2003 09:15 AM
Re: Code for saving as a .prn file Jim Rech Microsoft Excel Programming 0 5th Aug 2003 01:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:05 PM.