|
Add
Working with Word 2007 - issues
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
|
Latest News
Latest Forum Entries
|