View Single Post
 
Old 08-11-2014, 12:59 PM
spencerw.smith spencerw.smith is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: May 2014
Posts: 3
spencerw.smith is on a distinguished road
Default Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file)

My code opens the file dialog picker to import a data file and it automatically saves and closes. I'm trying to fix it so that if the person selects a file it runs as is, [COLOR="rgb(139, 0, 0)"]BUT if the user clicks cancel on the file dialog box, the macro stops instead of continuing to create a blank file.[/COLOR]


Thanks for all help.

Here is my code:

Selection.WholeStory
Selection.Delete
Dim defpath As String

'creates dialog box object
Dim oDl As Object
Set oDl = Application.FileDialog(msoFileDialogFilePicker)
With oDl
'sets file path
.InitialFileName = "W:\Daily to Fortis\Today"
'switches the file view to "All Files" instead of "All Word Documents"
.AllowMultiSelect = True
.Show
For j = 1 To .SelectedItems.Count
Documents.Add .SelectedItems(j)
Next

End With


'switch page orientation to landscape
Selection.PageSetup.Orientation = wdOrientLandscape

'Change the default filepath for documents back to the original default
Options.DefaultFilePath(wdDocumentsPath) = defpath
Selection.WholeStory
With Selection.Font
.Name = "Courier New"
.Size = 6.5
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone

End With

'where and how to save document
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"W:\Daily to Fortis PDF\" & Format(Date, "MMDD") & " GM" & ".pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=False, UseISO19005_1:=False
Selection.HomeKey Unit:=wdStory


'Closes the workbook without saving
Application.Quit False

End Sub
Reply With Quote