Command Button Macro To Close Document Then Delete File

J

JBark

It would be very much appreciated if someone could help me figure out why the
kill statement is not working in this macro. When the button is clicked, it
closes the file but does not delete it from where the file is saved to.

Private Sub CommandButton1_Click()

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
'Check that file exists
If Len(DocPathName) > 0 Then
'First remove readonly attribute, if set
SetAttr DocPathName, vbNormal
End If
'Then delete the file
Doc.Close wdDoNotSaveChanges
Kill DocPathName
End Sub
 
J

Jay Freedman

Assuming the command button and the code you showed are stored in the
document itself, closing the document unloads the code from memory.
That instantly stops the macro, so it never gets to the Kill
statement.

If you want this to work, you have to store the code in a template,
either one in Word's Startup folder or the Normal.dot template. Also,
the button should be on a toolbar, not in the document itself.
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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