AW: Tabelle formatiert an Word übergeben
12.01.2022 12:57:23
Jenny
Hallo Dieter,
erstmal tausend DANK!!! Das funktioniert schon mal super! :)
Ich habe nun versucht es - wie du geschrieben hast - für die anderen Tabellen analog zu machen.
Hier bin ich auf 2 Probleme gestoßen:
1. Wenn ich den Teil Study Preparation kopiere und für Project Management anpasse (habe ein neues Dim wdTab2 festgelegt damit er die PM Tabelle in die Tabelle 2 im Word überträgt), dann gibt er mir in der zweiten Tabelle auch Study Preparation und PM aus ... hängt das mit "xlWks.Colums.("A")" zusammen?
2. Ich möchte gerne, dass wenn z.B. die Tabelle Study Preparation nicht existiert, dass er mit trotzdem die PM Tabelle generiert.
Wäre es möglich, dass du mir hier nochmal weiterhilfst?
Ganz lieben Dank im Voraus
hier der Code, wie ich ihn um PM erweitert habe
Sub AngebotErstellen()
Dim pfad As String
Dim strDot As String
Dim suchErgebnis As Range
Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim wdTab As Object 'Word.Table(1)
Dim wdTab2 As Object 'Word.Table(2) 'neu für PM Tabelle
Dim wdZeile As Long
Dim xlWks As Worksheet
Dim xlZeile As Long
Dim xlZeile_L As Long
pfad = ThisWorkbook.Path & "\"
strDot = pfad & "Jenny.dotx"
' "Jenny.dotx" enthält eine Tabelle mit nur einer Zeile und 3 Spalten
Set wdApp = VBA.CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add(Template:=strDot)
Set wdTab = wdDoc.Tables(1)
' Spaltenbreiten einstellen
wdTab.Columns(1).Width = wdApp.CentimetersToPoints(10.49)
wdTab.Columns(2).Width = wdApp.CentimetersToPoints(2)
wdTab.Columns(3).Width = wdApp.CentimetersToPoints(3.15)
Set xlWks = ThisWorkbook.Worksheets("Costs detailed")
Set suchErgebnis = xlWks.Columns("A").Find(What:="Total Study Preparation", _
LookIn:=xlValues, _
LookAt:=xlWhole)
If suchErgebnis Is Nothing Then
MsgBox """Total Study Preparation""" & " nicht gefunden"
Exit Sub
End If
xlZeile_L = suchErgebnis.Row
With xlWks
With wdTab
For xlZeile = 2 To xlZeile_L
If xlZeile > 2 Then
.Rows.Add
End If
wdZeile = .Rows.Count
.Cell(wdZeile, 1).Range.Text = xlWks.Cells(xlZeile, 1).Text
.Cell(wdZeile, 2).Range.Text = xlWks.Cells(xlZeile, 3).Text
.Cell(wdZeile, 3).Range.Text = xlWks.Cells(xlZeile, 4).Text
If xlZeile > 2 Then
If xlWks.Cells(xlZeile, "A").Font.Bold Then
If Left$(xlWks.Cells(xlZeile, "A"), 6) "Total " Then
' Überschritfszeile
With wdTab.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(166, 166, 166) ' dunkelgrau
.Range.Font.Bold = True
End With
Else
' Summenzeile
With wdTab.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(217, 217, 217) ' hellgrau
.Range.Font.Bold = True
End With
End If
Else
' Datenzeile
With wdTab.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(195, 222, 209) ' türkis
.Range.Font.Bold = False
End With
End If
End If
Next xlZeile
End With
End With
' 1. Zeile formatieren
With wdTab.Rows(1)
.Shading.BackgroundPatternColor = RGB(0, 56, 106) ' Dunkelblau
.Range.Font.Bold = True
.Range.Font.ColorIndex = 8 ' 8 = wdWhite
.Range.ParagraphFormat.Alignment = 1 ' 1 = wdAlignParagraphCenter
End With
' Letzte Zeile formatieren
With wdTab.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(0, 56, 106) ' Dunkelblau
.Range.Font.Bold = True
.Range.Font.ColorIndex = 8 ' 8 = wdWhite
End With
'PM
Set wdTab2 = wdDoc.Tables(2)
' Spaltenbreiten einstellen
wdTab2.Columns(1).Width = wdApp.CentimetersToPoints(10.49)
wdTab2.Columns(2).Width = wdApp.CentimetersToPoints(2)
wdTab2.Columns(3).Width = wdApp.CentimetersToPoints(3.15)
Set xlWks = ThisWorkbook.Worksheets("Costs detailed")
Set suchErgebnis = xlWks.Columns("A").Find(What:="Total Project Management", _
LookIn:=xlValues, _
LookAt:=xlWhole)
If suchErgebnis Is Nothing Then
MsgBox """Total Project Management""" & " nicht gefunden"
Exit Sub
End If
xlZeile_L = suchErgebnis.Row
With xlWks
With wdTab2
For xlZeile = 2 To xlZeile_L
If xlZeile > 2 Then
.Rows.Add
End If
wdZeile = .Rows.Count
.Cell(wdZeile, 1).Range.Text = xlWks.Cells(xlZeile, 1).Text
.Cell(wdZeile, 2).Range.Text = xlWks.Cells(xlZeile, 3).Text
.Cell(wdZeile, 3).Range.Text = xlWks.Cells(xlZeile, 4).Text
If xlZeile > 2 Then
If xlWks.Cells(xlZeile, "A").Font.Bold Then
If Left$(xlWks.Cells(xlZeile, "A"), 6) "Total " Then
' Überschritfszeile
With wdTab2.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(166, 166, 166) ' dunkelgrau
.Range.Font.Bold = True
End With
Else
' Summenzeile
With wdTab2.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(217, 217, 217) ' hellgrau
.Range.Font.Bold = True
End With
End If
Else
' Datenzeile
With wdTab2.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(195, 222, 209) ' türkis
.Range.Font.Bold = False
End With
End If
End If
Next xlZeile
End With
End With
' 1. Zeile formatieren
With wdTab2.Rows(1)
.Shading.BackgroundPatternColor = RGB(0, 56, 106) ' Dunkelblau
.Range.Font.Bold = True
.Range.Font.ColorIndex = 8 ' 8 = wdWhite
.Range.ParagraphFormat.Alignment = 1 ' 1 = wdAlignParagraphCenter
End With
' Letzte Zeile formatieren
With wdTab2.Rows(wdZeile)
.Shading.BackgroundPatternColor = RGB(0, 56, 106) ' Dunkelblau
.Range.Font.Bold = True
.Range.Font.ColorIndex = 8 ' 8 = wdWhite
End With
'PM Ende
wdDoc.SaveAs2 Filename:=pfad & "Jenny.docx"
wdDoc.Close
wdApp.Quit
Set wdApp = Nothing
MsgBox "Fertig!", vbInformation, "Übertragung in Word-Vorlage"
End Sub
LG Jenny