473,839 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Word Automation - InsertBreak - New Page

1 New Member
I am trying to pull in some data from a database (Access) and merge the data to a Word document (basically mail merge). I got the data to fill in the bookmarks in the document but only the first selection from the checklistbox is being pulled in. Also I need the information thats pulled in to be on a separate page in the same document.

Hope that makes sense. Anyway here is my code.

Here is my code: (VB.Net)

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Dim wdApp As New Word.Application
  3.         wdApp.Visible = True
  4.         wdApp.Documents.Open("d:\testing.doc")
  5.  
  6.  
  7.         Try
  8.  
  9.             ' Fill in the data.
  10.             Dim dt As DataTable
  11.  
  12.             dt = infoData.GetData()
  13.  
  14.             Dim testchk As New CheckBox
  15.  
  16.             Dim checked_items As CheckedListBox.CheckedItemCollection = cboMailMerge.CheckedItems
  17.  
  18.             For Each item As Object In checked_items
  19.  
  20.                 Dim str As String = item.ToString()
  21.  
  22.                 For i As Integer = 0 To dt.Rows.Count - 1
  23.  
  24.                     If dt.Rows(i)("orgName").ToString() = str Then
  25.  
  26.                         Dim rng1 As Word.Range = wdApp.ActiveDocument.Bookmarks("ContactName").Range()
  27.                         Dim rng2 As Word.Range = wdApp.ActiveDocument.Bookmarks("CompanyName").Range()
  28.                         Dim rng3 As Word.Range = wdApp.ActiveDocument.Bookmarks("StreetAddress").Range()
  29.                         Dim rng4 As Word.Range = wdApp.ActiveDocument.Bookmarks("City").Range()
  30.                         Dim rng5 As Word.Range = wdApp.ActiveDocument.Bookmarks("State").Range()
  31.                         Dim rng6 As Word.Range = wdApp.ActiveDocument.Bookmarks("ZipCode").Range()
  32.                         Dim rng7 As Word.Range = wdApp.ActiveDocument.Bookmarks("EventName").Range()
  33.                         Dim rng8 As Word.Range = wdApp.ActiveDocument.Bookmarks("ContactName1").Range()
  34.  
  35.  
  36.                         rng1.Text = dt.Rows(i)("orgFname").ToString() & " " & dt.Rows(i)("orgLname").ToString()
  37.                         rng2.Text = dt.Rows(i)("orgName").ToString()
  38.                         rng3.Text = dt.Rows(i)("orgSaddress").ToString()
  39.                         rng4.Text = dt.Rows(i)("orgCity").ToString()
  40.                         rng5.Text = dt.Rows(i)("orgState").ToString()
  41.                         rng6.Text = dt.Rows(i)("orgZipcode").ToString()
  42.                         rng7.Text = dt.Rows(i)("ReqEvent").ToString()
  43.                         rng8.Text = dt.Rows(i)("orgFname").ToString() & " " & dt.Rows(i)("orgLname").ToString()
  44.  
  45.  
  46.  
  47.                         wdApp.ActiveDocument.Bookmarks.Add(rng1.Text, DirectCast(rng1, Word.Range))
  48.                         wdApp.ActiveDocument.Bookmarks.Add(rng2.Text, DirectCast(rng2, Word.Range))
  49.                         wdApp.ActiveDocument.Bookmarks.Add(rng3.Text, DirectCast(rng3, Word.Range))
  50.                         wdApp.ActiveDocument.Bookmarks.Add(rng4.Text, DirectCast(rng4, Word.Range))
  51.                         wdApp.ActiveDocument.Bookmarks.Add(rng5.Text, DirectCast(rng5, Word.Range))
  52.                         wdApp.ActiveDocument.Bookmarks.Add(rng6.Text, DirectCast(rng6, Word.Range))
  53.                         wdApp.ActiveDocument.Bookmarks.Add(rng7.Text, DirectCast(rng7, Word.Range))
  54.                         wdApp.ActiveDocument.Bookmarks.Add(rng8.Text, DirectCast(rng8, Word.Range))
  55.  
  56.  
  57.  
  58.  
  59.                     End If
  60.  
  61.  
  62.                 Next i
  63.                 wdApp.ActiveDocument.Range.InsertBreak()
  64.             Next item
  65.  
  66.  
  67.         Catch ex As Exception
  68.  
  69.         End Try
Nov 24 '07 #1
0 3214

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1897
by: bekil | last post by:
Thanks for your suggestion Murray Jack. I also record macro while doing it manually in word then I edit the macro to see the required code. Firstly I am creating a blank page. Secondly I go to second page then I changing page properties manually and I set orientation landscape by "this point forward".So My first page was portrait and my second page was landscape.This is what I want to get.
1
615
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes at a snails pace. Here are the detais: I wrote a C# program that creates a new Word doc and then loops through a
2
636
by: Bill Cooter | last post by:
I am attempting to insert an existing document into another existing document. When I insert the section break where I want the file insertion to take place, no matter what type of section break I use; wdSectionBreakContinuous; wdSectionBreakOddPage; wdSectionBreakEvenPage; or wdSectionBreakNextPage, incidently, the one that I want, it always results in a continuous break. Can anyone shed light on this item? Thanks in advance,
2
3015
by: Bill Cooter | last post by:
I am attempting to insert an existing document into another existing document. When I insert the section break where I want the file insertion to take place, no matter what type of section break I use; wdSectionBreakContinuous; wdSectionBreakOddPage; wdSectionBreakEvenPage; or wdSectionBreakNextPage, incidently, the one that I want, it always results in a continuous break. It works great for all breaks other than Section breaks. It's...
5
6697
by: John Smith | last post by:
Hey folks, I'm creating an MS Word document in a C# Windows application. Because the client base has so many different versions of Word they are using, I opted to go with late binding to create the document. **Everything works great, but I can't figure out how to do 2 things: 1) Double Spacing 2) Insert a Page Break
0
6309
by: mharris | last post by:
I need help with merging two Word documents into one through C# code. The problem isn't so much getting the documents put into one as it is maintaining the appropriate formatting, or rather reformating, after the merge. This is a full description of my needs. I have a C# class library that creates two Crystal Reports, and then exports them to the harddrive as Word documents. One's orientation is landscape, the other is portrait. I then...
0
4031
by: Raj Singh | last post by:
I am facing a problem with Visual C# 2003 and MS Word XP. I am using a customized template for Word and in that template I have defined some bookmarks. I am trying to create a Word document based on the customized template, reading all the bookmarks and replacing them with desired values, resulting in a new one page document being created which is then saved. This all works fine but when I try to extend the process so that the template...
0
1058
by: DJ | last post by:
Hi, I have this piece of code that work well in an .exe. If I move this code to a function in a dll and have an .exe calling the function, an exception is raised : The server threw an exception. Why the same code is not working in my dll??? The error occurs with the first call to the InsertFile function (and yes the files that I insert exists). In fact any call to Doc.Words.First is throwing an exception. Dim App As Word.Application
8
11305
by: Joachim | last post by:
Hi, I want to try call/run Ms. Word via my VB.Net First I added reference : Microsoft Word 11.0 Object Library. Then I got 2 references in my reference list : Word and Microsoft.Office.Core When I typed : Dim objWord as Word.Application Dim objDocument as Word.Document I got an error (underline at Word.Application and Word.Document). What is actually happen ?
0
9855
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10648
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.