|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
If the user cancels, then .SelectedItems.Count will be 0.
If (.SelectedItems.Count > 0) Then For j = 1 To .SelectedItems.Count Documents.Add .SelectedItems(j) Next else ' Nothing selected. Exit Sub end if |
#3
|
|||
|
|||
That's perfect. Thanks!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to deactivate File Save dialog box | KIM SOLIS | Word | 3 | 12-15-2013 12:30 PM |
An error occured while opening the file dialog | Warmwinds | PowerPoint | 7 | 11-14-2013 03:46 AM |
Importing selected lines from a txt file | Pedgonc | Excel | 2 | 01-09-2013 12:44 PM |
Word Macro: Save file as text with current file name | jabberwocky12 | Word VBA | 2 | 10-22-2010 12:23 PM |
Word crashes when using file dialog | jievie_2000@hotmail.com | Word | 5 | 07-27-2009 09:54 AM |