Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

Warn about overwriting an existing file?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mharris357@earthlink.net - 31 Mar 2006 04:47 GMT
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
Doug Robbins - Word MVP - 31 Mar 2006 05:02 GMT
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

Signature

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

>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
[quoted text clipped - 25 lines]
>
> End Sub
mharris357@earthlink.net - 31 Mar 2006 05:53 GMT
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.
Doug Robbins - Word MVP - 31 Mar 2006 15:49 GMT
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

Signature

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

> 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.
Jezebel - 31 Mar 2006 09:38 GMT
The SaveAs function doesn't check if the file exists ... it just overwrites.
You need to check for yourself, eg using Dir().

>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
[quoted text clipped - 25 lines]
>
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.