+ Reply to Thread
Results 1 to 2 of 2

Word files being overwritten when saved from excel.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    Word files being overwritten when saved from excel.

    Afternoon all,

    I am running code that exports various tables and the like to a new word document. I then need to give the user the option to save this word file just like using the 'save-as' function in word. I have this up and running fine however I have two issues. Firstly if a file with the same name already exists, it will be overwritten. Any ideas? And I need to specify the default folder in which the folder dialog should open.

    wdApp and wdDoc are both set as objects.

    Any help is really appreciated. Thanks guys.

    Code as below;
    MsgBox1 = MsgBox("Do you want to save to the folder?", vbYesNo)
    
    
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    
    
    If MsgBox1 = vbYes Then
    
    
    result = GetNextAvailableName(ByVal strPathAsString)
            varResult = Application.GetSaveAsFilename(FileFilter:="Word Files (*.docx), *.docx", InitialFileName:="TRICS Tables - Word")
    
    
            If varResult <> False Then
                wdApp.ActiveDocument.SaveAs Filename:=varResult, _
                FileFormat:=wdFormatDocumentDefault
                Exit Sub
            End If
    
    
    End If
    
    'Function;
    Function GetNextAvailableName(ByVal strPath As String) As String
    
    
        With CreateObject("Scripting.FileSystemObject")
    
    
            Dim strFolder As String, strBaseName As String, strExt As String, i As Long
            strFolder = .GetParentFolderName(strPath)
            strBaseName = .GetBaseName(strPath)
            strExt = .GetExtensionName(strPath)
    
    
            Do While .FileExists(strPath)
                i = i + 1
                strPath = .BuildPath(strFolder, strBaseName & " - " & i & "." & strExt)
            Loop
    
    
        End With
    
    
        GetNextAvailableName = strPath
    
    
    End Function

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,055

    Re: Word files being overwritten when saved from excel.

    You don't seem to be using the FileDialog, but the initial path is .InitialFileName

    varResult which is the name used to save the file doesn't go though the GetNextAvailableName function. Maybe something like this:

    Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    
    
    If MsgBox1 = vbYes Then
    
    
    result = GetNextAvailableName(ByVal strPathAsString)
    
    myFileName = GetNextAvailableName("C:\TRICS Tables - Word.docx")
    
            varResult = Application.GetSaveAsFilename(FileFilter:="Word Files (*.docx), *.docx", InitialFileName:=myFileName)
    
    
            If varResult <> False Then
                wdApp.ActiveDocument.SaveAs Filename:=varResult, _
                FileFormat:=wdFormatDocumentDefault
                Exit Sub
            End If
    
    
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Importing saved files into Excel
    By alexander.small in forum Excel General
    Replies: 0
    Last Post: 10-21-2013, 10:47 AM
  2. Replies: 3
    Last Post: 03-03-2013, 12:25 PM
  3. Excel Templates that change saved files
    By Snuffles in forum Excel General
    Replies: 2
    Last Post: 03-02-2010, 01:31 PM
  4. Excel sheet Saved as word doc using cell value
    By Oreg in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-24-2006, 02:33 AM
  5. Opening saved Excel files
    By Ray in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 07-02-2006, 07:30 AM
  6. Excel and Word will not open saved files
    By Flannagan in forum Excel General
    Replies: 2
    Last Post: 04-26-2006, 04:30 AM
  7. [SOLVED] saved excel files
    By Dave o in forum Excel General
    Replies: 1
    Last Post: 08-19-2005, 07:05 PM
  8. Replies: 1
    Last Post: 06-08-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1