M
matDi
Hello,
I'm relatively new to .NET and Office-Development, so I apologize in advance
if my questions sound newbish or if I'm stealing anybody's time.
I am trying to convert Office 2007 (.docx) files to the old 97-2003 format
(.doc) by using the conversion functionality from Word.
I used the code example for ExportAsFixedFormat from msdn
(http://msdn.microsoft.com/en-us/library/bb412305.aspx) and modified it to
use the saveAs method from the Document object
(http://msdn.microsoft.com/en-us/library/bb221597.aspx).
As long as i convert pdf or xps files everything runs fine (which was
already the case when is used the exportAsFixedFormat method).
But if I'm trying to convert into an Office97 or RTF format the program runs
through without errors, but the result document is completely empty with no
text content. However the filesize of the result document indicate that the
content should be there.
Perhaps I have to set the fileFormat parameter when i close the document ?
But it only takes the WdOriginalFormat which is of no use for me here or am I
wrong ? What confuses me more is that the pdf-conversion runs fine without
setting any parameter in the close-method.
I'm pretty stuck here, any tips you can provide are appreciated.
Thanks in advance.
Here is my code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace ConvertDocCS
{
class Program
{
static void Main(string[] args)
{
// Make sure the correct number of command line arguments were
specified.
if (args.Length < 3)
{
ShowUsage();
return;
}
string sourceDoc = args[0];
string targetDoc = args[1];
string targetFormat = args[2].ToUpper();
WdSaveFormat saveFormat;
// Make sure the target format is valid.
switch (targetFormat)
{
case "PDF": saveFormat = WdSaveFormat.wdFormatPDF;
break;
case "XPS": saveFormat = WdSaveFormat.wdFormatXPS;
break;
case "DOC97": saveFormat = WdSaveFormat.wdFormatDocument97;
break;
case "RTF": saveFormat = WdSaveFormat.wdFormatRTF;
break;
case "DOCX": saveFormat =
WdSaveFormat.wdFormatDocumentDefault;
break;
default: throw new Exception("The specified target format is
not valid.");
}
Program p = new Program();
try
{
p.ConvertDocument(sourceDoc, targetDoc, saveFormat);
Console.WriteLine("Conversion complete!");
}
catch (Exception ex)
{
Console.WriteLine("Conversion Error: " + ex.Message);
}
}
static void ShowUsage()
{
Console.WriteLine();
Console.WriteLine("Usage: ConvertDoc SourceDocPath
TargetFilePath TargetFormat");
Console.WriteLine();
Console.WriteLine("SourceDocPath The fully qualified path to
the Word document to convert.");
Console.WriteLine(" Enclose the path in quotes
if it contains spaces.");
Console.WriteLine();
Console.WriteLine("TargetFilePath The fully qualified path to
the target file to create.");
Console.WriteLine(" Enclose the path in quotes
if it contains spaces.");
Console.WriteLine();
Console.WriteLine("TargetFormat The format to convert the
Word document to.");
Console.WriteLine(" Supported values are PDF and
XPS and DOC97.");
Console.WriteLine();
}
public void ConvertDocument(string sourceDocPath, string
targetFilePath, WdSaveFormat targetFormat)
{
// Make sure the source document exists.
if (!System.IO.File.Exists(sourceDocPath))
throw new Exception("The specified source document does not
exist.");
// Create an instance of the Word ApplicationClass object.
ApplicationClass wordApplication = new ApplicationClass();
Document wordDocument = null;
// Declare variables for the Documents.Open and
ApplicationClass.Quit method parameters.
object paramSourceDocPath = sourceDocPath;
object paramMissing = Type.Missing;
// Declare variables for the Document.ExportAsFixedFormat method
parameters.
object paramFilePath = targetFilePath;
object paramSaveFormat = targetFormat;
object paramLockComments = false;
object paramPassword = paramMissing;
object paramAddToRecentFiles = false;
object paramWritePassword = paramMissing;
object paramReadOnlyRecommended = false;
object paramEmbedTrueTypeFonts = true;
object paramSaveNativePictureFormat = true;
object paramSaveFormsData = true;
object paramSaveAsAOCELetter = false;
object paramEncoding = paramMissing;
object paramInsertLineBreaks = true;
object paramAllowSubstitions = true;
object paramLineEnding = paramMissing;
object paramAddBiDiMarks = false;
try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(ref
paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing);
// Save it in the specified format.
if (wordDocument != null)
wordDocument.SaveAs(ref paramFilePath, ref
paramSaveFormat, ref paramLockComments, ref paramPassword, ref
paramAddToRecentFiles,
ref paramWritePassword, ref
paramReadOnlyRecommended, ref paramEmbedTrueTypeFonts, ref
paramSaveNativePictureFormat,
ref paramSaveFormsData, ref
paramSaveAsAOCELetter, ref paramEncoding, ref paramInsertLineBreaks, ref
paramAllowSubstitions,
ref paramLineEnding, ref paramAddBiDiMarks);
}
catch (Exception e)
{
throw e;
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}
As long as i convert pdf or xps files everything runs fine (which was
already the case when is used the exportAsFixedFormat method).
But if I'm trying to convert into an Office97 or RTF format the program runs
through without errors, but the result document is completely empty with no
text content. However the filesize of the result document indicate that the
content should be there.
Perhaps I have to set the fileFormat parameter when i close the document ?
But it only takes the WdOriginalFormat which is of no use for me here or am I
wrong ? What confuses me more is that the pdf-conversion runs fine without
setting any parameter in the close-method.
I'm pretty stuck here, any tips you can provide are appreciated.
Thanks in advance.
I'm relatively new to .NET and Office-Development, so I apologize in advance
if my questions sound newbish or if I'm stealing anybody's time.
I am trying to convert Office 2007 (.docx) files to the old 97-2003 format
(.doc) by using the conversion functionality from Word.
I used the code example for ExportAsFixedFormat from msdn
(http://msdn.microsoft.com/en-us/library/bb412305.aspx) and modified it to
use the saveAs method from the Document object
(http://msdn.microsoft.com/en-us/library/bb221597.aspx).
As long as i convert pdf or xps files everything runs fine (which was
already the case when is used the exportAsFixedFormat method).
But if I'm trying to convert into an Office97 or RTF format the program runs
through without errors, but the result document is completely empty with no
text content. However the filesize of the result document indicate that the
content should be there.
Perhaps I have to set the fileFormat parameter when i close the document ?
But it only takes the WdOriginalFormat which is of no use for me here or am I
wrong ? What confuses me more is that the pdf-conversion runs fine without
setting any parameter in the close-method.
I'm pretty stuck here, any tips you can provide are appreciated.
Thanks in advance.
Here is my code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace ConvertDocCS
{
class Program
{
static void Main(string[] args)
{
// Make sure the correct number of command line arguments were
specified.
if (args.Length < 3)
{
ShowUsage();
return;
}
string sourceDoc = args[0];
string targetDoc = args[1];
string targetFormat = args[2].ToUpper();
WdSaveFormat saveFormat;
// Make sure the target format is valid.
switch (targetFormat)
{
case "PDF": saveFormat = WdSaveFormat.wdFormatPDF;
break;
case "XPS": saveFormat = WdSaveFormat.wdFormatXPS;
break;
case "DOC97": saveFormat = WdSaveFormat.wdFormatDocument97;
break;
case "RTF": saveFormat = WdSaveFormat.wdFormatRTF;
break;
case "DOCX": saveFormat =
WdSaveFormat.wdFormatDocumentDefault;
break;
default: throw new Exception("The specified target format is
not valid.");
}
Program p = new Program();
try
{
p.ConvertDocument(sourceDoc, targetDoc, saveFormat);
Console.WriteLine("Conversion complete!");
}
catch (Exception ex)
{
Console.WriteLine("Conversion Error: " + ex.Message);
}
}
static void ShowUsage()
{
Console.WriteLine();
Console.WriteLine("Usage: ConvertDoc SourceDocPath
TargetFilePath TargetFormat");
Console.WriteLine();
Console.WriteLine("SourceDocPath The fully qualified path to
the Word document to convert.");
Console.WriteLine(" Enclose the path in quotes
if it contains spaces.");
Console.WriteLine();
Console.WriteLine("TargetFilePath The fully qualified path to
the target file to create.");
Console.WriteLine(" Enclose the path in quotes
if it contains spaces.");
Console.WriteLine();
Console.WriteLine("TargetFormat The format to convert the
Word document to.");
Console.WriteLine(" Supported values are PDF and
XPS and DOC97.");
Console.WriteLine();
}
public void ConvertDocument(string sourceDocPath, string
targetFilePath, WdSaveFormat targetFormat)
{
// Make sure the source document exists.
if (!System.IO.File.Exists(sourceDocPath))
throw new Exception("The specified source document does not
exist.");
// Create an instance of the Word ApplicationClass object.
ApplicationClass wordApplication = new ApplicationClass();
Document wordDocument = null;
// Declare variables for the Documents.Open and
ApplicationClass.Quit method parameters.
object paramSourceDocPath = sourceDocPath;
object paramMissing = Type.Missing;
// Declare variables for the Document.ExportAsFixedFormat method
parameters.
object paramFilePath = targetFilePath;
object paramSaveFormat = targetFormat;
object paramLockComments = false;
object paramPassword = paramMissing;
object paramAddToRecentFiles = false;
object paramWritePassword = paramMissing;
object paramReadOnlyRecommended = false;
object paramEmbedTrueTypeFonts = true;
object paramSaveNativePictureFormat = true;
object paramSaveFormsData = true;
object paramSaveAsAOCELetter = false;
object paramEncoding = paramMissing;
object paramInsertLineBreaks = true;
object paramAllowSubstitions = true;
object paramLineEnding = paramMissing;
object paramAddBiDiMarks = false;
try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(ref
paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing);
// Save it in the specified format.
if (wordDocument != null)
wordDocument.SaveAs(ref paramFilePath, ref
paramSaveFormat, ref paramLockComments, ref paramPassword, ref
paramAddToRecentFiles,
ref paramWritePassword, ref
paramReadOnlyRecommended, ref paramEmbedTrueTypeFonts, ref
paramSaveNativePictureFormat,
ref paramSaveFormsData, ref
paramSaveAsAOCELetter, ref paramEncoding, ref paramInsertLineBreaks, ref
paramAllowSubstitions,
ref paramLineEnding, ref paramAddBiDiMarks);
}
catch (Exception e)
{
throw e;
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}
As long as i convert pdf or xps files everything runs fine (which was
already the case when is used the exportAsFixedFormat method).
But if I'm trying to convert into an Office97 or RTF format the program runs
through without errors, but the result document is completely empty with no
text content. However the filesize of the result document indicate that the
content should be there.
Perhaps I have to set the fileFormat parameter when i close the document ?
But it only takes the WdOriginalFormat which is of no use for me here or am I
wrong ? What confuses me more is that the pdf-conversion runs fine without
setting any parameter in the close-method.
I'm pretty stuck here, any tips you can provide are appreciated.
Thanks in advance.