Problems saving docx to doc97 or rtf formats programmatically

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.
 
G

Gordon Bentley-Mix

It's unlikely that you will find much help in this newsgroup since most of
the regular posters here work almost exclusively with VBA and, therefore,
know very little about .NET. Accordingly, I would suggest that you post your
question in a .NET-specific newsgroup.

However, before I stopped reading your post because I couldn't understand
your code, I did see something in your question that I *did* recognise <g>
and that might give you some direction. You mention that you don't have any
problem converting to .pdf or .xps formats. There is a commonality between
these formats that may have an impact: Word considers these "print" formats
rather than "save" formats. That is to say that Word uses print methods with
the appropriate drivers to create .pdf and .xps files. OTOH, to create files
in Word 97-2003 format or RTF, Word performs a save operation. Dunno what
this might mean in the .NET world, but at least it's a place to start.

And don't worry about sounding like a newbie; we all were once. As for
"stealing" time... well... since we're all volunteers it means we give away
our time for free, and you can't steal what's given. ;-D
--
Cheers!

Gordon Bentley-Mix
Word MVP

matDi said:
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.
 
P

Peter Jamieson

I tried your code and had the same results. I've also had a look around
and seen some conversations abotut his general area, but have not yet
spotted a resoltion. I tried making the new instance of Word visible and
breaking after the SaveAs. Let's suppose your document is called
mydoc.docx and you are trying to save it as mydoc.rtf. The interesting
thing is that
a. when you .SaveAs from VBA, the document is saved, then the title
bar shows the new name, i.e. mydoc.rtf
b. when you .SaveAs from C#, mydoc.rtf is undoubtedly created, but the
title bar shows the old name, i.e. mydoc.docx. Further, I can tell from
the content of mydoc.rtf that it is exactly the same as the empty
document I would get if I created a new blank Document in Word.

In other words, it's as if .SaveAs is being applied to the wrong
document. Since, according to Documents.Count, no other document is
open, that is a little strange.

Considering the number of people out there apparently using this
technique, I can't help feeling there must be a simple error, but cannot
spot it right now.

Peter Jamieson

http://tips.pjmsn.me.uk

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.
 
M

matDi

Thanks for your reply. I ended up in changing every parameter I pass through
the saveAs method and it turned out that setting EmbedTrueTypeFonts to false
solved my problem. I yet don't know why but it works...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top