(view source code of word2openoffice.cs as plain text)
using System;
using System.Collections.Generic;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
namespace Word2OpenOffice
{
class Word2OpenOffice
{
static string progver = "1.00";
static bool debug = false;
static int Main(string[] args)
{
#region Initialize Variables
#if DEBUG
debug = true;
#endif
bool overwrite = false;
bool prompt = false;
int converted = 0;
int failed = 0;
int matchingfiles = 0;
int overwritten = 0;
int skipped = 0;
string inputfilespec = null;
string inputfileext;
string parentfolder;
#endregion Initialize Variables
#region Command Line Parsing
if ( args.Length == 0 || args.Length > 2 )
{
return ShowHelp( );
}
foreach ( string arg in args )
{
switch ( arg.ToUpper( ) )
{
case "/?":
return ShowHelp( );
case "/O":
if ( overwrite )
{
return ShowHelp( "Duplicate command line switch /O" );
}
overwrite = true;
break;
case "/P":
if ( prompt )
{
return ShowHelp( "Duplicate command line switch /P" );
}
prompt = true;
break;
default:
if ( string.IsNullOrWhiteSpace( inputfilespec ) && arg.IndexOfAny( "/?;,<>".ToCharArray( ) ) == -1 )
{
inputfilespec = arg;
}
else
{
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
break;
}
}
#endregion Command Line Parsing
#region Command Line Validation
// Validate input filespec
if ( string.IsNullOrWhiteSpace( inputfilespec ) )
{
return ShowHelp( "Please specify an input file" );
}
parentfolder = Directory.GetParent( inputfilespec ).FullName;
if ( !Directory.Exists( parentfolder ) )
{
return ShowHelp( "Directory \"{0}\" not found", parentfolder );
}
inputfileext = Path.GetExtension( inputfilespec ).ToLower( );
if ( !inputfileext.Contains( "*" ) && inputfileext != ".doc" && inputfileext != ".docx" )
{
return ShowHelp( "Invalid file format for \"{0}\"", inputfilespec );
}
// check mutually exclusive switches /O and /P
if ( overwrite && prompt )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "WARNING:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "/O and /P switches are mutually exclusive; ignoring /O" );
Console.ResetColor( );
overwrite = false;
}
#endregion Command Line Validation
#region Iterate File List and Convert Each File
foreach ( string inputfile in Directory.GetFiles( parentfolder, Path.GetFileName( inputfilespec ) ) )
{
matchingfiles += 1;
if ( debug )
{
Console.WriteLine( "[{0}]\t\"{1}\"", matchingfiles, inputfile );
}
if ( Path.GetExtension( inputfile ) != ".doc" && Path.GetExtension( inputfile ) != ".docx" )
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write( "Skipped " );
Console.ResetColor( );
Console.WriteLine( "\"{0}\" because its extension is neither .DOC nor .DOCX", inputfile );
skipped += 1;
continue;
}
else
{
string outputfile = Path.ChangeExtension( inputfile, ".odt" );
if ( File.Exists( outputfile ) )
{
if ( overwrite )
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write( "Overwriting " );
Console.ResetColor( );
Console.WriteLine( "\"{0}\"", outputfile );
overwritten += 1;
}
else if ( prompt )
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write( "Output file \"{0}\" already exists, do you want to overwrite it? [y/N] ", outputfile );
Console.ResetColor( );
string answer = Console.ReadKey( false ).Key.ToString( );
if ( answer != "Y" )
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write( "\nSkipping " );
Console.ResetColor( );
Console.WriteLine( "\"{0}\" at user's request", Path.GetFileName( inputfile ) );
skipped += 1;
continue;
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write( "\nOverwriting " );
Console.ResetColor( );
Console.WriteLine( "\"{0}\" at user's request", Path.GetFileName( outputfile ) );
overwritten += 1;
}
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write( "Skipped " );
Console.ResetColor( );
Console.WriteLine( "\"{0}\" because \"{1}\" already exists", Path.GetFileName( inputfile ), Path.GetFileName( outputfile ) );
skipped += 1;
continue;
}
}
Console.Write( "Converting \"{0}\" . . . ", Path.GetFileName( inputfile ) );
if ( WordConvert( inputfile, outputfile ) )
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine( "Success" );
Console.ResetColor( );
converted += 1;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine( "Failed" );
Console.ResetColor( );
failed += 1;
}
}
}
#endregion Iterate File List and Convert Each File
// Show statistics
Console.ForegroundColor = ConsoleColor.White;
Console.Write( "\n{0} matching file names, ", matchingfiles );
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write( "{0} skipped, ", skipped );
Console.ForegroundColor = ConsoleColor.Green;
Console.Write( "{0} converted, ", converted );
Console.ForegroundColor = ConsoleColor.Red;
Console.Write( "{0} failed, ", failed );
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine( "{0} overwritten", overwritten );
Console.ResetColor( );
return 0;
}
static bool WordConvert( string inputpath, string outputpath )
{
try
{
Word.Application wordapp = new Word.Application( );
wordapp.Visible = false;
Word.Document worddoc = wordapp.Documents.Open( inputpath );
// Save the entire original document with the new file name and file type
worddoc.SaveAs( outputpath, Word.WdSaveFormat.wdFormatOpenDocumentText );
// Close the document(s) and application
object savechanges = Word.WdSaveOptions.wdDoNotSaveChanges;
worddoc.Close( ref savechanges );
wordapp.Quit( ref savechanges );
return true;
}
catch ( Exception )
{
return false;
}
}
static int ShowHelp( params string[] errmsg )
{
#region Help Text
/*
Word2OpenOffice, Version 1.00
Open a Microsoft Word document and save it in OpenOffice ODT format
Usage: Word2OpenOffice.exe "wordfile" [ Options ]
Where: wordfile Word document(s) to be converted (extension ".doc"
or ".docx"; wildcard "*" allowed, e.g. "name*.doc*")
Options: /O silently Overwrite existing output file(s)
(default: abort or skip if output file exists)
/P Prompt to overwrite existing output file(s)
(default: abort or skip if output file exists)
Notes: [1] This program requires a "regular" (MSI based) Microsoft Word
(2007 or later) installation, it will fail on an MS Office
"click-to-run" installation.
[2] The converted output file(s) will have the same name and location
as the input file(s), with extension ".odt".
[3] If wildcards are used in the Word file names, and /O or /P switch
are not used, the program will display an error message in case an
output file already exists, but it will then continue to convert
the next file instead of aborting.
[4] If Word was already active when this program is started, any other
opened document(s) will be left alone, and only the document(s)
opened by this program will be closed.
Examples: Word2OpenOffice.exe "D:\folder\myfile.doc"
will convert the file and save it to "D:\folder\myfile.odt"
Word2OpenOffice.exe "D:\folder\*.doc*"
will convert all matching files and save them to "D:\folder\*.odt"
Written by Rob van der Woude
http://www.robvanderwoude.com
*/
#endregion Help Text
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "Word2OpenOffice, Version {0}", progver );
Console.Error.WriteLine( "Open a Microsoft Word document and save it in OpenOffice ODT format" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Word2OpenOffice.exe \"wordfile\" [ Options ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "wordfile" );
Console.ResetColor( );
Console.Error.WriteLine( " Word document(s) to be converted (extension \".doc\"" );
Console.Error.WriteLine( " or \".docx\"; wildcard \"*\" allowed, e.g. \"name*.doc*\")" );
Console.Error.WriteLine( );
Console.Error.Write( "Options: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/O" );
Console.ResetColor( );
Console.Error.Write( " silently " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "O" );
Console.ResetColor( );
Console.Error.WriteLine( "verwrite existing output file(s)" );
Console.Error.WriteLine( " (default: abort or skip if output file exists)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /P P" );
Console.ResetColor( );
Console.Error.WriteLine( "rompt to overwrite existing output file(s)" );
Console.Error.WriteLine( " (default: abort or skip if output file exists)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: [1] This program requires a \"regular\" (MSI based) Microsoft Word" );
Console.Error.WriteLine( " (2007 or later) installation, it will fail on an MS Office" );
Console.Error.WriteLine( " \"click-to-run\" installation" );
Console.Error.WriteLine( " [2] The converted output file(s) will have the same name and location" );
Console.Error.WriteLine( " as the input file(s), with extension \".odt\"." );
Console.Error.Write( " [3] If wildcards are used in the Word file names, and " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/O" );
Console.ResetColor( );
Console.Error.Write( " or " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/P" );
Console.ResetColor( );
Console.Error.WriteLine( " switch" );
Console.Error.WriteLine( " are not used, the program will display an error message in case an" );
Console.Error.WriteLine( " output file already exists, but it will then continue to convert" );
Console.Error.WriteLine( " the next file instead of aborting." );
Console.Error.WriteLine( " [4] If Word was already active when this program is started, any other" );
Console.Error.WriteLine( " opened document(s) will be left alone, and only the document(s)" );
Console.Error.WriteLine( " opened by this program will be closed." );
Console.Error.WriteLine( );
Console.Error.Write( "Examples: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Word2OpenOffice.exe \"D:\\folder\\myfile.doc\"" );
Console.ResetColor( );
Console.Error.WriteLine( " will convert the file and save it to \"D:\\folder\\myfile.odt\"" );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( " Word2OpenOffice.exe \"D:\\folder\\*.doc*\"" );
Console.ResetColor( );
Console.Error.WriteLine( " will convert all matching files and save them to \"D:\\folder\\*.odt\"" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return 1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0110 seconds