Deleting a file

R

Rick

I have a macro that saves the currently opened file with
a new name and into a different directory. I then need it
to delete the original file. If I try to delete the
original before closing the renamed file, I get an error.
If I try to delete it after closing the renamed file,
it's as if the entire macro stops. The file is built on
the template that's used to create the document.

The macro runs and I get the 1st message box but not the
second.

Here's the code I'm using:
ChangeFileOpenDirectory FilePath 'Change to proper dir
ActiveDocument.SaveAs FullReportName,
FileFormat:=wdFormatDocument
On Error GoTo CleanUpErrorHandler
MsgBox ("Got Here 1")
ActiveDocument.Close ' closes currently opened file
MsgBox ("Got Here 2")
ChangeFileOpenDirectory Path
MsgBox (DelFile)
Kill DelFile ' deletes original file
End
 
D

dsc

This works for me:

Public Sub MAIN()
Dim button
Dim NameofFile As String
NameofFile = ActiveDocument.Name
ActiveDocument.SaveAs "FarngledyFarngledy.doc"
ActiveDocument.Close
button = MsgBox("Delete " + NameofFile, 17)
If button = 1 Then
Kill NameofFile
End If
End Sub

Are you sure you're passing the right string to ChangeFileOpenDirectory the
second time you call it?
 
R

Rick

The macro performs other functions and then at the end,
it renames & saves the file and then should delete the
original. I'd rather not have a button for the user -
just have the macro run. I am passing the correct
directory.

Since your macro is a Public Sub, I assume it is in
Normal.dot. My macro is not in Normal.dot but rather in
the template that the document is based on. I'm wondering
if by closing the document that the macro may be ending
early. Deletion of the file is the last thing the macro
should do.

Rick
 
D

dsc

I'd rather not have a button for the user -
just have the macro run.

I just use the button to give myself one last chance not to delete. No
problem to take those lines out.
Since your macro is a Public Sub, I assume it is in
Normal.dot. My macro is not in Normal.dot but rather in
the template that the document is based on. I'm wondering
if by closing the document that the macro may be ending
early. Deletion of the file is the last thing the macro
should do.

Can you put the macro in an add in file?

At work, we have an add in file on the server that gives us all access to
its macros.
 
R

Rick

I'm not sure how to create an Add-in. I know I can add it
through the Tools menu.

The macro I'm working on is one of 6 for a specific
template. The template has bookmarks, etc that are filled
with the macros. This last macro checks some of the data
and saves the file to another directory for archival
purposes. I didn't want to add this last macro to
Normal.dot since that can be a nightmare to manage for 18
users. I'd rather the template be on the server and I can
modify it as necessary. If I add the one macro to
Normal.dot then I'll have to touch each users machine if
changes are necessary. The template has its own toolbar
with buttons to fire the macros. Not sure how to add an
additional button to the special toolbar from Normal.dot.

Thanks for any further assistance you can provide.
 
C

Charles Kenyon

Why not put your macro in your template? Then your macro will be available
in all documents based on your template.

An Add-In (global template) provides macros that are available in every
document, not just those based on your template. You can make a template a
global template by putting it in the user's Word startup folder. Document
templates should not be used as global templates.

Do _not_ attempt to change the user's normal.dot for this.
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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

Top