HI
i have some VBA code that inputs data from cells into a Word templete bookmarks and then saves the document as a seperate file mutiple times.
This works well however what i need help with is a way to say if range B2 to B10000 > 0 then dont copy the data to my word templete. The reason for this is if there is no amount in B2 then i dont want the information copied to the doc temeplete.
beleow is the code so far
Formula:
Private Sub CommandButton1_Click()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:myfiles\Letter template.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("Text1").Range.Text = ws.Range("A2").Value
.Bookmarks("Text2").Range.Text = ws.Range("A2").Value
.Bookmarks("Text3").Range.Text = ws.Range("B2").Value
.Bookmarks("Text4").Range.Text = ws.Range("C2").Value
.Bookmarks("Text5").Range.Text = ws.Range("D2").Value
.Bookmarks("Text6").Range.Text = ws.Range("E2").Value
End With
objWord.ActiveDocument.SaveAs FileName:="C:myfiles\Letter1.docx"
objWord.ActiveDocument.Close
objWord.Documents.Open "C:myfiles\Letter template.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("Text1").Range.Text = ws.Range("A3").Value
.Bookmarks("Text2").Range.Text = ws.Range("A3").Value
.Bookmarks("Text3").Range.Text = ws.Range("B3").Value
.Bookmarks("Text4").Range.Text = ws.Range("C3").Value
.Bookmarks("Text5").Range.Text = ws.Range("D3").Value
.Bookmarks("Text6").Range.Text = ws.Range("E3").Value
End With
objWord.ActiveDocument.SaveAs FileName:="C:myfiles\Letter2.docx"
objWord.ActiveDocument.Close
objWord.Documents.Open "C:myfiles\Letter template.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("Text1").Range.Text = ws.Range("A4").Value
.Bookmarks("Text2").Range.Text = ws.Range("A4").Value
.Bookmarks("Text3").Range.Text = ws.Range("B4").Value
.Bookmarks("Text4").Range.Text = ws.Range("C4").Value
.Bookmarks("Text5").Range.Text = ws.Range("D4").Value
.Bookmarks("Text6").Range.Text = ws.Range("E4").Value
End With
objWord.ActiveDocument.SaveAs FileName:="C:myfiles\Letter3.docx"
objWord.ActiveDocument.Close
Set objWord = Nothing
End Sub
Bookmarks