A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Hi,
SAMPLE
open word template and create new word documents (docx)
from data in excel file in activesheet
results
all docx files on desktop in a folder
pic1/template
.
pic2/data in excel
.
.
result
file word docx <Kostas>
pic/ folder on desktop
.
===============================
vba code
[Update-1]
Option Explicit ' << START VBA
Dim ws As Worksheet
Dim wdApp As Object
Dim wdDoc As Object
Dim newFd, sPath
Dim x, y
'
Sub Open_wd_Template_Update()
'## 29-09-2024 ##
Dim ws As Worksheet
Set ws = ActiveSheet 'data in active sheet / row2-row4
Application.ScreenUpdating = False
'
'1/ create a folder on desktop <fd-update-wd>
newFd = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\fd-update-wd" '<< create a new Folder
If Dir(newFd, vbDirectory) = Empty Then MkDir newFd
'
'2/ change template path BUT NOT IN ONEDRIVE
sPath = ThisWorkbook.Path & "\template1-wd.dotx" '<< change name
'
'3/ open word app
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
Else
Set wdApp = GetObject(, "Word.Application")
End If
wdApp.Visible = True
'
'4/ loop in excel file
For x = 2 To 4
Set wdDoc = wdApp.Documents.Add(Template:=sPath, NewTemplate:=False, DocumentType:=0)
'
'add date today
With wdDoc.Content.Find
.Text = "<<date>>" '<< find what
.Replacement.Text = Format(Date, "dd/mm/yyyy") '<< replace with
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
'
'update table in word doc row2/ 5 cells
For y = 1 To 5
wdDoc.Tables(1).Cell(2, y).Range.Text = ws.Cells(x, y).Value 'name
Next y
'
'4a/ save word-doc as docx on desktop in fd <fd-update-wd>
wdDoc.SaveAs newFd & "" & ws.Cells(x, 1).Value, FileFormat:=wdFormatXMLDocument
wdDoc.Close False
Next x
wdApp.Quit
Application.ScreenUpdating = True
Set wdApp = Nothing
Set wdDoc = Nothing
End Sub ' << END VBA