AW: Neues Worddokument aus Excel heraus erzeugen
16.11.2006 17:11:39
fcs
Hallo GerWas,
nach etwas Bastelarbeit ist eine brauchbare Lösung herasugekommen.
Im VBA-Editor muss du ggf. unter Extras-Veisweise zusätzlich "Microsoft Word xy.z Object Library" als verfügbaren Verweis markieren.
Für das Speichern muss du halt entscheiden ob du die Einzelspeicherung per Dialogfenster oder das Speichern mit Zählnummer einsetzen willst.
Gruß
Franz
Sub worddokument()
Dim doc As Document, wks As Worksheet, Zeile As Long
Set wks = ActiveSheet
'Word vorher manuell starten, synchronisation von Word und Excel sonst irgendwie schwierig
'Word vorher manuell starten, synchronisation von Word und Excel sonst irgendwie schwierig
Application.WindowState = xlMinimized
Application.ActivateMicrosoftApp xlMicrosoftWord
For Zeile = 1 To wks.Cells(wks.Rows.Count, "A").End(xlUp).Row
'Worddokument aus Vorlage neu anlegen
Set doc = Documents.Add(Template:= _
"C:\Vordrucke\Technik\TechnischeBeschreibung.dot" _
, NewTemplate:=False, DocumentType:=0)
'Code zum übertragen der Infos aus dem Excel-File ins Wordfile
With doc
.Bookmarks("Lfd_Nr").Range.Text = wks.Range("A1").Value
.Bookmarks("Tag").Range.Text = wks.Range("B1").Value
.Bookmarks("Zeit").Range.Text = wks.Range("C1").Value
End With
'Worddokument speichern und schließen
doc.Activate
'Speicherndialog anzeigen
doc.Application.Dialogs(wdDialogFileSaveAs).Show
GoTo weiter
'Alternative für Speichern: Dateiname = Text+Zeilennummer
Verzeichnis = "C:\Lokale Daten\Test\"
Datei = "Testdatei" & Format(Zeile, "00") & ".doc"
doc.Application.Options.SavePropertiesPrompt = False 'Dokumenteigenschaften abfragen AUS
doc.SaveAs Filename:=Verzeichnis & Datei, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
False, WritePassword:="", ReadOnlyRecommended:=False
doc.Application.Options.SavePropertiesPrompt = True 'Dokumenteigenschaften abfragen EIN
weiter:
If Zeile = wks.Cells(wks.Rows.Count, "A").End(xlUp).Row Then
doc.Application.WindowState = wdWindowStateMinimize
End If
doc.Close
Next Zeile
Application.WindowState = xlMaximized
End Sub