Wrox Programmer Forums
|
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2007, 09:57 PM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB.Net Word Automation

Can anyone tell me why I am getting an 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. I hope that made sense. Anyway here is my code:


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
If anyone could help it would be greatly appreciated.:)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Mail Merge Automation using ASP.Net sbhatia ASP.NET 1.0 and 1.1 Professional 3 November 29th, 2013 07:18 AM
Word Automation TheNoobiest Beginning VB 6 1 November 9th, 2007 03:11 PM
Word Automation in XP bmains ASP.NET 1.0 and 1.1 Professional 0 July 21st, 2004 09:27 AM
Word automation in VB bobshark VB How-To 1 June 13th, 2003 08:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.