Excel Data to Word Template

Franztestet

New Member
Joined
Oct 20, 2021
Messages
6
Office Version
  1. 2019
  2. 2016
Platform
  1. 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!
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
3,017
This seems like it should work. You need to save your .docx file to the Custom Office Templates folder (in documents folder) as a Word template. Your XL wb needs to be in your documents folder for this code to work. The new files will be created in your documents folder. HTH. Dave
Code:
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, Flag As Boolean

Const wdgotobookmark As Long = -1

'open Word application
On Error Resume Next
Set wd = GetObject(, "word.application")
If Err.Number <> 0 Then
On Error GoTo 0
Set wd = CreateObject("Word.Application")
End If
On Error GoTo ErFix
'ab Spalte 8
Set sh = ThisWorkbook.Sheets("sheet1")
iRow = 1 'row in which data starts from in database
'aus Excel Sheet  "Overview" ab Feld B8
WorkOrder = LCase(Worksheets("sheet1").Range("B1").Value)
Do While sh.Range("A" & iRow).Value <> ""
If WorkOrder = LCase(CStr(sh.Range("C" & iRow).Value)) Then 'condition
'******XL wb in Documents
Set wdDOC = wd.Documents.Add(Template:=ThisWorkbook.Path & "\Custom Office Templates\test.dotx", _
                                   NewTemplate:=False, DocumentType:=0)
With wdDOC
If .Bookmarks.Exists("DN") = True Then
.Goto what:=wdgotobookmark, Name:="DN"
wd.Selection.TypeText CStr(sh.Range("B" & iRow).Value)
Else
MsgBox "No DN bookmark"
.Close SaveChanges:=False
GoTo ErFix
End If
If .Bookmarks.Exists("DNa") = True Then
.Goto what:=wdgotobookmark, Name:="DNa"
wd.Selection.TypeText CStr(sh.Range("C" & iRow).Value)
.SaveAs2 ("C:\yourfoldername\" & CStr(sh.Range("B" & iRow).Value) & ".docx")
Flag = True
Else
MsgBox "No DNa bookmark"
.Close SaveChanges:=False
GoTo ErFix
End If

.Close SaveChanges:=False
Exit Do
End With
End If
iRow = iRow + 1
Loop

ErFix:
Set wdDOC = Nothing
wd.Quit
Set wd = Nothing
If Err.Number <> 0 Then
MsgBox "error"
Else
If Flag Then
MsgBox "Word document created"
End If
End If
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,459
Messages
5,831,760
Members
430,088
Latest member
meagerd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top