Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2014, 12:59 PM
spencerw.smith spencerw.smith is offline Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Windows 7 64bit Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Office 2010 64bit
Novice
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file)
 
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
  #2  
Old 08-11-2014, 01:30 PM
Cosmo Cosmo is offline Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Windows Vista Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Office 2007
Competent Performer
 
Join Date: Mar 2012
Posts: 240
Cosmo is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 08-12-2014, 07:56 AM
spencerw.smith spencerw.smith is offline Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Windows 7 64bit Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Office 2010 64bit
Novice
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file)
 
Join Date: May 2014
Posts: 3
spencerw.smith is on a distinguished road
Default

That's perfect. Thanks!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) How to deactivate File Save dialog box KIM SOLIS Word 3 12-15-2013 12:30 PM
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) 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
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Word Macro: Save file as text with current file name jabberwocky12 Word VBA 2 10-22-2010 12:23 PM
Stop macro if no file is selected in dialog box (when user presses cancel instead of selecting file) Word crashes when using file dialog jievie_2000@hotmail.com Word 5 07-27-2009 09:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft