Closing and reopening the same file

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I need a macro to close an open file but without saving its changed content.
Then reopen it again. Hence, the macro will have to be able to read the
filename first and use that name to reopen it. Any help is much appreciated.

TIA
Tom
 
If you're running this from a different workbook, but want to close the
activeworkbook.

Dim wkbkFullName as string
dim wkbk as workbook
with activeworkbook
wkbkfullname = .fullname
.close savechanges:=false
set wkbk = workbooks.open(filename:=wkbkfullname)
end with
 
It won't change the way the code works, but it looks more natural:

Dim wkbkFullName as string
dim wkbk as workbook
with activeworkbook
wkbkfullname = .fullname
.close savechanges:=false
end with
set wkbk = workbooks.open(filename:=wkbkfullname)
 
Thank you Dave. That's wonderful. It did exactly what I hoped it would do.
Just a couple of questions:
1. Does Excel capture the name of the active document with the line,
wkbkfullname = .fullname ?
2. The use of dot (.) fascintes me. Where can I find out how else it can be
used? Am not a programmer.

TIA
Tom
 
Check VBA's help for the With statement.

It makes coding easier to type and easier to read.

with workbooks("someworkbookname.xls").worksheets("somename")
.range("a1").value = "hi"
.range("b1:b99").clearcontents
.protect
end with

is easier to type and read than:


workbooks("someworkbookname.xls").worksheets("somename").range("a1").value _
= "hi"

workbooks("someworkbookname.xls").worksheets("somename").range("b1:b99") _
.clearcontents

workbooks("someworkbookname.xls").worksheets("somename").protect

The leading dot means that that property or method (like an adjective and or
verb) belongs to the object that was used in the preceding With statement. In
this case a range object on a certain worksheet in a specific workbook.

And so .fullname belongs to the object Activeworkbook.

..fullname will return the drive\path\filename.ext

..name will return only the filename.ext portion.



Thank you Dave. That's wonderful. It did exactly what I hoped it would do.
Just a couple of questions:
1. Does Excel capture the name of the active document with the line,
wkbkfullname = .fullname ?
2. The use of dot (.) fascintes me. Where can I find out how else it can be
used? Am not a programmer.

TIA
Tom
 
Thanks once again. By the way, when I named your original set of codes
"Reload" located in Personal.xls, and then called it inside another
procedure with:
Application.Run "'Personal.xls'!Module10.Reload"
the active document just closed but did not reopen. I wonder why it did not
execute the last line:
set wkbk = workbooks.open(filename:=wkbkfullname)
?

Tom
 
Maybe you made another change you didn't share???

I would have guessed that either the workbook would open or you'd see an error
message.

PS. I use xl2003 and I've modified one of my toolbars this way:

Tools|Customize|Commands Tab|File Category
About half way down the right hand side is: Update File

If I open the file in readonly mode, I can click on this button to close
(reminded to save if necessary) and reopen that file.


Thanks once again. By the way, when I named your original set of codes
"Reload" located in Personal.xls, and then called it inside another
procedure with:
Application.Run "'Personal.xls'!Module10.Reload"
the active document just closed but did not reopen. I wonder why it did not
execute the last line:
set wkbk = workbooks.open(filename:=wkbkfullname)
?

Tom
 
Dave Peterson said:
Maybe you made another change you didn't share???
I don't follow. Do you mean share as in, "Sharing Directory" within MS
Windows Explorer? If so that wasn't the case as all the documents used were
in the same directory location.
I would have guessed that either the workbook would open or you'd see an
error message.
Neither. The active workbook just closed with no error message.
 
I meant that maybe you made a change to the suggested code and you didn't share
it in your follow-up post. Share your curent version.

But maybe it's the location of the code. If you put it in the activeworkbook's
project, then as soon as the activeworkbook closes, the code stops. It has to
be in a different workbook.

I don't follow. Do you mean share as in, "Sharing Directory" within MS
Windows Explorer? If so that wasn't the case as all the documents used were
in the same directory location.

Neither. The active workbook just closed with no error message.
<<snipped>>
 
Dave Peterson said:
I meant that maybe you made a change to the suggested code and you didn't
share
it in your follow-up post. Share your curent version.
In my first reply I pointed out that the codes did exactly as I wanted.
Meaning that they did work. I placed a button
in the toolbar to activate it. Here they are:
Sub Reload()

' Reload the active document.
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Dim wkbkFullName As String
Dim wkbk As Workbook
With ActiveWorkbook
wkbkFullName = .FullName
.Close savechanges:=False
End With
Set wkbk = Workbooks.Open(Filename:=wkbkFullName)
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.StatusBar = False
End With
End Sub
But maybe it's the location of the code. If you put it in the
activeworkbook's
project, then as soon as the activeworkbook closes, the code stops. It
has to
be in a different workbook.
My next reply pointed out that they were located in the file Personal.xls
When used in another document that has many buttons to allow many funtions
to be
performed on the document, I wanted the document to reload before the user
activates
another function and without having to click on the "Reload Button" on the
toolbar.
Hence, within each of the other function procedures, I included the line:
Application.Run "'Personal.xls'!Module10.Reload"
but the document just closed down even though the "Reload" procedure was in
a different location
different from that of the document. Because there are many other documents
each with their own
individual functions that can be performed on them, I don't want having to
place the "Reload" procedure
in each one of them.
 
I read your first message and I thought that you may have done something
different than what you described.

How do you execute this macro?

If it's from a shortcut key, make sure that shortcut key combination does not
include the shift key.
 
Dave Peterson said:
I read your first message and I thought that you may have done something
different than what you described.

How do you execute this macro?
I did explain this in my last post. By placing a button in the menu bar and
assigning the name "Personal.xls!Reload" to it, on clicking the button it
executes the procedure. No shortcut key was used at all.
But I'm still puzzled why the procedure did not reload the active document
when it was called from inside another macro.
 
Yes you did.

But I have a difficult time picking out your response from the intermingled
text.

I couldn't duplicate your problem.

Are you sure that the button points to the correct macro in the correct
workbook.

You may have noticed that most people top post in the excel newsgroups. I find
it easier to read and follow. You may want to do that, too.
 
Dave Peterson said:
Yes you did.

But I have a difficult time picking out your response from the
intermingled
text.
I am using MS Outlook Express Newsgroup to access this forum. Its newsreader
shows the reply to my post together. So there is no problem following the
thread
I couldn't duplicate your problem.

Are you sure that the button points to the correct macro in the correct
workbook.
Nooooo. The macro procedure attached to the button located in the toolbar
works well but it doesn't work properly when it is called up inside another
procedure. That's what I've been saying.
You may have noticed that most people top post in the excel newsgroups. I
find
it easier to read and follow. You may want to do that, too.
I thought I was when I selected "microsoft.public.excel.misc." to subscribe
from the list of Newsgroups inside Outlook Express. Is there another way?
 
If you top post, I'll find it easier to read your responses. Just reply at the
top of the message.

It's got nothing to do with how you're reading the messages. It has everything
to do with how you type your reply in Outlook Express.

If you're calling the procedure from another procedure, then I'd guess that
there was something wrong in that calling procedure.

But I don't recall seeing anything more than:
Application.Run "'Personal.xls'!Module10.Reload"

I asked how you started this other procedure that contained this line and I
don't recall seeing your response. I'm not asking how you click on the button.
I'm asking about how you start the procedure that calls this subroutine.
 
If you're calling the procedure from another procedure, then I'd
guess that there was something wrong in that calling procedure.

But I don't recall seeing anything more than:
Application.Run "'Personal.xls'!Module10.Reload"

I asked how you started this other procedure that contained this
line and I don't recall seeing your response.
Your question then was, "How do you execute this macro?"
As we were talking about your macro at the time, the word "this" was
understood by me to mean that you wanted to know how I started "your macro".
Having pointed out that this subroutine was intended to be called inside
another macro I quoted the relevant line:
Application.Run "'Personal.xls'!Module10.Reload"
in my reply.
You have clarified your question above when you now use the phrase "this
other procedure". It is linked to another button in the document which calls
it as well as calling on the main procedure which carries out an analytical
task using the data in the document.
 
Tom said:
If you're calling the procedure from another procedure, then I'd
guess that there was something wrong in that calling procedure.

But I don't recall seeing anything more than:
Application.Run "'Personal.xls'!Module10.Reload"

I asked how you started this other procedure that contained this
line and I don't recall seeing your response.
Your question then was, "How do you execute this macro?"
As we were talking about your macro at the time, the word "this" was
understood by me to mean that you wanted to know how I started "your macro".
Having pointed out that this subroutine was intended to be called inside
another macro I quoted the relevant line:
Application.Run "'Personal.xls'!Module10.Reload"
in my reply.
You have clarified your question above when you now use the phrase "this
other procedure". It is linked to another button in the document which calls
it as well as calling on the main procedure which carries out an analytical
task using the data in the document. I don't have another guess.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top