Danke für deine Verbeserungen. Ich habe Sie eingebaut und habe jetzt auch den Fehler gefunden.
Aus replaceDate = lastDayOfMonth.ToString("dd MMMM yyyy"); wurde replaceDate = lastDayOfMonth.ToString("dd. MMMM yyyy");
Da der String falsch weitergegeben wurde, hat Word das Datum nicht übernommen. Jetzt druckt er mir zB von Jänner bis Juni alles richtig aus. Zumindest als PDF. Am Drucker muss ich es testen, ob irgendein Fehler gibt.
public void LastDate2()
{
string filePath = @"C:\EtikettenVorlage.docm";
string searchText = "LOT 15";
string replaceText = txtbxStart.Text;
string replaceEndString = txtbxStop.Text;
string searchDate = "31. August 2026";
int Seiten = Convert.ToInt32(txtbxAnzahl.Text);
string replaceDate = dateTimePicker.Text;
Word.Application wordApp = null;
Word.Document doc = null;
try
{
// 1. Word Anwendung starten
wordApp = new Word.Application();
wordApp.Visible = true;
progressBar.Value = 10;
// 2. Dokument öffnen
object file = filePath;
object nul = Type.Missing;
doc = wordApp.Documents.Open(ref file, ref nul, ref nul,
ref nul, ref nul, ref nul, ref nul, ref nul,
ref nul, ref nul, ref nul, ref nul, ref nul,
ref nul, ref nul, ref nul);
Statuslabel.Text = "Dokument wird geöffnet...";
progressBar.Value = 20;
doc.Application.Run("AutoExec");
DateTime startDate = dateTimePicker.Value;
DateTime endDate = startDate.AddMonths(Convert.ToInt32(txtbxStop.Text));
// Iteration durch den Datumsbereich
for (DateTime date = startDate; date <= endDate; date = date.AddMonths(1))
{
// Berechne den letzten Tag des aktuellen Monats
DateTime lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
replaceDate = lastDayOfMonth.ToString("dd. MMMM yyyy");
replaceText = "0";
// 3. Gesamten Inhalt auswählen
doc.Content.Select();
Statuslabel.Text = "Dokument wird gelesen...";
progressBar.Value = 30;
// 4. Suchen und Ersetzen für LOT Wert
Word.Find findObject = wordApp.Selection.Find;
findObject.ClearFormatting();
findObject.Text = searchText;
findObject.Replacement.ClearFormatting();
findObject.Replacement.Text = "LOT 15";
Statuslabel.Text = "LOT wird gesucht...";
progressBar.Value = 40;
// Replace-Parameter richtig setzen für LOT Wert
findObject.Execute(
FindText: searchText,
ReplaceWith: "",
Replace: Word.WdReplace.wdReplaceAll);
Statuslabel.Text = "LOT wird gesucht und Entfernt...";
progressBar.Value = 50;
//ReplaceDatum
Word.Find findDate = wordApp.Selection.Find;
findDate.ClearFormatting();
findDate.Text = searchDate;
findDate.Replacement.ClearFormatting();
findDate.Replacement.Text = replaceDate;
Statuslabel.Text = "Datum wird gesucht...";
progressBar.Value = 60;
// Replace-Parameter richtig setzen
findDate.Execute(
FindText: searchDate,
ReplaceWith: replaceDate,
Replace: Word.WdReplace.wdReplaceAll);
Statuslabel.Text = "Datum wird gesucht und Ersetzt...";
progressBar.Value = 70;
// 5. Dokument speichern und schließen
wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
TimeSpan time = new TimeSpan(800);
doc.PrintOut(20, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Seiten);
Statuslabel.Text = "Es wird gedruckt...";
progressBar.Value = 80;
Word.Find findDateBack = wordApp.Selection.Find;
findDate.ClearFormatting();
findDate.Text = replaceDate;
findDate.Replacement.ClearFormatting();
findDate.Replacement.Text = searchDate;
Statuslabel.Text = "Datum zurücksetzen...";
progressBar.Value = 60;
findDateBack.Execute(
FindText: replaceDate,
ReplaceWith: searchDate,
Replace: Word.WdReplace.wdReplaceAll);
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(SaveChanges: Word.WdSaveOptions.wdDoNotSaveChanges);
wordApp.Quit(saveChanges, ref nul, ref nul);
progressBar.Value = 90;
Statuslabel.Text = "Fertig";
progressBar.Value = 100;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Habe es korrigiert und es läuft bis auf einen Fehler:
Wenn das erste mal das Datum ersetzt wird, funktioniert es beim 2. mal nicht mehr.
Also wenn das Datum auf Jänner gesetzt wird, wird beim nächsten durchlauf die Variable auf Februar gesetzt, aber es wird im Word Dokument nicht ersetzt.
public void LastDate2()
{
int year = dateTimePicker.Value.Year;
int month = dateTimePicker.Value.Month;
int day = dateTimePicker.Value.Day;
string filePath = @"C:\EtikettenVorlage.docm";
string searchText = "LOT 15";
string replaceText = txtbxStart.Text;
string replaceEndString = txtbxStop.Text;
string searchDate = "31. August 2026";
int Seiten = Convert.ToInt16(txtbxAnzahl.Text);
string replaceDate = dateTimePicker.Text;
Word.Application wordApp = null;
Word.Document doc = null;
try
{
// 1. Word Anwendung starten
wordApp = new Word.Application();
wordApp.Visible = true;
progressBar.Value = 10;
// 2. Dokument öffnen
object file = filePath;
object nul = Type.Missing;
doc = wordApp.Documents.Open(ref file, ref nul, ref nul,
ref nul, ref nul, ref nul, ref nul, ref nul,
ref nul, ref nul, ref nul, ref nul, ref nul,
ref nul, ref nul, ref nul);
Statuslabel.Text = "Dokument wird geöffnet...";
progressBar.Value = 20;
doc.Application.Run("AutoExec");
DateTime startDate = new DateTime(year, month, day);
DateTime endDate = new DateTime(year, month + Convert.ToInt32(Seiten), day); //Seiten=Anzahl der Monate
// Iteration durch den Datumsbereich
for (DateTime date = startDate; date <= endDate; date = date.AddMonths(1))
{
// Berechne den letzten Tag des aktuellen Monats
DateTime lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
dateTimePicker.Value = DateTime.Parse(lastDayOfMonth.ToString("dd MMMM yyyy"));
replaceDate = dateTimePicker.Text;
replaceText = "0";
// 3. Gesamten Inhalt auswählen
doc.Content.Select();
Statuslabel.Text = "Dokument wird gelesen...";
progressBar.Value = 30;
// 4. Suchen und Ersetzen für LOT Wert
Word.Find findObject = wordApp.Selection.Find;
findObject.ClearFormatting();
findObject.Text = searchText;
findObject.Replacement.ClearFormatting();
findObject.Replacement.Text = "LOT 15";
Statuslabel.Text = "LOT wird gesucht...";
progressBar.Value = 40;
// Replace-Parameter richtig setzen für LOT Wert
findObject.Execute(
FindText: searchText,
ReplaceWith: "",
Replace: Word.WdReplace.wdReplaceAll);
Statuslabel.Text = "LOT wird gesucht und Entfernt...";
progressBar.Value = 50;
//ReplaceDatum
Word.Find findDate = wordApp.Selection.Find;
findDate.ClearFormatting();
findDate.Text = searchDate;
findDate.Replacement.ClearFormatting();
findDate.Replacement.Text = replaceDate;
Statuslabel.Text = "Datum wird gesucht...";
progressBar.Value = 60;
// Replace-Parameter richtig setzen
findDate.Execute(
FindText: searchDate,
ReplaceWith: replaceDate,
Replace: Word.WdReplace.wdReplaceAll);
Statuslabel.Text = "Datum wird gesucht und Ersetzt...";
progressBar.Value = 70;
// 5. Dokument speichern und schließen
wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
TimeSpan time = new TimeSpan(800);
//doc.Application.Run("AutoExec"); //Deaktiviert die Meldung und Druckt das Dokument aus
doc.PrintOut(20, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Seiten); //Seiten ist die Anzahl wie viele gedruckt wird
Statuslabel.Text = "Es wird gedruckt...";
progressBar.Value = 80;
TimeSpan time2 = new TimeSpan(800);
searchDate = "31. August 2026";
//Setzt den aktuelle Datum auf August 26 zurück, damit die Suche wieder von vorne beginnen kann - HIER ist auch irgendwie der Fehler. Das Datum wird in Word nicht zurückgesetzt
Word.Find findDateBack = wordApp.Selection.Find;
findDate.ClearFormatting();
findDate.Text = replaceDate;
findDate.Replacement.ClearFormatting();
findDate.Replacement.Text = searchDate;
Statuslabel.Text = "Datum wird gesucht...";
progressBar.Value = 60;
findDateBack.Execute(
FindText: replaceDate,
ReplaceWith: searchDate,
Replace: Word.WdReplace.wdReplaceAll);
//}
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//doc.SaveAs2("Print.docx", ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul, ref nul);
//doc.Close(SaveChanges: Word.WdSaveOptions.wdDoNotSaveChanges);
//wordApp.Quit(saveChanges, ref nul, ref nul);
progressBar.Value = 90;
Statuslabel.Text = "Fertig";
progressBar.Value = 100;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Bin da etwas weiter, aber Microsoft Word sagt, dass das Format falsch ist:
public void LastDate()
{
string replaceDate = dateTimePicker.Value.ToString();
int year = Convert.ToInt32(replaceDate.Substring(6,4));
int month = Convert.ToInt32(replaceDate.Substring(3,2));
int day = Convert.ToInt32(replaceDate.Substring(0, 2));
//
string filePath = @"C:\EtikettenVorlage.docm";
string searchString = "LOT 15";
string replaceString = txtbxStart.Text;
string replaceEndString = txtbxStop.Text;
string searchDate = "31. August 2026";
int Seiten = Convert.ToInt16(txtbxAnzahl.Text);
//string replaceDate = dateTimePicker.Text;
//
string temporarydate = "";
DateTime startDate = new DateTime(year, month, day);// 2025, 06,23);
DateTime endDate = new DateTime(year, month+2, day);
// Iteration durch den Datumsbereich
for (DateTime date = startDate; date <= endDate; date = date.AddMonths(1))
{
// Berechne den letzten Tag des aktuellen Monats
DateTime lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
MessageBox.Show(lastDayOfMonth.ToString("dd.MMMM yyyy"));
dateTimePicker.Value = lastDayOfMonth;
temporarydate = Convert.ToString(dateTimePicker.Value.ToString("dd.MMMM yyyy"));
ReplaceTextInWordDocument3(filePath, searchString, replaceString, searchDate, temporarydate, Seiten);
}
Ich möchte meinen code um eine Funktion erweitern, der mir den letzten Tag im Monat anzeigt
lastDayOfMonth.ToString("dd.MM.yyyy");
ist im Prinzip ein long(DateTIme). Ich brauche aber ein string, damit ich es an die Methode weiterleiten kann.
Wie kann ich DateTime in ein String umwandeln? MitConvert.ToString(lastDayOfMonth.ToString("dd.MM.yyyy"));
bekomme ich bei temporarydate nicht das Datum sondern nur ""
public void LastDate()
{
string replaceDate = dateTimePicker.Value.ToString();
int year = Convert.ToInt32(replaceDate.Substring(6,4));
int month = Convert.ToInt32(replaceDate.Substring(3,2));
int day = Convert.ToInt32(replaceDate.Substring(0, 2));
//
string filePath = @"C:\Vorlage.docm";
string searchString = "LOT 15";
string replaceString = txtbxStart.Text;
string replaceEndString = txtbxStop.Text;
string searchDate = "31. August 2026";
int Seiten = Convert.ToInt16(txtbxAnzahl.Text);
//string replaceDate = dateTimePicker.Text;
//
string temporarydate = "";
DateTime startDate = new DateTime(year, month, day);// 2025, 06,23);
DateTime endDate = new DateTime(year, month+2, day);
// Iteration durch den Datumsbereich
for (DateTime date = startDate; date <= endDate; date = date.AddMonths(1))
{
// Berechne den letzten Tag des aktuellen Monats
DateTime lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
MessageBox.Show(lastDayOfMonth.ToString("dd.MM.yyyy"));
temporarydate = lastDayOfMonth.ToString("dd.MM.yyyy");
//MessageBox.Show($"Letzter Tag im Monat {date.ToString("MMMM yyyy")}: {lastDayOfMonth.ToString("dd.MM.yyyy")}");
ReplaceTextInWordDocument3(filePath, searchString, replaceString, searchDate, temporarydate, Seiten);
}
Macht natürlich mehr Sinn, alles über C# zu steuern, aber ich bin da eher ein Laie in C# Programmieren.
Aber zumindest läuft es schon mal fürs Erste.
Ich überlege jetzt noch, dass ich vielleicht nur 1 Word Dokument öffne, es bearbeite drucke und dann wieder bearbeite. Aktuell wird ja für jede Version ein neues Word Dokument geöffnet, bearbeitet und dann gedruckt und wieder geschlossen, was wohl ineffizent ist. Aber der Druckauftrag ist sowieso zu langsam von Windows wie ich finde.
Danke, ich werde mir den Process explorer anschauen, ob ich da was rausfinden kann. kenne mich da 0 aus.
Ich bin auf die Idee gekommen ein Macro im Word zu schreiben, der mir die Warnung ignoriert und siehe da, die Warnung wird ignoriert und es wird gedruckt. ABER halt nur wenn ich das Makro ausführe im Word.
Kann man in C# ein Makro nach dem Öffnen aktivieren?
Sub AutoExec()
' Store current Background Printing setting.
Dim x As Boolean
x = Options.PrintBackground
' Turn off error messaging and Background Printing.
Application.DisplayAlerts = wdAlertsNone
Options.PrintBackground = False
' Print the active document.
ActiveDocument.PrintOut
' Turn on error messaging and restore Background Printing
' to original setting.
'Application.DisplayAlerts = wdAlertsAll
'Options.PrintBackground = x
End Sub
Ich habe die Seitenränder im Word von 0,42 (oben und unten) auf 0,1 cm reduziert und er druckt es ohne was abzuschneiden.
Wie kann ich schauen, welcher Process den Dialog aufmacht? Es wäre wohl auch eine Option, wenn ich zumindest das Fenster mit den Seitenränder sehe, wo ich zumindest JA drucken kann. Somit müsste ich halt nur mehr für jeden Ausdruck "Ja" klicken. Der Rest macht mein Code. Spart mir auch einiges an Arbeit.
.
Das Problem ist, dass es sich um Etiketten handelt die auf A4 sind. Da kann ich fast nicht aus, dass ich Richtung "Randlos" drucken möchte.
Das Dokument reizt halt die Ränder komplett aus (was leider nötig ist, weil sehr viel Text in den Etiketten sind). Aber beim Ausdruck wird nichts abgeschnitten oder so.
Wenn ich ein PDF drucke, dann klappt es ohne den Hinweis.
Ich glaube es braucht eine manuelle Eingabe. Wenn ich das Word Dokument direkt öffne und drucken möchte, kommt immer die Warnung "Ihre Seitenränder sind ziemlich schmal. Möglicherweise werden Teile des Inhalts beim Drucken abgeschnitten. Möchten Sie trotzdem drucken?"
Das sehe ich nicht, wenn ich es via C# ausdrucken möchte. Ich glaube er bleibt hier stehen. Wenn ich einen PDF Drucker als Standardwähle, dann funktioniert es Problemlos.
Der Punkt ist eher: Ich möchte zB 10 Seiten mit LOT 1, 10 Seiten mit LOT 2, 10 Seiten mit LOT 3....10 Seiten mit LOT 10.
Das funktioniert ja schon im prinzip mit meinem Code. Aber bei meinem Echten Drucker, hab ich halt diese Warnung "Ihre Seitenränder sind ziemlich schmal. Möglicherweise werden Teile des Inhalts beim Drucken abgeschnitten. Möchten Sie trotzdem drucken?"
Somit druckt es nicht. Wenn ich den PDF Drucker wähle, kommt die Warnung halt nicht von Word und er erstellt die PDFs
etwa so?
wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
TimeSpan time = new TimeSpan(800);
doc.PrintOut(20, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Seiten);
Also aktuell sehe ich, wie mein Code Word öffnet, die Stellen ersetzt und in der Leiste von Word steht halt "Es wird gedruckt". Und es erscheint auch in den Aufträgen vom Drucker, aber sie bleiben "In Warteschlange" und es wird nicht gedruckt.
im Word sehe ich noch eine Meldung:
"Bitte warten sie einen moment, Word muss zuerst das Drucken abschließen." als Infofenster. Wenn ich ein PDF erstelle funktioniert es, da ich ja eine Interaktion habe, wo ich das PDF speichere