Christoph99
New Member
- Joined
- Dec 21, 2022
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
Generally, I would like to paste the data from an excel file into a word template (both in the same folder) and then save it under a variable name.
If I paste my code directly into the excel file, it works.
But now I want to save this macro in my personal folder, so that I can use the macro with different excel files.
Thereby 3 problems arise:
1. the data is no longer transferred to the template.
2. the final file is saved in my personal folder and not with the template and excel file in the folder
3. the file name is wrong. The variable data from the excel is missing
Here is my code:
Thx for help
If I paste my code directly into the excel file, it works.
But now I want to save this macro in my personal folder, so that I can use the macro with different excel files.
Thereby 3 problems arise:
1. the data is no longer transferred to the template.
2. the final file is saved in my personal folder and not with the template and excel file in the folder
3. the file name is wrong. The variable data from the excel is missing
Here is my code:
VBA Code:
Sub ExportToWordCasualty()
'Activate library
Dim wordapp As New Word.Application
Dim doc As Word.Document
Dim Zeile As Long
Zeile = ActiveCell.Row
'Make Word visible
wordapp.Visible = True
'Open Word file
Set doc = wordapp.Documents.Open("J:\I\Kartoffel\Template_Casualty_Muster.docx")
'fill word file with Excel data
doc.Bookmarks("Bodily_Injury").Range.Text = Tabelle1.Cells(Zeile, 6).Value
doc.Bookmarks("Construction_Liability").Range.Text = Tabelle1.Cells(Zeile, 8).Value
'Save Word file
doc.SaveAs2 ThisWorkbook.Path & "\Casualty_" & Tabelle1.Cells(Zeile, 2).Value & "_" & Tabelle1.Cells(Zeile, 1).Value & "_" & Tabelle1.Cells(Zeile, 17).Value & ".docx"
'Close Word file
doc.Close SaveChanges:=False
'Close Word file
wordapp.Quit
End Sub
Thx for help
Last edited by a moderator: