Word files being overwritten when saved from excel.

DouglasWicker

New Member
Joined
Aug 8, 2017
Messages
38
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.

Code as below;

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



Thanks all.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top