Add Working with Word 2007 - issues


I would really appreciate if someone could help me out.   We are using Word OLD automation in our Legacy application for last 9 years. This new upgrade from Word2003 to Word2007 is not compatible with our current code. Any suggestions/recommendations are highly appreciated.  Thanks in advance.
 
Issue:
 
Created word document with Word 2007 using OLE automation (Delphi Verion 7.0) and streamed into a BLOB field in Oracle database. We were able to see the word document on the screen before it got streamed into the database.  There are no issues savings this BLOB data into the database. When we tried to view the same document, our Delphi code streams the BLOB data into a temp word document. Application is not able to open the document at this point giving an error message “File may be corrupted”. But, when we export the BLOB data and stored the word document into the file system, we are able to open and view the document without any errors. The file size is very big (approximately 2MB+) as compared to the same document created using Word 2003 (approximately 120KB).
 
 
Code snippets:
 
Storing BLOB
 
Procedure SaveToDocument(aLetter: TComplaintPackageLetterItem);
var
  aVar1 : OLEVariant;
  aImageSequence, aDocSequence : string;
  sTempFile : PChar;
  aFileStream : TFileStream;
  aBlobStream : TStream;
  aDocumentHistoryItem : TDocumentHistoryItem;
  Q: TQuery;
begin
  aDocumentHistoryItem := TDocumentHistoryItem.Create;
  DBConnection.Database.StartTransaction;
  try
    aImageSequence := '';
    aDocSequence := '';
    sTempFile := StrAlloc(2550000); // Save the Word doc into a Temporary directory
    GetTempPath(StrBufSize(sTempFile), sTempFile);
    GetTempFileName(sTempFile, 'OCI', 0, sTempFile);
    aVar1 := string(sTempFile);
    aLetter.WordDocument.SaveAs(aVar1);
    aLetter.WordDocument.Close;
    aLetter.WordDocument.Disconnect;
    aLetter.WordDocument.Free;
    aLetter.WordDocument := nil;
    aFileStream := TFileStream.Create(sTempFile, fmOpenRead);  // Create a file Stream to it
 
    QueryCpl_Doc_Image_Seq.Active := False;
    QueryCpl_Doc_Image_Seq.Active := True;
    aImageSequence := QueryCpl_Doc_Image_Seq.FieldByName('NEXTVAL').AsString;
    QueryCpl_Doc_Image_Seq.Active := False;
 
    QueryID_Doc_Seq.Active := False;
    QueryID_Doc_Seq.Active := True;
    aDocSequence := QueryID_Doc_Seq.FieldByName('NEXTVAL').AsString;
    QueryID_Doc_Seq.Active := False;
 
    ComplaintObject.DocumentHistoryCollection.AddNewItem(aDocumentHistoryItem);
 
    Q := TQuery.Create(nil);
    try
      Q.DatabaseName := TableCpl_Doc_Image.DatabaseName;
      with Q.SQL do
      begin
        Add('INSERT INTO CPL_DOCUMENT_HISTORY (ID_DOC, ID_CPL, INDEX_ENTITY, DATE_DELIVERY, ' +
                                               'ID_EMP_UPDATED_BY)');
        Add('VALUES (:ID_DOC, :ID_CPL, :INDEX_ENTITY, :DATE_DELIVERY, ' +
                                              ':ID_EMP_UPDATED_BY)');
 
      end; //with Q.SQL
      with Q do
      begin
        ParamByName('ID_DOC').AsString := aDocSequence;
        ParamByName('ID_CPL').AsInteger := StrToInt(Self.ID);
        ParamByName('INDEX_ENTITY').AsInteger := aDocumentHistoryItem.EntityIndex;
        ParamByName('DATE_DELIVERY').AsDateTime := aDocumentHistoryItem.DeliveryDate;
        ParamByName('ID_EMP_UPDATED_BY').AsInteger := StrToInt(aDocumentHistoryItem.ID_Emp);
        ExecSQL;
      end; //with Q
    finally
      Q.Free;
    end; //try..finally
    TableCpl_Doc_Image.Open;
    TableCpl_Doc_Image.Insert;
    TableCpl_Doc_Image.FieldByName('ID_IMAGE').AsString := aImageSequence;
    TableCpl_Doc_Image.FieldByName('ID_DOC').AsString := aDocSequence;
    TableCpl_Doc_Image.FieldByName('IMAGE_SEQ').AsInteger := 0;
    TableCpl_Doc_Image.FieldByName('PAGE_COUNT').AsInteger := 0;
    aBlobStream := TableCpl_Doc_Image.CreateBlobStream(TableCpl_Doc_Image.FieldByName('IMAGE'), bmReadWrite);
    aBlobStream.CopyFrom(aFileStream, aFileStream.Size);
    TableCpl_Doc_Image.Post;
    aBlobStream.Free;
    TableCpl_Doc_Image.Close;
 
    aFileStream.Free;
    DeleteFile(sTempFile);
    StrDispose(sTempFile);
 
    DBConnection.Database.Commit;
  except
    on E: Exception do
    begin
      DBConnection.Database.Rollback;
      raise Exception.Create('Unable to Save Document Details' + #10#13 + E.Message);
    end; //on
  end; //try..except
end;
 
 
 
Retrieving BLOB
 
Procedure ShowFormLetters(sLetters: TDocumentHistoryItem; bPrintLetters, bOpenedFromCplForm, bCPLBatchPrint : Boolean);
var
  sTempFile : PChar;
  aFileStream : TFileStream;
  aStream : TStream;
  aDocument : TWordDocument;
  wordSave : OLEVariant;
begin
  // try to create the letter from the database
  if sLetters.ID_DOC <> 0 then
  begin
    //TableCpl_Doc_Image.Active := True;
    QueryDocImage.Close;
    QueryDocImage.ParamByName('ID_DOC').AsInteger := sLetters.ID_DOC;
    QueryDocImage.Open;
    if QueryDocImage.Bof and QueryDocImage.Eof then
      MessageDlg('Unable to locate requested letter.', mtError, [mbOK], 0);
    while not QueryDocImage.Eof do
    begin
      if not QueryDocImage.FieldByName('IMAGE').IsNull then
      //if not TableCpl_Doc_Image.FieldByName('IMAGE').IsNull then
      begin
        sTempFile := StrAlloc(2500000);
        try
          GetTempPath(StrBufSize(sTempFile), sTempFile);
          GetTempFileName(sTempFile, 'OCI', 0, sTempFile);
          aFileStream := TFileStream.Create(sTempFile, fmCreate);
          aStream := QueryDocImage.CreateBlobStream(QueryDocImage.FieldByName('IMAGE'), bmRead);
          aFileStream.CopyFrom(aStream, aStream.Size);
          aFileStream.Write(aFileStream, aFileStream.Size);
          aStream.Free;
          aFileStream.Free;
          aDocument := TWordDocument.Create(Self);
          aVar1 := string(sTempFile);
          aDocument.ConnectTo(PackageLetterCollection.
                              WordApplication.Documents.Open(aVar1, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                                                             EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                                                             EmptyParam, EmptyParam, EmptyParam,  EmptyParam));
          FormManager.aTmpFileList.Add(sTempFile);
          aDocument.Saved := False;
          aDocument.ActiveWindow.Caption := sLetters.EntityName + ', ' + sLetters.FormLetter;
          DeleteFile(sTempFile);
          StrDispose(sTempFile);
          wordSave := wdDoNotSaveChanges;
          if bPrintLetters then
          begin
            aDocument.PrintOut;
            aDocument.Close(wordSave);
          end
          else
          begin
            PackageLetterCollection.WordApplication.Visible := True;
            PackageLetterCollection.WordApplication.Activate;
          end;
        finally
          QueryDocImage.Close;
        end; // try..
      end
      else
        MessageDlg('No letter stored for this record.', mtError, [mbOK], 0);
    end;
  end;
end;
 
 
Questions:

Are there any BDE configuration changes needed to fix the above issue ?
Is there a different way of storing data into the BLOB field ?
Is there a different unit we should use for word 2007 instead of word97.pas ?
Related Discussions
  • DOT ON SCREEN (2001-01-03 05:42:55)
    Delphi does not support a Pixel function with a HDC, you have to use the Windows-API-function COLORREF SetPixel( HDC hdc, // handle...
  • HOW TO RUN DATABASE APPLICATION ON THE INTERNET (2017-06-09 10:54:22)
    Well, in design I think there is already an issue. Backend is more then just a database with which you connect. There should be a complete layer...
  • HELP WITH CHDIR !!!! TURBO PASCAL VS DELPHI (2001-01-03 08:48:49)
    This is because the ChDir Procedure, the SetCurrentDir and SetCurrentDirectory Functions are changing the directory in the active process. Try...
  • RUNNING A DATABASE APPLICATION ON ANOTHER COMPUTER (2001-01-07 07:36:33)
    In the BDE on the other PC (the one without the database), you must add an alias to point to the location of the database in the format of:...
  • ADD TO RESOURCE ON RUNTIME (2001-01-04 11:00:49)
    I think what you are asking is to change the .EXE file at run-time. Yes, it is possible, but nobody I found will share that information. If you...
  • TELEPHONE BASED APPLICATION (2001-01-08 12:29:39)
    I did something like this. I wrote a fully operative phone- based banking applciation in two days using VisualVoice and Dialogic cards. However,...
  • ICONS (2001-01-10 09:09:37)
    This component is not working correctly. Colors look different when saved with this component.
  • SAVING WITHOUT USING SAVETOFILE() ? (2001-01-11 08:50:58)
    I know that one.. It's not working correctly unforutnatly. When I save a 256 color as 8bit it looks *very* different.. if I save it in 32bit...
  • HOW CAN I ACCESS BTRIEVE DATA FILES (2001-01-24 08:38:59)
    Try downloading the Btrieve programmer's guide : ftp://ftp.pervasive.com/documentation/btrieve/v615/B615PROG.PDF Good luck ...
  • EXAMPLE OF STRINGTOWIDECHAR (2001-01-10 16:26:22)
    var WideCharBuf: array of WideChar; begin StringToWideChar(StringToConvert, WideCharBuf, SizeOf(WideCharBuf) div 2); end;
Latest News
Submit News Form Past News
Latest Forum Entries