OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > Code Critique

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 15-01-2008, 16:54
m31uk3
Guest
 
Posts: n/a
Default Save as CMX 5.0 with GetFileBox

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
Thanks!
Reply With Quote
  #2  
Old 16-01-2008, 07:22
wOxxOm's Avatar
wOxxOm wOxxOm is offline
Senior Member
 
Join Date: Mar 2005
Posts: 836
Default

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
Reply With Quote
  #3  
Old 16-01-2008, 09:59
m31uk3
Guest
 
Posts: n/a
Default

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.
Reply With Quote
  #4  
Old 16-01-2008, 11:27
wOxxOm's Avatar
wOxxOm wOxxOm is offline
Senior Member
 
Join Date: Mar 2005
Posts: 836
Default

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.
Reply With Quote
  #5  
Old 16-01-2008, 14:58
m31uk3
Guest
 
Posts: n/a
Default

Does it matter where you place the On Error Resume Next?

I usually place this on the first line of my Sub/Fuction...
Reply With Quote
  #6  
Old 16-01-2008, 15:06
wOxxOm's Avatar
wOxxOm wOxxOm is offline
Senior Member
 
Join Date: Mar 2005
Posts: 836
Default

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.
Reply With Quote
  #7  
Old 17-01-2008, 15:53
m31uk3
Guest
 
Posts: n/a
Default

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
Best
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
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


All times are GMT -5. The time now is 13:15.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com