Warn about overwriting an existing file?

M

mharris357

I have been using the following macro to unmerge a long document and
create 6 labeled documents in same folder, but it does not warn about
overwriting an existing file. I would appreciate advise on how to add
that function.Thanks in advance!

Sub UnMerge()
Dim docNew As Word.Document
Dim sctsActive As Word.Sections
Dim astrFiles(1 To 6) As String
Dim i As Long

astrFiles(1) = "Label.doc"
astrFiles(2) = "Address.doc"
astrFiles(3) = "Data Entry.doc"
astrFiles(4) = "Rep.doc"
astrFiles(5) = "AppFile.doc"
astrFiles(6) = "Application.doc"

Set sctsActive = Word.ActiveDocument.Sections
For i = 1 To 6
Set docNew = Documents.Add
With docNew
sctsActive(i).Range.Copy
.Range.Paste
.SaveAs astrFiles(i)
End With
Next i

End Sub
 
M

mharris357

Actually, that did not work for me. I want a warning, "Overwrite file?"
that will allow me to relabel that file. It needs to fit into the
existing unmerge macro.
 
J

Jezebel

The SaveAs function doesn't check if the file exists ... it just overwrites.
You need to check for yourself, eg using Dir().
 
D

Doug Robbins - Word MVP

You need to make use of the information to which I directed you. This may
not be exactly what you want as it will just stop the routine. You may
instead want to display an input box into which the user can insert a new
filename:

Dim docNew As Word.Document
Dim sctsActive As Word.Sections
Dim astrFiles(1 To 6) As String
Dim i As Long

astrFiles(1) = "Label.doc"
astrFiles(2) = "Address.doc"
astrFiles(3) = "Data Entry.doc"
astrFiles(4) = "Rep.doc"
astrFiles(5) = "AppFile.doc"
astrFiles(6) = "Application.doc"

Set sctsActive = Word.ActiveDocument.Sections
For i = 1 To 6
Set docNew = Documents.Add
With docNew
sctsActive(i).Range.Copy
.Range.Paste
If Dir(astrFiles(i)) = "" Then
.SaveAs astrFiles(i)
Else
MsgBox "The document " & astrFiles(i) & "already exists"
Exit Sub
End If
End With
Next i


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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