Inhalt
- Textmarken in Word einfügen und anzeigen lassen
- Letzte Zeile finden
- Word-Bilbiothek (Microsoft Word 16.0 Object Library) aktivieren
- Word-Applikation sichtbar machen
- Word-Datei aus Excel-VBA heraus öffnen
- Pfad einer Datei auslesen und kopieren
- Word-Datei mit Excel-Daten befüllen (Textmarken ersetzen)
- Word-Datei als neue Word-Datei (.docx) abspeichern
- Word-Datei als PDF-Datei (.pdf) abspeichern
- Pfad der aktuellen Arbeitsmappe auslesen
- Direktbereich nutzen
- Programm Schritt-für-Schritt mit F8 durchgehen
- Bild in Word-Datei einfügen
- Bildgröße verändern
- Mehrere Zeilen in VBA auskommentieren
- Makro für aktive Zeile ausführen
- Makro über Icon ausführen
- Seriendruck erstellen
Code
Sub ExportToWord() 'Bibliothek aktivieren Dim wordapp As New Word.Application Dim doc As Word.Document Dim wrdPic As Word.InlineShape Dim Zeile As Long Zeile = ActiveCell.Row 'Word sichtbar machen wordapp.Visible = True 'Word-Datei öffnen Set doc = wordapp.Documents.Open("C:\Users\kaiwe\Desktop\Newsletter-Template.docx") 'Word-Datei mit Excel-Daten befüllen doc.Bookmarks("Firma").Range.Text = Kundendaten.Cells(Zeile, 5).Value doc.Bookmarks("Straße").Range.Text = Kundendaten.Cells(Zeile, 7).Value doc.Bookmarks("Ort").Range.Text = Kundendaten.Cells(Zeile, 8).Value doc.Bookmarks("Land").Range.Text = Kundendaten.Cells(Zeile, 9).Value doc.Bookmarks("Datum").Range.Text = Date doc.Bookmarks("Ansprechpartner").Range.Text = Kundendaten.Cells(Zeile, 6).Value 'Bild einfügen Set wrdPic = doc.Bookmarks("Bild").Range.InlineShapes.AddPicture("C:\Users\kaiwe\Desktop\Beispielbild.jpg") 'wrdPic.ScaleHeight = 10 'wrdPic.ScaleWidth = 10 'Word-Datei abspeichern doc.SaveAs2 ThisWorkbook.Path & "\Newsletter " & Kundendaten.Cells(Zeile, 4).Value & ".docx" 'Word-Datei als PDF abspeichern doc.ExportAsFixedFormat ThisWorkbook.Path & "\Newsletter " & Kundendaten.Cells(Zeile, 4).Value & ".pdf", wdExportFormatPDF 'Word-Datei schließen doc.Close SaveChanges:=False 'Word-Applikation schließen wordapp.Quit End Sub Sub ExportToWord_Seriendruck() 'Bibliothek aktivieren Dim wordapp As New Word.Application Dim doc As Word.Document Dim wrdPic As Word.InlineShape Dim Zeile As Long 'Word sichtbar machen wordapp.Visible = True For Zeile = 11 To Kundendaten.Cells(Rows.Count, 4).End(xlUp).Row 'Word-Datei öffnen Set doc = wordapp.Documents.Open("C:\Users\kaiwe\Desktop\Newsletter-Template.docx") 'Word-Datei mit Excel-Daten befüllen doc.Bookmarks("Firma").Range.Text = Kundendaten.Cells(Zeile, 5).Value doc.Bookmarks("Straße").Range.Text = Kundendaten.Cells(Zeile, 7).Value doc.Bookmarks("Ort").Range.Text = Kundendaten.Cells(Zeile, 8).Value doc.Bookmarks("Land").Range.Text = Kundendaten.Cells(Zeile, 9).Value doc.Bookmarks("Datum").Range.Text = Date doc.Bookmarks("Ansprechpartner").Range.Text = Kundendaten.Cells(Zeile, 6).Value 'Bild einfügen Set wrdPic = doc.Bookmarks("Bild").Range.InlineShapes.AddPicture("C:\Users\kaiwe\Desktop\Beispielbild.jpg") 'wrdPic.ScaleHeight = 10 'wrdPic.ScaleWidth = 10 'Word-Datei abspeichern 'doc.SaveAs2 ThisWorkbook.Path & "\Newsletter " & Kundendaten.Cells(Zeile, 4).Value & ".docx" 'Word-Datei als PDF abspeichern doc.ExportAsFixedFormat ThisWorkbook.Path & "\Newsletter " & Kundendaten.Cells(Zeile, 4).Value & ".pdf", wdExportFormatPDF 'Word-Datei schließen doc.Close SaveChanges:=False Next Zeile 'Word-Applikation schließen wordapp.Quit End Sub 'Bereitgestellt von VBATrainer: www.vbatrainer.de End Sub