Results 1 to 3 of 3
  1. #1
    MrFarum is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2016
    Posts
    1

    Access VBA how to automatic save a document without overwrite

    Hey all,



    I am working on a way to automatic open a word document fill in some text and save, but the problem I am seeking an answer to is the following: "If the filename already exist my current code (See below) will overwrite the old file... BUT I want to keep both files, and eventually ad a uniq number to the new file, e.g. Doc(2).

    I am trying to avoid userinteraction, so no prompt or msg boxes.

    My code:

    Sub Makro1()
    Dim objWord As Object
    Dim objDoc As Object
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objDoc.Saveas "C:\Users\MFM\Documents"
    objDoc.Close
    Set objDoc = Nothing
    Set objWord = Nothing
    End Sub

    Question: How can I code so that the document will automatic saveas the two first words in the word document and if that filename already exist ad a (number) to the filename.

    The default mode in using objdoc.saveas is that it overwrite if the filename exist, I want to NOT overwrite but to keep both files without asking the user.


    Thank you

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    Code:
    '....your code here...
    'then
    vBase ="myFile"
    vName = vBase & ".doc"
    vFile = "C:\Users\MFM\Documents\" & vName
    while FileExists(vFile)
       i = i + 1
       vName = vBase & "(" & i & ").doc"
       vFile = "C:\Users\MFM\Documents\" & vName
    wend
    objDoc.Saveas vFile
    objDoc.Close
    Set objDoc = Nothing
    Set objWord = Nothing
    end sub
    
    Public Function FileExists(ByVal pvFile) As Boolean
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    FileExists = FSO.FileExists(pvFile)
    Set FSO = Nothing
    End Function

  3. #3
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    removed post - already answered
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. button to scan a document to pdf and save to folder
    By charlieb in forum Programming
    Replies: 2
    Last Post: 04-20-2015, 09:40 AM
  2. Automatic Document Naming
    By dbakeg00 in forum Access
    Replies: 7
    Last Post: 01-22-2015, 03:25 PM
  3. Replies: 3
    Last Post: 02-22-2013, 06:41 AM
  4. Replies: 1
    Last Post: 01-18-2013, 12:32 AM
  5. save document
    By scamper in forum Reports
    Replies: 1
    Last Post: 01-03-2012, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums