Autor Beitrag
lexmy3
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 140

WIN 2000 Prof. + Office 2000
Delphi 5 Enterprise
BeitragVerfasst: Do 26.01.06 17:01 
Hallo
Ich möchte mit Delphi einen Serien-Dokument-Vorlage schreiben.

Habe diesen Code aus dem Forum. Aber es geht nicht oder ich mache was falsch.


ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Word97, OleServer, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WordApplication1: TWordApplication;
    WordDocument1: TWordDocument;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
  ComObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  // Word Document to open
  // Dein Word Dokument
  YourWordDocument = 'c:\test\worddoc.doc';
var
  BookmarkName, Doc, R: OleVariant;
begin
  // Start a Word instance
  // Word Instanz Starten
  try
//WordApplication1 := CreateOleObject('WordApplication');
  except
    ShowMessage('keine Verbindung zur MS Word!');
  end;
  // Open your Word document
  // Dok. öffnen
  WordApplication1.Connect;
  WordApplication1.Documents.Open(YourWordDocument);
  Doc := WordApp.ActiveDocument;

  // name of your bookmark
  // Name der Textmarke
  BookmarkName := 'Marke';

  // Check if bookmark exists
  // Überprüfen, ob Textmarke vorhanden
  if Doc.Bookmarks.Exists(BookmarkName) then
  begin
    R := Doc.Bookmarks.Item(BookmarkName).Range;
    // Add text at our bookmark
    // Text bei Textmarke einfügen
    R.InsertAfter('Text bei Textmarke');
    // You make a text formatting like changing its color
    // Man kann nun auch den Text formatieren. z.B die Farbe wechseln.
    R.Font.Color := clRed;
  end;

  // Save your document and quit Word
  // Dokument Speichern und Word beenden.
  if not VarIsEmpty(WordApp) then
  begin
    WordApp.DisplayAlerts := 0;
    WordApp.Documents.Item(1).Save;
    WordApp.Quit;
    BookmarkName := Unassigned;
    R := Unassigned;
    WordApp := Unassigned;
  end;
end;


end.


Ich bekomme also hier die Fehlermeldung:
[Fehler] Unit1.pas(42): Inkompatible Typen: 'TWordApplication' und 'IDispatch'
[Fehler] Unit1.pas(49): Die Typen der tatsächlichen und formalen Var-Parameter müssen übereinstimmen

Es gibt mehrere Optionen Word anzusprechen doch bis jetzt hatte ich kein Glück.

Hatt einer eventuell nen Tip was ich hier ändern sollte
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 26.01.06 20:20 
es geht nicht, weil du die verschiedenen Optionen auf Word zuzugreifen wild mischst.

mit Hilfe der Serverkomnpos wird so ein Schuh draus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
procedure TForm1.Button14Click(Sender: TObject);
var BookmarkName, R,YourWordDocument: OleVariant;
begin
  // Dok. öffnen
  WordApplication1.Connect;
  try
    YourWordDocument:='c:\test\worddoc.doc';
    WordApplication1.Documents.Open(YourWordDocument,emptyparam,emptyparam,emptyparam,
                                    emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,
                                    emptyparam,emptyparam,emptyparam);
    WordDocument1.ConnectTo(WordApplication1.ActiveDocument);

    // Name der Textmarke
    BookmarkName := 'Marke';

    // Überprüfen, ob Textmarke vorhanden
    if WordDocument1.Bookmarks.Exists(BookmarkName) then
    begin
      R := WordDocument1.Bookmarks.Item(BookmarkName).Range;
      // Text bei Textmarke einfügen
      R.InsertAfter('Text bei Textmarke');
      // Man kann nun auch den Text formatieren. z.B die Farbe wechseln.
      R.Font.Color := clRed;
    end;
    WordDocument1.Disconnect;
  finally
    WordApplication1.Disconnect;
  end;
end;


Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
lexmy3 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 140

WIN 2000 Prof. + Office 2000
Delphi 5 Enterprise
BeitragVerfasst: Fr 27.01.06 12:02 
Hey, Supper es funzt.

Danke für deine Reaktion. Habe nur noch eine Frage:

Nach dem Ausführen des Programms wird das Referenz Document schreibgeschützt. Und Ich kann dann nichts dran verändern. Wie hebe ich den Schereibschutz auf?

Mfg

lexmy3
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Fr 27.01.06 17:53 
weil es noch offen ist?
word müßte für diesen code nicht gestartet sein, es würde im Hintergrund laufen. Du kannst das doc schließen (wordoc.close(...) mit parameter savechanges false) oder bringst es mit wordapp.visisble zum vorschein.

Mfg frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)