Hi,
I'm trying to generate a lot of word documents from a list in excel. The macro copies a predefined template and insert parameters from the excel list into bookmarks in the template and saves the template with a different name. So far so good, but when I loop through this routine the problem is that the parameters just add up after the bookmarks. In other words, how can i delete the text under the bookmark in the word doc before inserting a new parameter and saving? Or is there another solution?
PS: I'm new to vba...
--------------------------------------------------------------------------
mal = MyPath & "\Frontpages\"
MkDir (mal)
'Copies the template to new folder
FileCopy "M:\#work\#LCI\SMIR&Forside\MALFOR~1.DOC", mal & "mal.doc"
'Open the template
Set wdApp = New Word.Application
Set myDoc = wdApp.Documents.Open(mal & "mal.doc")
'Loop
For iRow = 8 To iMaxRow
With Worksheets("Side 1").Cells(iRow, 1)
'See if the celle is empty
If .Value = "" Then
Exit For
Else
Docname = Worksheets("Side 1").Cells(2, 2).Value
DocNr = Worksheets("Side 1").Cells(iRow, 1).Value
DocPO = Worksheets("Side 1").Cells(2, 5).Value
DocTag = Worksheets("Side 1").Cells(iRow, 3).Value
DocTitle = Worksheets("Side 1").Cells(iRow, 4).Value
With myDoc.Bookmarks
.Item("DocName").Range.InsertAfter Docname
.Item("DocNr").Range.InsertAfter DocNr
.Item("DocPO").Range.InsertAfter DocPO
.Item("DocTag").Range.InsertAfter DocTag
.Item("DocTitle").Range.InsertAfter DocTitle
myDoc.SaveAs (mal & DocNr)
End With
End If
End With
Next iRow
myDoc.Close
Set wdApp = Nothing
Set myDoc = Nothing
Kill (mal & "mal.doc")
--------------------------------------------------------------------------
I'm grateful for tips/help.
Cheers
I'm trying to generate a lot of word documents from a list in excel. The macro copies a predefined template and insert parameters from the excel list into bookmarks in the template and saves the template with a different name. So far so good, but when I loop through this routine the problem is that the parameters just add up after the bookmarks. In other words, how can i delete the text under the bookmark in the word doc before inserting a new parameter and saving? Or is there another solution?
PS: I'm new to vba...
--------------------------------------------------------------------------
mal = MyPath & "\Frontpages\"
MkDir (mal)
'Copies the template to new folder
FileCopy "M:\#work\#LCI\SMIR&Forside\MALFOR~1.DOC", mal & "mal.doc"
'Open the template
Set wdApp = New Word.Application
Set myDoc = wdApp.Documents.Open(mal & "mal.doc")
'Loop
For iRow = 8 To iMaxRow
With Worksheets("Side 1").Cells(iRow, 1)
'See if the celle is empty
If .Value = "" Then
Exit For
Else
Docname = Worksheets("Side 1").Cells(2, 2).Value
DocNr = Worksheets("Side 1").Cells(iRow, 1).Value
DocPO = Worksheets("Side 1").Cells(2, 5).Value
DocTag = Worksheets("Side 1").Cells(iRow, 3).Value
DocTitle = Worksheets("Side 1").Cells(iRow, 4).Value
With myDoc.Bookmarks
.Item("DocName").Range.InsertAfter Docname
.Item("DocNr").Range.InsertAfter DocNr
.Item("DocPO").Range.InsertAfter DocPO
.Item("DocTag").Range.InsertAfter DocTag
.Item("DocTitle").Range.InsertAfter DocTitle
myDoc.SaveAs (mal & DocNr)
End With
End If
End With
Next iRow
myDoc.Close
Set wdApp = Nothing
Set myDoc = Nothing
Kill (mal & "mal.doc")
--------------------------------------------------------------------------
I'm grateful for tips/help.
Cheers
