VB.Net Word Automation

D

Deboe

Can anyone tell me why I am getting a blank page in my word document using
the code below? I have a checklistbox on my form and for each checked item I
want the same amount of pages in my word document. For instance, 3 items
checked = 3 MS Word pages but its giving me 4 pages instead with the second
page in the document being blank. As you can see below I am not necessarily
using the VSTO add-in for VS Pro but I figured someone here would be able to
point something out to help. Thanks in advance. I hope that made sense.
Anyway here is my code:


Dim dt As DataTable
dt = XSdSponsor.GetData()

Dim wdApp As New Word.Application
wdApp.Visible = True
wdApp.Documents.Open("d:\testing.doc")

Dim pagebreak As Object = Word.WdBreakType.wdPageBreak
Dim checked_items As CheckedListBox.CheckedItemCollection

checked_items = cboMailMerge.CheckedItems

Dim item As Object

Dim bVal As Boolean = True
Dim Rng As Word.Range


For Each item In checked_items

Dim str As String = item.ToString()

For i As Integer = 0 To dt.Rows.Count - 1

If dt.Rows(i)("orgName").ToString() = str Then


Dim rng1 As Word.Range =
wdApp.ActiveDocument.Bookmarks("ContactName1").Range()
Dim rng2 As Word.Range =
wdApp.ActiveDocument.Bookmarks("CompanyName").Range()
Dim rng3 As Word.Range =
wdApp.ActiveDocument.Bookmarks("StreetAddress").Range()
Dim rng4 As Word.Range =
wdApp.ActiveDocument.Bookmarks("City").Range()
Dim rng5 As Word.Range =
wdApp.ActiveDocument.Bookmarks("State").Range()
Dim rng6 As Word.Range =
wdApp.ActiveDocument.Bookmarks("ZipCode").Range()
Dim rng7 As Word.Range =
wdApp.ActiveDocument.Bookmarks("ContactName").Range()
Dim rng8 As Word.Range =
wdApp.ActiveDocument.Bookmarks("EventName").Range()

If bVal = True Then
rng1.Text = dt.Rows(i)("orgFname").ToString() & " "
& dt.Rows(i)("orgLname").ToString()
rng2.Text = dt.Rows(i)("orgName").ToString()
rng3.Text = dt.Rows(i)("orgSaddress").ToString()
rng4.Text = dt.Rows(i)("orgCity").ToString()
rng5.Text = dt.Rows(i)("orgState").ToString()
rng6.Text = dt.Rows(i)("orgZipcode").ToString()
rng7.Text = dt.Rows(i)("orgFname").ToString() & " "
& dt.Rows(i)("orgLname").ToString()
rng8.Text = dt.Rows(i)("ReqEvent").ToString()

Else

Rng =
wdApp.ActiveWindow.Selection.Bookmarks("\page").Range()

Rng.Select()
Rng.Document.ActiveWindow.Selection.Copy()
Rng.Select()
Rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
Rng.Select()
Rng.InsertBreak(pagebreak)
Rng.Paste()

rng1.Text = dt.Rows(i)("orgFname").ToString() & " "
& dt.Rows(i)("orgLname").ToString()
rng2.Text = dt.Rows(i)("orgName").ToString()
rng3.Text = dt.Rows(i)("orgSaddress").ToString()
rng4.Text = dt.Rows(i)("orgCity").ToString()
rng5.Text = dt.Rows(i)("orgState").ToString()
rng6.Text = dt.Rows(i)("orgZipcode").ToString()
rng7.Text = dt.Rows(i)("orgFname").ToString() & " "
& dt.Rows(i)("orgLname").ToString()
rng8.Text = dt.Rows(i)("ReqEvent").ToString()

End If

wdApp.ActiveDocument.Bookmarks.Add("ContactName1",
CType(rng1, Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("CompanyName",
CType(rng2, Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("StreetAddress",
CType(rng3, Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("City", CType(rng4,
Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("State", CType(rng5,
Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("ZipCode",
CType(rng6, Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("ContactName",
CType(rng7, Word.Range))
wdApp.ActiveDocument.Bookmarks.Add("EventName",
CType(rng8, Word.Range))

bVal = False

Exit For

End If

Next

Next item
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top