VBA - Excel/Word: Probleme mit SaveAs Methode
16.12.2014 13:44:24
Marc
ich kämpfe nun schon seit einiger Zeit mit folgendem Problem:
Ich habe ein Excel-Mappe, aus der ich per VBA eine Word-Vorlage öffne und mit Daten fülle. Dieses Word-Dokument soll nun unter einem neuen Namen abgespeichert werden. Dafür nutze ich die SaveAs Methode - die übrigens nur mit Excel wunderbar funktioniert. Anstatt das Dokument zu speichern, bekomme ich nur den "Speichern Unter" Dialog angezeigt. Für ein Dokument mag das ja ok sein. Allerdings generiert mein Code die Word-Dokumente in Abhängigkeit der Zeilenanzahl, so dass diese zwingend automatisch gespeichert werden müssen.
Hier mein Code (ist bisher haupts. zu Testzwecken, daher nicht wirklich aufgeräumt)
Sub Beschlussvorlagen_generieren()
Dim Word As Object
Dim WinDoc As Object
Dim wbQuelldatei As Workbook
Dim strZielDatei As String
Set wbQuelldatei = ActiveWorkbook
strZielDatei = ThisWorkbook.Path & "\" & "Vorlagen" & "\" & "Beschlussvorlage_FB-V.dotx"
Dim x As Integer
Dim y As Long
Dim topNummer As Integer
Dim topThema As String
Dim topBeschreibung As String
Dim topBegründung As String
Dim eventName As String
Dim eventDatumStart As String
Dim eventOrt As String
Application.ScreenUpdating = False
wbQuelldatei.Activate
Sheets("Tagesordnungspunkte").Select
Cells(1, 2).Select
x = Selection.CurrentRegion.Rows.Count
MsgBox (x)
Set Word = CreateObject("Word.Application")
Word.Visible = True: Word.WindowState = wdWindowStateMaximize
Word.Activate
With Word
For y = 2 To x
topNummer = wbQuelldatei.Sheets("Tagesordnungspunkte").Cells(y, 1)
topThema = wbQuelldatei.Sheets("Tagesordnungspunkte").Cells(y, 2)
topBeschreibung = wbQuelldatei.Sheets("Tagesordnungspunkte").Cells(y, 3)
topBegründung = wbQuelldatei.Sheets("Tagesordnungspunkte").Cells(y, 4)
eventName = wbQuelldatei.Sheets("Startseite").Cells(3, 4)
eventDatumStart = wbQuelldatei.Sheets("Startseite").Cells(7, 4)
eventOrt = wbQuelldatei.Sheets("Startseite").Cells(5, 4)
Set WinDoc = Word.Documents.Add(strZielDatei)
.ActiveDocument.FormFields("docVeranstaltung").Range.Text = eventName
.ActiveDocument.FormFields("docDatumStart").Range.Text = eventDatumStart
.ActiveDocument.FormFields("docOrt").Range.Text = eventOrt
.ActiveDocument.FormFields("docTOP").Range.Text = topNummer
.ActiveDocument.FormFields("docTOPThema").Range.Text = topThema
.ActiveDocument.FormFields("docBeschreibung").Range.Text = topBeschreibung
.ActiveDocument.FormFields("docBegründung").Range.Text = topBegründung
Application.DisplayAlerts = False
WinDoc.SaveAs Filename:=ThisWorkbook.Path & "\" & "Dokumente" & "\" & " _
Beschlussvorlagen" & "\" & topNummer & "-" & topThema & ".docx"
WinDoc.Quit
Application.DisplayAlerts = True
Next y
End With
Worksheets("Startseite").Select
End Sub
Selbst das hier funktioniert nicht, wenn ich es direkt aus Wird aufrufe:ActiveDocument.SaveAs FileName:="C:\Temp\Test.doc"
Hat jemand zufällig eine Lösung parat?
Vielen Dank schon mal,
Marc