![]() |
#1
|
|||
|
|||
![]()
Hello All,
I stumbled upon GetFileBox from searching the form and didn't find a very good example of using it with a save function so I wrote this. I am running X3 and it works great. My only wish is to return the key press from GetFileBox this way I can return better error codes. Since when the use cancels he will get a complaint about an incorrect path even though he didn't choose a path at all. I would like to hear any ideas you have as well so share up! Code:
Sub SaveNest() Dim sPath As String, sName As String, sOptions As StructSaveAsOptions 'Show file browser. sName = CorelScriptTools.GetFileBox("CMX - Corel Presentation Exchange 5.0|*.cmx", "Save Drawing", 1, "", ".cmx") 'If valid file path and name save. If sName <> "" Then 'Set variable to current users desktop path. 'sPath = Environ("SYSTEMDRIVE") & Environ("HOMEPATH") & "\Desktop\" 'Define save options Set sOptions = New StructSaveAsOptions With sOptions .EmbedICCProfile = False .EmbedVBAProject = False .Filter = cdrCMX5 .IncludeCMXData = False .Overwrite = True .Range = cdrAllPages .ThumbnailSize = cdr10KColorThumbnail .Version = cdrCurrentVersion End With 'Execute document save. ActiveDocument.SaveAs sName, sOptions Else MsgBox "Not a valid destination.", vbCritical, "ExportNest" End If End Sub |
#2
|
||||
|
||||
![]()
GetFileBox has two modes: Open and Save, controlled by the third parameter (0 or 1). In Open mode the return value is either an empty string or an existing file (always), in Save mode anything typed by a user may be returned (it seems).
if the returned value is an empty string this always means a user has cancelled the dialog. So for the existense of a file you'd better use another technique. Code:
.......... sName = CorelScriptTools.GetFileBox(.........) if sName="" then 'user cancelled the dialog else if Dir(sName)<>"" then msgbox "File already exists" endif on error resume next .............SAVE FILE CODE.......... if err.number then msgbox "Error saving: " & err.Description endif endif |
#3
|
|||
|
|||
![]()
That sheds some light on the empty string so I can always assume it is a user cancel on null string.
So is your code above a simple way to validate the destination folder? Is err.number and err.Description returned by VBA? Well this error check should be relative to whether .Overwrite is set to True or False. Last edited by m31uk3; 16-01-2008 at 10:05. |
#4
|
||||
|
||||
![]()
yes, "err" object is a VBA built-in one.
I'm not sure what "err.number" codes are in different scenarios, so I usually use a breakpoint right after SaveAs method for each possible scenario and check the "err" object in Watches pane for its values of err.number or err.description. |
#5
|
|||
|
|||
![]()
Does it matter where you place the On Error Resume Next?
I usually place this on the first line of my Sub/Fuction... |
#6
|
||||
|
||||
![]()
so do I, yet during a development time it sometimes is useful to comment that out in order to see what unexpected conditions might occur in order to handle them.
|
#7
|
|||
|
|||
![]()
Thanks for your help see the finalized code below:
Code:
Sub SaveNest() On Error Resume Next Dim aName As String, sPath As String, sName As String, sOptions As StructSaveAsOptions 'Set variable to current users desktop path. 'sPath = Environ("SYSTEMDRIVE") & Environ("HOMEPATH") & "\Desktop\Plotting\" 'Show file browser. sName = CorelScriptTools.GetFileBox("CMX - Corel Presentation Exchange 5.0|*.cmx", "Save Drawing", 1, "", ".cmx") 'If valid file path and name save. If sName = "" Then 'User cancelled the dialog Else 'Define save options Set sOptions = New StructSaveAsOptions With sOptions .EmbedICCProfile = False .EmbedVBAProject = False .Filter = cdrCMX5 .IncludeCMXData = False .Overwrite = True .Range = cdrAllPages .ThumbnailSize = cdr10KColorThumbnail .Version = cdrCurrentVersion End With 'Execute document save. ActiveDocument.SaveAs sName, sOptions 'Handle Errors If Err.Number Then MsgBox "File Save Error: " & Err.Description, vbCritical, "SaveDeSe" End If End If End Sub |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Converting CMX to CDR (X3) | Funky | Macros/Add-ons | 10 | 29-05-2008 01:41 |
how to save multiple pages in coreldraw 12 OR how to save quality eps for animation | kickingandscreaming | General | 2 | 19-08-2007 22:28 |
Save in CD11 is not working properly... | ddonnahoe | CorelDRAW/Corel DESIGNER VBA | 1 | 29-10-2004 14:11 |
Save Dialog Box | Mark | CorelDRAW/Corel DESIGNER VBA | 1 | 21-07-2004 06:15 |
Save as HTML Image Map | adriano | Corel Photo-Paint VBA | 1 | 15-04-2003 10:16 |