Autor Beitrag
BLUE-Design
Hält's aus hier
Beiträge: 7

Windows Vista
Delphi 7 Pers.
BeitragVerfasst: Fr 16.03.07 11:36 
Moin zusammen

Möchte mittels Ole Automation ein bestehendes Word Dokument vervollständigen. Dazu verwende ich Textmarken. Aber komischerweise wird mein Text nicht an der entsprechender Stelle eingefügt, sondern einfach am Anfang des Dokuments. Was stimmt da nicht?

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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, StdCtrls;

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

var
  Form1: TForm1;
      
implementation
uses ComObj;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var Word: variant;
    Seletion, wdGoToBookmark, Textmarke1: OleVariant;

begin
  screen.Cursor := crHourglass;
  try
    // Es wird zuerst versucht, eine laufende Word-Instanz zu verwenden
    word := GetActiveOleObject('Word.Application');
  except
    try
      // Läuft Word noch nicht, wird eine neue Verbindung aufgebaut
      Word := CreateOleObject('Word.Application');
    except
      // Schlägt sie fehl (Word nicht installiert), gibt es eine Fehlermeldung
      ShowMessage('Microsoft Word kann nicht starten.');
      screen.cursor := crDefault;
      exit;
    end;
  end;
  word.visible := true;
  Word.Documents.Add(Template:='F:\Word\test.doc');
  Seletion := Word.Selection;
  try
    Seletion.GoTo(What:=wdGoToBookmark, Name:=Textmarke1);
    Word.Selection.TypeText ('Irgendetwas einzufügen');
  finally
    word := unassigned;
    screen.cursor := crDefault;
  end;
end;

end.
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 16.03.07 20:28 
Hallo

ich habe deinen code jetzt nicht probiert, aber Word will den Namen der Textmarke also
ausblenden Delphi-Quelltext
1:
goto ... name:='Textmarke1'					

da fehlen die '' bei Dir

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
BLUE-Design Threadstarter
Hält's aus hier
Beiträge: 7

Windows Vista
Delphi 7 Pers.
BeitragVerfasst: Sa 17.03.07 17:07 
Hallo Keldorn

Zitat:
ich habe deinen code jetzt nicht probiert, aber Word will den Namen der Textmarke also goto ... name:='Textmarke1'

Ich hab die Textmarke nun mal in die '' gesetzt, funzt leider auch ned. Komischerweise erhalte ich jetz auch noch folgende Exception:

Im Projekt Projekt1.exe ist eine Exception der Klasse EAccessViolation aufgetreten. Meldung: 'Zugriffsverletzung bei Adresse 00456A57 im Modul 'Project1.exe' . Lesen von Adresse 00000000'. Prozess wurde angehalten. Mit Einzelne Anweisung oder Start fortsetzen.

Hier nochmals meinen überarbeiteten Code:
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, OleServer, StdCtrls, ComCtrls, ExtCtrls;

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

var
  Form1: TForm1;

implementation
uses ComObj;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Word: variant;
    Selection: OleVariant;
    wdGoToBookmark: OleVariant;

begin
  screen.Cursor := crHourglass;
  try
    // Es wird zuerst versucht, eine laufende Word-Instanz zu verwenden
    word := GetActiveOleObject('Word.Application');
  except
    try
      // Läuft Word noch nicht, wird eine neue Verbindung aufgebaut
      Word := CreateOleObject('Word.Application');
    except
      // Schlägt sie fehl (Word nicht installiert), gibt es eine Fehlermeldung
      ShowMessage('Microsoft Word kann nicht starten.');
      screen.cursor := crDefault;
      exit;
    end;
  end;
  word.visible := true;

  Selection := Word.Selection;
  Word.Documents.Add(Template:='J:\Word\test.doc');

  try
    Selection.GoTo(What:=wdGoToBookmark, Name:='textmarke1');
    //Word.Selection.TypeText ('Test');
  finally
    word := unassigned;
    screen.cursor := crDefault;
  end;
  
end;

end.


Hab auch schon andere Funktionen wie Selection.Find etc versucht, funktioniert einfach nix. Langsam denke ich, dass hier etwas mit der "Selection" falsch läuft..
JDKDelphi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 115
Erhaltene Danke: 22

WIN2000, XP, WIN 7 , UNIX, LINUX
Assembler für (Z8x, 68xxx,R6000,Intel), DELPHI 6 Enterprise, MAGIC eDeveloper V9+V10, C++, C#,VB, .NET, zertifizierter iBOLT-Programmierer
BeitragVerfasst: Sa 17.03.07 19:52 
Titel: WORD
Hallo

RANGE-Objekt benutzen

Gruss

_________________
Wo andere aufhören, fange ich erst an..
BLUE-Design Threadstarter
Hält's aus hier
Beiträge: 7

Windows Vista
Delphi 7 Pers.
BeitragVerfasst: Sa 17.03.07 21:31 
Titel: Re: WORD
OK, RANGE hat mich schon mal einen Schritt weitergebracht. Aber komischerweise funktioniert der GoTo Sprung :?

Auf jeden fall hab ich's mit diesem Beitrag hingebracht: www.delphi-forum.de/....php?p=329713#329713

Aber dennoch Danke :)