FileSaveAs function syntax error

VBA and general Excel Q&A and examples
Post Reply
eha
Posts: 2
Joined: Wed Jul 07, 2010 10:28 am

FileSaveAs function syntax error

Post by eha »

I'm using DDE to control DPlot from Excel, and am trying to save a 3D contour plot I have DPlot generate from Excel data. The problem I have is that when I try to execute the macro it gives me a syntax error in the FileSaveAs command. Apparently it expects the open quotation for the file name to be the terminator for the command. However, as far as I can tell I'm following the definition of the FileSaveAs command per the DPlot manual, and everything BUT the FileSaveAs command works fine. Any idea what I am doing wrong?

Here is the example from the manual:
FileSaveAs(1,"c:\mydata\|TITLE1|.grf")


Here is an excerpt from my macro:

For row = 1 To nrows
DDEExecute Channel, "[XYZEx(0,1," + Str$(Sel.Cells(row, 1)) + "," + Str$(Sel.Cells(row, 2)) + "," + Str$(Sel.Cells(row, 3)) + ")]"
Next row

Application.Wait (Now + TimeValue("0:00:03"))
'Adds a delay before displaying the plot
DDEExecute Channel, "[XYZRegen()][ViewRedraw()]"
'Displays the plot
DDEExecute Channel, "[ContourLevels(41,0,1)]"
'Sets contour levels
DDEExecute Channel, "[FileSaveAs(1,"d:\|TITLE1|.grf")]"

DDETerminate Channel

End Sub
User avatar
DPlotAdmin
Posts: 2312
Joined: Tue Jun 24, 2003 9:34 pm
Location: Vicksburg, Mississippi
Contact:

Post by DPlotAdmin »

Does your graph have a first title line? If not, that's the problem (though the error message could be a bit more helpful).

Oh wait... that isn't it, and you meant a syntax error produced by Excel rather than DPlot. It's the quotation marks. The entire command is a character string and so must be surrounded with quotation marks. Character string arguments also must be surrounded with quotation marks, but this:

Code: Select all

DDEExecute Channel, "[FileSaveAs(1,"d:\|TITLE1|.grf")]"
is interpreted as the command

"[FileSaveAs(1,"

followed by

d:\|TITLE1|.grf")]"

which Excel sees as nonsense. The easiest way to handle this in VB/VBA, when the string is a constant as it is in this case, is to double-up on the quotes, as in:

Code: Select all

DDEExecute Channel, "[FileSaveAs(1,""d:\|TITLE1|.grf"")]"
If instead your character string argument is a variable, as in

Code: Select all

Dim arg as string

arg = "d:\|TITLE1|.grf"
... then you'd need to do something like this:

Code: Select all

DDEExecute Channel, "[FileSaveAs(1," & chr$(34) & arg & chr$(34) & ")]"
In my defense, the example you cite is for the DPlot macro editor, where the quotation mark thing isn't an issue. And it is explained in the parent topic, but I need to do a better job explaining this in each of the many dozens of topics for commands that include character string arguments.

I'm curious about why you want to use Excel's DDEExecute, etc. rather than the Add-In commands (DPlot_Command in this case). With the latter you'll get better error diagnostics.
Visualize Your Data
support@dplot.com
Post Reply