На главную Наши проекты:
Журнал   ·   Discuz!ML   ·   Wiki   ·   DRKB   ·   Помощь проекту
ПРАВИЛА FAQ Помощь Участники Календарь Избранное RSS
msm.ru
Модераторы: Rouse_, jack128, Krid
  
    > Сохранение данных из StringGrid в RTF (MS Word)
      Взято из Delphi Knowledge Base

      Сохранение данных из StringGrid в MS Word

      ExpandedWrap disabled
        uses  
          ComObj;  
         
        procedure TForm1.Button1Click(Sender: TObject);  
        var  
          WordApp, NewDoc, WordTable: OLEVariant;  
          iRows, iCols, iGridRows, jGridCols: Integer;  
        begin  
          try  
            // Create a Word Instance  
            // Word Instanz erzeugen  
            WordApp := CreateOleObject('Word.Application');  
          except  
            // Error...  
            // Fehler....  
            Exit;  
          end;  
         
          // Show Word  
          // Word anzeigen  
          WordApp.Visible := True;  
         
          // Add a new Doc  
          // Neues Dok einfugen  
          NewDoc := WordApp.Documents.Add;  
         
          // Get number of columns, rows  
          // Spalten, Reihen ermitteln  
          iCols := StringGrid1.ColCount;  
          iRows := StringGrid1.RowCount;  
         
          // Add a Table  
          // Tabelle einfugen  
          WordTable := NewDoc.Tables.Add(WordApp.Selection.Range, iCols, iRows);  
         
          // Fill up the word table with the Stringgrid contents  
          // Tabelle ausfullen mit Stringgrid Daten  
          for iGridRows := 1 to iRows do  
            for jGridCols := 1 to iCols do  
              WordTable.Cell(iGridRows, jGridCols).Range.Text :=  
                StringGrid1.Cells[jGridCols - 1, iGridRows - 1];  
         
          // Here you might want to Save the Doc, quit Word...  
          // Hier evtl Word Doc speichern, beenden...  
         
          // ...  
            
          // Cleanup...  
          WordApp := Unassigned;  
          NewDoc := Unassigned;  
          WordTable := Unassigned;  
        end;


      Сохранение в формате rtf
      Open a new Application and place:

      a button named Button3,
      a RitchText object named WordEditor
      and an OpenDialog component.

      From now on, you can browse for any *.doc file and load it into the RitchText object.

      NOTE: Format:=6 instructs Word to save the file as RTF. Extension is not enough.

      Other File Formats:

      Argument Format File Format
      0 Normal (Word format)
      1 Document Template
      2 Text Only (extended characters saved in ANSI character set)
      3 Text+Breaks (plain text with line breaks; extended characters saved in ANSI character set)
      4 Text Only (PC-8) (extended characters saved in IBM PC character set)
      5 Text+Breaks (PC-8) (text with line breaks; extended characters saved in IBM PC character set)
      6 Rich-text format (RTF)


      ExpandedWrap disabled
        procedure TImport_Form.ToolButton3Click(Sender: TObject);
        var
          WordApp: Variant;
        begin
          if OpenDialog1.Execute then
          begin
            Edit1.Text := ExtractFileName(OpenDialog1.FileName);
            StatusBar1.SimpleText := OpenDialog1.FileName;
            WordApp := CreateOleObject('Word.Basic');
            if not VarIsEmpty(WordApp) then
            begin
              WordApp.FileOpen(OpenDialog1.FileName);
              WordApp.FileSaveAs(Name := 'c:\temp_bb.rtf', Format := 6);
              WordApp.AppClose;
              WordApp := Unassigned;
              WordEditor.Lines.LoadFromFile('c:\temp_bb.rtf');
            end
            else
              ShowMessage('Could not start MS Word');
          end;
         
        end;

      How to prevent word from opening password-protected files or resume wizard files and sometimes causing application to hang ?

      The sollution is to add the folowing query before openning the document:

      if WordApp.ActiveDocument.HasPassword = True then
      MsgBox("Password Protected");

      You can even preset the password propery as:

      WordApp.Password := 'mypassword";

      NOTE: If the above code generates an "Undefined property: ActiveDocument" change the:

      CreateOleObject('Word.Basic');

      with

      CreateOleObject('Word.Application');


      Âçÿòî ñ http://www.baltsoft.com/

      Эта тема была разделена из темы "Сохранение данных из StringGrid в RTF"
      0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
      0 пользователей:


      Рейтинг@Mail.ru
      [ Script execution time: 0,0182 ]   [ 16 queries used ]   [ Generated: 28.03.24, 15:25 GMT ]