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

Re: Warn about overwriting an existing file? by Doug

Doug
Thu Mar 30 22:02:05 CST 2006

You will probably find something to help you in the article "How to save a
document using a filename that gets incremented by 1 each time if the
filename already exists":

http://www.word.mvps.org/FAQs/MacrosVBA/SaveIncrementedFilename.htm

--
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

<mharris357@earthlink.net> wrote in message
news:1143776873.640774.32070@g10g2000cwb.googlegroups.com...
>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
>



Re: Warn about overwriting an existing file? by mharris357

mharris357
Thu Mar 30 22:53:45 CST 2006

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.


Re: Warn about overwriting an existing file? by Jezebel

Jezebel
Fri Mar 31 02:38:42 CST 2006

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




<mharris357@earthlink.net> wrote in message
news:1143776873.640774.32070@g10g2000cwb.googlegroups.com...
>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
>



Re: Warn about overwriting an existing file? by Doug

Doug
Fri Mar 31 08:49:30 CST 2006

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

<mharris357@earthlink.net> wrote in message
news:1143780825.011434.281930@i40g2000cwc.googlegroups.com...
> 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.
>