PeterKom
04-30-2020, 01:14 PM
Attached an example of the script copying data from excel to word.
Please copy both files in one folder, open the excel file and under Templates sheet browse and insert the test.docx file.
Then choose it on the Ponudbe Sheet and press the green button.
The Word templates opens and all the tags are being replaced by the actual data (stored in the row after the company data in the main table, this is normaly hidden for the user).
My wish is to add a picture in each of the 4 tables in the Word Template. Each tabel represents on position (one product).
I have made only table 1 so you can see where I wish the picture to be.
If the user makes a configuration with only one product in Excel (this part is not visible in excel) the other 3 tables are being deleted.
I solved this with help of the user macropod! Thanks again.
So what would be the best way to add a picture.
Is it possible excel just provedes the link (path) to the image file and word shows it instead of the <<Picture>> tag or does the image needs to be copied
from Excel to Word to replace the <<Picture>> tag.
Becasue at the moment all data in Excel that is beeing copied sits in one row adding some pictures there would not be OK.
Maybe i can make a Sheet with all the pictures that i will use, name them and then the user configures the product on a separate sheet (not visible in the test file)
and based on this configuration the correct picture is selected automaticaly. The data from the configuratior is then being copied to the one row in sheet Ponudbe,
where the data is ready to creat the actual offer.
Any help would be appreciated.
Below the code form the attached excel file:
Option Explicit
Sub CreateWordDocuments()
Dim CustRow, CustCol, LastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim VarFormat As String
Dim VarValue As String
Dim WordContent As Word.Range
Dim date_example As String
date_example = Now()
Dim SaveNr As String
Dim str As String
Dim fol As String
With Sheet1
If .Range("B3").Value = Empty Then
MsgBox "Prosim izberi pravo podlogo iz seznama podlog"
.Range("H3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value 'Set Template Row
TemplName = .Range("H3").Value 'Set Template Name
FrDays = .Range("M3").Value 'Set From Days
ToDays = .Range("O3").Value 'Set To Days
DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
'Open Word Template
On Error Resume Next 'If Word is already running
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("E9999").End(xlUp).Row 'Determine Last Row in Table
For CustRow = 8 To LastRow 'ROČNA PONDUBA
If .Range("O" & CustRow).Value = "" And TemplName <> "ROČNA PONUDBA" Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
For CustCol = 4 To 200 'Move Through Columns
If .Cells(4, CustCol).Value = "Splosno" Then VarFormat = "General" Else: VarFormat = .Cells(5, CustCol).Value 'Determine Variable Format
TagName = .Cells(7, CustCol).Value 'Tag Name
TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = Application.WorksheetFunction.Text(TagValue, VarFormat)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With
Next CustCol
Dim Tbl As Long, Rw As Long, StrData As String
With WordDoc
For Tbl = 4 To 2 Step -1
With .Tables(Tbl)
StrData = ""
For Rw = 3 To 12
StrData = StrData & Split(.Cell(Rw, 2).Range.Text, vbCr)(0) '.Bookmarks("Schedule_of_Payments").Range.Tables(1).Delete
Next
If StrData = "" Then .Range.Bookmarks(1).Range.Delete 'If StrData = "" Then .Range.Bookmarks(1).Range.Tables(1).Delete
End With
Next
End With
If TemplName <> "ROČNA PONUDBA" Then
SaveNr = .Range("D" & CustRow).Text
str = "FOR TEST INSERT PATH TO FOLDER WHERE TEMPALTE IS STORED" & .Range("H" & CustRow).Value & " " & SaveNr & "-" & .Range("U" & CustRow).Value & "-" & .Range("R" & CustRow).Value & "\"
fol = Dir(str, vbDirectory)
If fol = "" Then MkDir str
FileName = str & .Range("H" & CustRow).Value & " " & SaveNr & "-" & .Range("U" & CustRow).Value & "-" & .Range("R" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName 'Saves Word document
FileName = str & .Range("H" & CustRow).Value & " " & SaveNr & "-" & .Range("U" & CustRow).Value & "-" & .Range("R" & CustRow).Value & ".pdf" 'Create full filename & Path with current workbook location, Last Name & First Name
On Error Resume Next
Kill (FileName) 'Delete filename with the same name if it exists
On Error GoTo 0
On Error Resume Next
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF 'Exports Word document as PDF in and saves in same folder as Word document
'WordDoc.Close False
End If
.Range("O" & CustRow).Value = "DA" 'pre je bilo to' TemplName 'Template Name
.Range("P" & CustRow).Value = Format(date_example, "dd.mm.yyyy hh:nn")
'.Range("R" & CustRow) = Format(date_example, "yyyy")' 'SEM ODRSTRANIL KER NI POTREBNO, IMAM FORMULO ZA TO, KER JE PODATEK POTREBEN PREJ'
'WordDoc.Close
End If '3 condition met
Next CustRow
'WordApp.Quit 'WordApp.Quit Does not quit Word when testing templates
End With
End Sub
Please copy both files in one folder, open the excel file and under Templates sheet browse and insert the test.docx file.
Then choose it on the Ponudbe Sheet and press the green button.
The Word templates opens and all the tags are being replaced by the actual data (stored in the row after the company data in the main table, this is normaly hidden for the user).
My wish is to add a picture in each of the 4 tables in the Word Template. Each tabel represents on position (one product).
I have made only table 1 so you can see where I wish the picture to be.
If the user makes a configuration with only one product in Excel (this part is not visible in excel) the other 3 tables are being deleted.
I solved this with help of the user macropod! Thanks again.
So what would be the best way to add a picture.
Is it possible excel just provedes the link (path) to the image file and word shows it instead of the <<Picture>> tag or does the image needs to be copied
from Excel to Word to replace the <<Picture>> tag.
Becasue at the moment all data in Excel that is beeing copied sits in one row adding some pictures there would not be OK.
Maybe i can make a Sheet with all the pictures that i will use, name them and then the user configures the product on a separate sheet (not visible in the test file)
and based on this configuration the correct picture is selected automaticaly. The data from the configuratior is then being copied to the one row in sheet Ponudbe,
where the data is ready to creat the actual offer.
Any help would be appreciated.
Below the code form the attached excel file:
Option Explicit
Sub CreateWordDocuments()
Dim CustRow, CustCol, LastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim VarFormat As String
Dim VarValue As String
Dim WordContent As Word.Range
Dim date_example As String
date_example = Now()
Dim SaveNr As String
Dim str As String
Dim fol As String
With Sheet1
If .Range("B3").Value = Empty Then
MsgBox "Prosim izberi pravo podlogo iz seznama podlog"
.Range("H3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value 'Set Template Row
TemplName = .Range("H3").Value 'Set Template Name
FrDays = .Range("M3").Value 'Set From Days
ToDays = .Range("O3").Value 'Set To Days
DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
'Open Word Template
On Error Resume Next 'If Word is already running
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("E9999").End(xlUp).Row 'Determine Last Row in Table
For CustRow = 8 To LastRow 'ROČNA PONDUBA
If .Range("O" & CustRow).Value = "" And TemplName <> "ROČNA PONUDBA" Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
For CustCol = 4 To 200 'Move Through Columns
If .Cells(4, CustCol).Value = "Splosno" Then VarFormat = "General" Else: VarFormat = .Cells(5, CustCol).Value 'Determine Variable Format
TagName = .Cells(7, CustCol).Value 'Tag Name
TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = Application.WorksheetFunction.Text(TagValue, VarFormat)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With
Next CustCol
Dim Tbl As Long, Rw As Long, StrData As String
With WordDoc
For Tbl = 4 To 2 Step -1
With .Tables(Tbl)
StrData = ""
For Rw = 3 To 12
StrData = StrData & Split(.Cell(Rw, 2).Range.Text, vbCr)(0) '.Bookmarks("Schedule_of_Payments").Range.Tables(1).Delete
Next
If StrData = "" Then .Range.Bookmarks(1).Range.Delete 'If StrData = "" Then .Range.Bookmarks(1).Range.Tables(1).Delete
End With
Next
End With
If TemplName <> "ROČNA PONUDBA" Then
SaveNr = .Range("D" & CustRow).Text
str = "FOR TEST INSERT PATH TO FOLDER WHERE TEMPALTE IS STORED" & .Range("H" & CustRow).Value & " " & SaveNr & "-" & .Range("U" & CustRow).Value & "-" & .Range("R" & CustRow).Value & "\"
fol = Dir(str, vbDirectory)
If fol = "" Then MkDir str
FileName = str & .Range("H" & CustRow).Value & " " & SaveNr & "-" & .Range("U" & CustRow).Value & "-" & .Range("R" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName 'Saves Word document
FileName = str & .Range("H" & CustRow).Value & " " & SaveNr & "-" & .Range("U" & CustRow).Value & "-" & .Range("R" & CustRow).Value & ".pdf" 'Create full filename & Path with current workbook location, Last Name & First Name
On Error Resume Next
Kill (FileName) 'Delete filename with the same name if it exists
On Error GoTo 0
On Error Resume Next
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF 'Exports Word document as PDF in and saves in same folder as Word document
'WordDoc.Close False
End If
.Range("O" & CustRow).Value = "DA" 'pre je bilo to' TemplName 'Template Name
.Range("P" & CustRow).Value = Format(date_example, "dd.mm.yyyy hh:nn")
'.Range("R" & CustRow) = Format(date_example, "yyyy")' 'SEM ODRSTRANIL KER NI POTREBNO, IMAM FORMULO ZA TO, KER JE PODATEK POTREBEN PREJ'
'WordDoc.Close
End If '3 condition met
Next CustRow
'WordApp.Quit 'WordApp.Quit Does not quit Word when testing templates
End With
End Sub