Commit abcf13ce authored by Michael Vernier's avatar Michael Vernier

Added optional pdf name parameter, fixed working directory issue with new 2015 build

parent 07ccb213
...@@ -5,7 +5,7 @@ using System.Text; ...@@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Word;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Word2PDF namespace Word2PDF
{ {
...@@ -14,7 +14,7 @@ namespace Word2PDF ...@@ -14,7 +14,7 @@ namespace Word2PDF
static void Main( string[] args ) static void Main( string[] args )
{ {
object missing = Type.Missing; object missing = Type.Missing;
object notTrue = false; object notTrue = false;
string docxname = ""; string docxname = "";
try try
{ {
...@@ -23,20 +23,39 @@ namespace Word2PDF ...@@ -23,20 +23,39 @@ namespace Word2PDF
catch( Exception e ) catch( Exception e )
{ {
Console.WriteLine( "Missing file" ); Console.WriteLine( "Missing file" );
Console.WriteLine( "Usage: " + args[ 0 ] + "[docx filename]" ); Console.WriteLine( "Usage: " + System.AppDomain.CurrentDomain.FriendlyName + " <docx filename> [pdf filename]" );
Environment.Exit( -1 ); Environment.Exit( -1 );
} }
if( docxname == "--help" || docxname == "-help" || docxname == "-h" )
{
Console.WriteLine( "Usage: " + System.AppDomain.CurrentDomain.FriendlyName + " <docx filename> [pdf filename]" );
Environment.Exit( 0 );
}
FileInfo file = new FileInfo( docxname ); FileInfo file = new FileInfo( docxname );
object docxnameobj = ( object )file.FullName; object docxnameobj = (object)file.FullName;
//Console.WriteLine( "File name full: " + file.FullName ); //Console.WriteLine( "File name full: " + file.FullName );
string pdfname = file.FullName.Substring( 0, file.FullName.LastIndexOf( '.' ) ) + ".pdf"; string pdfname = "";
try
{
pdfname = args[ 1 ];
if( !pdfname.Contains( ".pdf" ) )
{
pdfname += ".pdf";
}
}
catch( Exception e )
{
pdfname = file.FullName.Substring( 0, file.FullName.LastIndexOf( '.' ) ) + ".pdf";
}
//Console.WriteLine( "pdf name full: " + pdfname ); //Console.WriteLine( "pdf name full: " + pdfname );
pdfname = Environment.CurrentDirectory + '\\' + pdfname;
object pdfnameobj = ( object )pdfname; object pdfnameobj = (object)pdfname;
object format = ( object )( WdSaveFormat.wdFormatPDF ); object format = (object)( WdSaveFormat.wdFormatPDF );
Microsoft.Office.Interop.Word.Application word = null; Microsoft.Office.Interop.Word.Application word = null;
...@@ -44,15 +63,15 @@ namespace Word2PDF ...@@ -44,15 +63,15 @@ namespace Word2PDF
try try
{ {
word = new Application(); word = new Application();
doc = word.Documents.Open( ref docxnameobj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing ); doc = word.Documents.Open( ref docxnameobj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing );
doc.SaveAs2( ref pdfnameobj, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing ); doc.SaveAs2( ref pdfnameobj, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing );
( ( Microsoft.Office.Interop.Word._Document )doc ).Close( ref missing, ref missing, ref missing ); ( (Microsoft.Office.Interop.Word._Document)doc ).Close( ref missing, ref missing, ref missing );
( ( Microsoft.Office.Interop.Word._Application )word ).Quit( ref notTrue, ref missing, ref missing ); ( (Microsoft.Office.Interop.Word._Application)word ).Quit( ref notTrue, ref missing, ref missing );
} }
catch( Exception e ) catch( Exception e )
{ {
...@@ -70,7 +89,7 @@ namespace Word2PDF ...@@ -70,7 +89,7 @@ namespace Word2PDF
{ {
Marshal.FinalReleaseComObject( word ); Marshal.FinalReleaseComObject( word );
word = null; word = null;
} }
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment