Franztestet
New Member
- Joined
- Oct 20, 2021
- Messages
- 6
- Office Version
- 2019
- 2016
- Platform
- Windows
Hi,
I would like to run a Word Template with a range of excel data. It should copy the data to word if the cell in A is the same then cell d1 and that criteria for every line in the excel table. After that it should save the word with a name which is in cell b (for every single line).
This is how my code looks right now. It somehow doesn't work and i have no ide what to change.
Option Explicit
Sub zwanzigsterzehnter()
Dim wd As Object
Dim wdDOC As Object
Dim iRow As Long
Dim PercentageScore As Variant
Dim sh As Worksheet
Dim myValue As Variant
Dim WorkOrder As String
Dim wdgotobookmark As Object
'aus Excel Sheet „Overview“ ab Feld B8
WorkOrder = Worksheets("Overview").Range("B8").Value
'ab Spalte 8
Set sh = ThisWorkbook.Sheets("Overview")
iRow = 8 'row in which data starts from in database
Do While sh.Range("A" & iRow).Value <> "" 'alle Zeilen die nicht leer sind und Zelle A gleich Wert in D1 entspricht
If WorkOrder = sh.Range("D1" & iRow).Value Then
'Word Vorlage öffnen
Set wdDOC = wd.Documents.Add("C:\Pfad\Vorlage.docx")
‚Word sichtbar lassen
wd.Visible = True
'Textmarken in Word einfügen
wd.Selection.GoTo what:=wdgotobookmark, NAME:="DN"
wd.Selection.TypeText Text:=sh.Range("B" & iRow).Value
wd.Selection.GoTo what:=wdgotobookmark, NAME:="DNa"
wd.Selection.TypeText Text:=sh.Range("C" & iRow).Value
'Word mit neuem Namen aus Zeile B1 speichern
ActiveDocument.SaveAs2 (ThisWorkbook.Path & "C:\Pfad\" & sh.Range("B1" & iRow).Value & ".docx")
Exit Do
End If
iRow = iRow + 1
Loop
MsgBox ("Word Vorlage erstellt")
End Sub
Thanks for your help!
I would like to run a Word Template with a range of excel data. It should copy the data to word if the cell in A is the same then cell d1 and that criteria for every line in the excel table. After that it should save the word with a name which is in cell b (for every single line).
This is how my code looks right now. It somehow doesn't work and i have no ide what to change.
Option Explicit
Sub zwanzigsterzehnter()
Dim wd As Object
Dim wdDOC As Object
Dim iRow As Long
Dim PercentageScore As Variant
Dim sh As Worksheet
Dim myValue As Variant
Dim WorkOrder As String
Dim wdgotobookmark As Object
'aus Excel Sheet „Overview“ ab Feld B8
WorkOrder = Worksheets("Overview").Range("B8").Value
'ab Spalte 8
Set sh = ThisWorkbook.Sheets("Overview")
iRow = 8 'row in which data starts from in database
Do While sh.Range("A" & iRow).Value <> "" 'alle Zeilen die nicht leer sind und Zelle A gleich Wert in D1 entspricht
If WorkOrder = sh.Range("D1" & iRow).Value Then
'Word Vorlage öffnen
Set wdDOC = wd.Documents.Add("C:\Pfad\Vorlage.docx")
‚Word sichtbar lassen
wd.Visible = True
'Textmarken in Word einfügen
wd.Selection.GoTo what:=wdgotobookmark, NAME:="DN"
wd.Selection.TypeText Text:=sh.Range("B" & iRow).Value
wd.Selection.GoTo what:=wdgotobookmark, NAME:="DNa"
wd.Selection.TypeText Text:=sh.Range("C" & iRow).Value
'Word mit neuem Namen aus Zeile B1 speichern
ActiveDocument.SaveAs2 (ThisWorkbook.Path & "C:\Pfad\" & sh.Range("B1" & iRow).Value & ".docx")
Exit Do
End If
iRow = iRow + 1
Loop
MsgBox ("Word Vorlage erstellt")
End Sub
Thanks for your help!