I want to send Corel Draw .CDR drawing binary files and XML SVG files from the application to a server via HTTP POST.
I have done some research and this existing post seems closest but doesn't work for my situation: How can I send an HTTP POST request to a server from Excel using VBA?
I've added a user-custom button to the Corel Draw tool pane and created a macro to run when this button is pressed. The macro contains the following code.
Sub OpenLabelPrintExport()
'
' Recorded 24/06/2008
'
' Description:
'
'
' Add a reference to Microsoft WinHTTP Services
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
'MsgBox "hello"
Dim expflt As ExportFilter
Dim expopt As StructExportOptions
Dim responseText As String
Set expopt = New StructExportOptions
expopt.UseColorProfile = False
' expopt.DontExportFonts
Set expflt = ActiveDocument.ExportEx("C:\afile.svg", cdrSVG, cdrAllPages, expopt)
expflt.Finish
file = "C:\afile.svg"
Dim oS As ADODB.STREAM
Set oS = New STREAM
oS.Type = 2
oS.Open
oS.LoadFromFile file
Dim contentlength As Integer
contentlength = oS.Size
sEntityBody = "-----boundary" & vbCrLf
sEntityBody = sEntityBody & "Content-Dispostion: form-data; name=fileInputElementName; filename=""" + sFileName + """" & vbCrLf
sEntityBody = sEntityBody & "Content-Transfer-Encoding: 7bit" & vbCrLf
sEntityBody = sEntityBody & "Content-Type: text/xml" & vbCrLf & vbCrLf
' did use oS
sEntityBody = sEntityBody & "text" & vbCrLf
sEntityBody = sEntityBody & "-----boundary--" & vbCrLf & vbCrLf
' Set xhr = New MSXML2.XMLHTTP30
Dim xhr As WinHttp.WinHttpRequest
Set xhr = New WinHttpRequest
xhr.Open "POST", sUrl, False
xhr.SetRequestHeader "Content-Type", "multipart/form-data; boundary=""-----boundary"""
xhr.Send sEntityBody
End Sub
On my server, I have the following Perl CGI script to accept the file:
#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/usr/lib/cgi-bin/";
my $query = new CGI;
my $filename = $query->param("file");
my $email_address = $query->param("email_address");
if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your file (try a smaller file).";
exit;
}
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("file");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( )
{
print UPLOADFILE;
}
close UPLOADFILE;
print STDOUT "success";
I have tested the server-side script with a HTML form on a brower.
I would like advise on getting the VBA script that runs in Corel Draw to work correctly. I have searched and searched and can't seem to find a definitive answer to sending binary and text files from a VBA enabled application to a server via HTTP POST. I have bought some books on the subject too but am no wiser.
I need this to work with Corel Draw 12 and Corel Draw X4.
Thanks in advance.
You can save the file locally, and then use cURL to post the data to your server (using the Shell command in VBA).
Here is a working solution for Corel Draw 12. This is for exporting SVG - it could be extended to export .CDR and .PDF at the same time using the exporter object provided by Corel for the Visual Basic Application environment. For these two binary formats, base64 may be required to encode them before sending.
Credits:
credit http://www.vbforums.com/showthread.php?t=337424 extended this function to actually do the sending originally the function used Winsock - but this is unavailable in the Visual Basic Application environment of Corel Draw 12/XIV So I replaced this with a WinHttpRequest
credit: http://bytes.com/topic/asp-classic/answers/659406-winhttprequest-posting-byte-string-multipart-message-howto#post2618801 - for adding the req.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary line which is required in WinHttpRequest so that the server-side code on receiving the post can retrieve the actual file data and other params
credit: http://word.mvps.org/faqs/macrosvba/DeleteFiles.htm to delete the temporary file
Four parts to the solution provided below:
1) Instructions
2) The Corel Draw Visual Basic Application code
3) the server-side Perl CGI script to accept the file sent as a standard HTTP POST CGI message
4) test html form web page just to test the server-side Perl cgi script