chickenfigt1989 - Do 10.01.13 06:48
Titel: Word Ansteuern
Hallo,
Ich habe sowas wie eine Adress Datenbank in der ich die Adressen eingebe die in einer INI Datei gespeichert werden.
Die Adressen kann ich mir in den Programm in einer Listbox ausgeben lassen,
wobei noch die Funktion Serienbrief fehlt.
Einen Normalen Brief mach ich mit folgender Funktion:
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:
| procedure TForm2.Button1Click(Sender: TObject); var Word: OLEVariant;
begin try Word := CreateOleObject('Word.Application'); except ShowMessage('Word kann nicht geöffnet werden!'); Exit; end; Word.Visible := True; Word.Documents.Add; if Word.ActiveWindow.View.SplitSpecial <> 0 then Word.ActiveWindow.Panes[2].Close; if (Word.ActiveWindow.ActivePane.View.type = 1) or (Word.ActiveWindow.ActivePane.View.type = 2) or (Word.ActiveWindow.ActivePane.View.type = 5) then Word.ActiveWindow.ActivePane.View.type := 3; Word.ActiveWindow.ActivePane.View.SeekView := 9;
Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := True; Word.Selection.ParagraphFormat.Alignment := 2; Word.Selection.TypeText(FormatDateTime('dd.mm.yyyy', Now)); Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := True;
if Word.Selection.HeaderFooter.IsHeader = True then Word.ActiveWindow.ActivePane.View.SeekView := 10 else Word.ActiveWindow.ActivePane.View.SeekView := 9;
Word.ActiveWindow.ActivePane.View.SeekView := 0; Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := true; Word.Selection.TypeText(Text := Edit3.Text + ' ' + edit2.Text); Word.Selection.TypeParagraph; Word.Selection.TypeText(Text := edit4.Text); Word.Selection.TypeParagraph;
Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := true;
Word.Selection.TypeText(Text := edit5.Text ); Word.Selection.TypeParagraph; Word.Selection.TypeText(Text := edit6.Text); Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.Font.Name := 'Arial'; Word.Selection.Font.Size := 14; Word.Selection.Font.Bold := True; Word.Selection.TypeText(Text := Combobox1.Text); Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := True; if radiogroup1.ItemIndex = 0 then Word.Selection.TypeText(Text := 'Sehr geehrter Herr ' + edit3.Text+','+#13#10+#13#10) else Word.Selection.TypeText(Text := 'Sehr geehrte Frau ' + edit3.Text+','+#13#10+#13#10) end; |
Jetzt dachte ich mach ich es beim Serienbrief genauso nur setz ich davor noch folgenden Code:
Delphi-Quelltext
1: 2: 3:
| for I:=Listbox1.Items.Count-1 downto 0 do begin if Listbox1.Selected[I] then |
Der Code sieht dann so aus:
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: 84:
| procedure TForm2.Button6Click(Sender: TObject); var I: Integer; Word: OLEVariant; begin
try Word := CreateOleObject('Word.Application'); except ShowMessage('Word kann nicht geöffnet werden!'); Exit; end; for I:=Listbox1.Items.Count-1 downto 0 do begin if Listbox1.Selected[I] then Word.Visible := True; Word.Documents.Add; if Word.ActiveWindow.View.SplitSpecial <> 0 then Word.ActiveWindow.Panes[2].Close; if (Word.ActiveWindow.ActivePane.View.type = 1) or (Word.ActiveWindow.ActivePane.View.type = 2) or (Word.ActiveWindow.ActivePane.View.type = 5) then Word.ActiveWindow.ActivePane.View.type := 3; Word.ActiveWindow.ActivePane.View.SeekView := 9;
Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := True; Word.Selection.ParagraphFormat.Alignment := 2; Word.Selection.TypeText(FormatDateTime('dd.mm.yyyy', Now)); Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := True;
if Word.Selection.HeaderFooter.IsHeader = True then Word.ActiveWindow.ActivePane.View.SeekView := 10 else Word.ActiveWindow.ActivePane.View.SeekView := 9;
Word.ActiveWindow.ActivePane.View.SeekView := 0; Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := true; Word.Selection.TypeText(Text := Edit3.Text + ' ' + edit2.Text); Word.Selection.TypeParagraph; Word.Selection.TypeText(Text := edit4.Text); Word.Selection.TypeParagraph;
Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := true;
Word.Selection.TypeText(Text := edit5.Text ); Word.Selection.TypeParagraph; Word.Selection.TypeText(Text := edit6.Text); Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.Font.Name := 'Arial'; Word.Selection.Font.Size := 14; Word.Selection.Font.Bold := True; Word.Selection.TypeText(Text := Combobox1.Text); Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.TypeParagraph; Word.Selection.Font.Name := 'Times New Roman'; Word.Selection.Font.Size := 12; Word.Selection.Font.Bold := True; if radiogroup1.ItemIndex = 0 then Word.Selection.TypeText(Text := 'Sehr geehrter Herr ' + edit3.Text+','+#13#10+#13#10) else Word.Selection.TypeText(Text := 'Sehr geehrte Frau ' + edit3.Text+','+#13#10+#13#10) end; |
Vom Lösungsweg her denke ich mir das ich die erste Markierte Adresse öffnen muss, drucken und wieder Freigeben und dann die liste der Reihe nach durcharbeiten.
Aber wie genau mach ich das?
Wenn ich jetzt in meiner INI Datei 100 Adressen drin habe, wird 100 Mal Word geöffnet mit nur einer Adresse.
Ich denke mal da fehlt noch die Zuweisung das er die Liste mit den Markierten Adressen der Reihe nach durcharbeiten soll.
Kann ich es mit der Variable [I] machen?
Wenn ja kann mir vllt wer sagen wo die hin muss?
Wenn ich komplett falsch liege, könntet ihr mir dann ein bisschen Hilfestellung geben?
Gruß
Delete - Do 10.01.13 12:29
Wie Bummi bereits angedeutet hatte, ist es am einfachsten und vor allem zielführend, wenn du dir aus den Adressen, denen ein Brief zugesandt werden soll, eine CSV-Datei bastelst. Die sieht dann bei meinem
Adressprogramm [
http://www.pcheil.de/Site/Adress.htm] z.B. so aus:
Quelltext
1: 2: 3: 4: 5: 6:
| AdressAnrede;Anrede;Titel;Institut;Vorname;NachName;Strasse;Ort;Telefon;Fax;Handy;Email;Geburtstag;Anlass; Herrn;Sehr geehrter Herr;;;Charli;Morgen;Neustädter Str. 277;13125 Berlin; ;;;Charli.Morgen@haut.de;;Weihnachtsbrief; Herrn;Sehr geehrter Herr;;;Dalfon;Vaske;Hüsung 102;12359 Berlin; ;;;Dalfon.Vaske@gmx.de;;Weihnachtsbrief; Herrn;Sehr geehrter Herr;;Werbefirma Gnadenlos;Aysen;Ukulani;Borussiastr. 82;12103 Berlin; ;;;Aysen.Ukulani@arcor.de;;Weihnachtsbrief; Herrn;Sehr geehrter Herr;;;Friedrich-Wolfgang;Weill;Benschallee 288;14163 Berlin; ;;;Friedrich-Wolfgang.Weill@arcor.de;;Weihnachtsbrief; Frau;Sehr geehrte Frau;;;Chriso;Sonnentag;Leonorenstr. 133;12247 Berlin; ;;;Chriso.Sonnentag@nox.de;;Weihnachtsbrief; |
Damit das Ganze auch funktioniert, erstellst du dir in WORD eine Dokumentenvorlage mit Serienbrief-Funktion. Dort richtest du mit der Serienbrief-Funktion von WORD und mit der CSV-Datei als Datenbank eine Serienbrief-Vorlage ein. Wenn du diese dann in deinem Program via ShellExecute aufrufst, wird ein neues, nur mit den Vorgaben aus der Dokumentvorlage bestücktes WORD-Dokument geöffnet. Lade dir ruhig einmal das oben verlinkte Programm herunter und probiere es aus, damit du eine Vorstellung davon erhältst, wie das funktioniert.