Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
hykal1993
Contributor II
Contributor II

Macro Export to Excel will Overwrite Data without Warning.

I'm using Macro below to export the excel file to a specific folder. However, it will overwrite if there's existing file without warning. How can I create a warning? Thanks.


'Main

sub exportToExcelMain


Dim path

path = getExportPathMain()


dim o

Set o = ActiveDocument.GetSheetObject("Main")

o.ExportBiff path


Set o = nothing


msgbox("Export to " & path & " succeeded!")


end sub


function getExportPathMain


getExportPathMain = ActiveDocument.Variables("Main Export").GetContent.String


end function

1 Solution

Accepted Solutions
andrey_krylov
Specialist
Specialist

Hi. Try this

sub exportToExcelMain


Dim path

path = getExportPathMain()


Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(path) Then

  If MsgBox(path & " already exists. Do you want to replace it?", vbYesNo, "Confirm Save As") = vbNo Then

       Exit sub

  End If

End If

dim o

Set o = ActiveDocument.GetSheetObject("Main")

o.ExportBiff path


Set o = nothing


msgbox("Export to " & path & " succeeded!")


end sub


function getExportPathMain


getExportPathMain = ActiveDocument.Variables("Main Export").GetContent.String


end function

View solution in original post

3 Replies
m_woolf
Master II
Master II

Google vbscript check if file exists

andrey_krylov
Specialist
Specialist

Hi. Try this

sub exportToExcelMain


Dim path

path = getExportPathMain()


Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(path) Then

  If MsgBox(path & " already exists. Do you want to replace it?", vbYesNo, "Confirm Save As") = vbNo Then

       Exit sub

  End If

End If

dim o

Set o = ActiveDocument.GetSheetObject("Main")

o.ExportBiff path


Set o = nothing


msgbox("Export to " & path & " succeeded!")


end sub


function getExportPathMain


getExportPathMain = ActiveDocument.Variables("Main Export").GetContent.String


end function

hykal1993
Contributor II
Contributor II
Author

‌thank you soo much!!