delphims-wordole-automation

Delphi Ole Automation - Save Word97-2003 file in Word 2010 without Save As dialog


I'm trying to save a Word 97-2003 document (.doc) using Delphi 6 and Word 2010.

Before Word 2010 everything worked fine with

WordDoc.SaveAs(FileName := FileName, FileFormat := wdFormatDocument);

where FileName := 'c:\doc.doc'

Now, Word 2010 presents an Save Dialog and I'm not sure why. I've tried the new SaveAs2 method

WordDoc.SaveAs2(FileName := FileName, FileFormat := wdFormatDocument, CompatibilityMode:= wdWord2003);

but with the same result.

Oddly,

WordDoc.SaveAs2(FileName := FileName, FileFormat := wdFormatDocumentDefault, CompatibilityMode:= wdWord2003);

works fine, without the Save As dialog, but the saved file has Word 2010 format and .doc extension, which will confuse old Word versions.

So, any ideas how I can save a file in old document Word format using Word 2010 without Save As dialog popping up?


Solution

  • @David Heffernan: Well, writing a short demonstrating program solved my problem.

    The original program opened an *.mhtml file and tried to convert it to doc format. And there I had the problem. When you create a new doc you can save it it any format without problem. That lead me to think that maybe the issue was that I saved from a non-native format to another non-native format.

    So, my solution was to save it twice: first in the native format and then to the old format:

    procedure SaveDocFile(WordDoc: Variant; FileName: string);
    const wdFormatDocumentDefault=16;
    begin
      WordDoc.ActiveWindow.View.Type := wdPrintView;
      if WordDoc.Application.Version='14.0' then
      begin
        WordDoc.SaveAs2(FileName := FileName, FileFormat := wdFormatDocumentDefault);
        WordDoc.SaveAs2(FileName := FileName, FileFormat := wdFormatDocument);
      end
      else
        WordDoc.SaveAs(FileName := FileName, FileFormat := wdFormatDocument);
    end;