473,471 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to create a document based on a template to add db data

15 New Member
Hi all,

I am using visual studio 2003 with installed VSTO with Office 2003.
I am trying to populate a template that I created with db data in order to create an invoices to our customers (similar to merge but more flexible).

I'm using bookmarks to populate the template and it works well,
Hoever I'm not sure how to initialize and move to the next page on the document...
Meaning How to append a new page to the filled document and re use the template page from scratch in order to populate it again for the second record and so on thru the loop.


it should look like :

Dear Mr. _______ (db field CsutomerName) Todays Date:_______
This is your invoice for: _________(orderDate). Total is: ______
Thank you for your payment Blah blah blah ...
......
more fields...



Regards
Stu
Dec 16 '08 #1
6 2421
nukefusion
221 Recognized Expert New Member
Hello vstud70,
How is your template set up? I'm not very familiar with the tools packaged with VSTO. Is the template a feature of VSTO or something that you've implemented yourself?
Dec 17 '08 #2
vstud70
15 New Member
Hi nukefusion,

It's a regular template, a .dot file ,that has some verbiage and
some bookmarks located where I would like to insert my d.b. fields.

As for VSTO, I'm not too familiar with it so much, I just installed it since to my understanding it will be easier to code with it relative to regular VBA code on the word document (and i 'm lacking vba experience). You can ignore that it is VSTO for that matter.

What currently I do is:
i. open the fname.dot template file once.
ii. I insert into the fname.dot file some fields from the database
iii. write the filled template, or save it (save as command) into a .doc file (a different file name)

What I'm not sure how to do is to repeat this process, as I scroll the dataset records that I pulled from the database.
do i need to clean the template so i can rewrite the next itteration fields, if so how, or do i close the template file and re open it to write into it again?
And how do i tell the application to write to the next page on the word document? (should i write page break to jump to the next page).

So i'm not sure how to loop this thing...
Let me know if makes things clearer.

Thanks in advance!
Dec 17 '08 #3
nukefusion
221 Recognized Expert New Member
Ok, thanks for providing extra information. I guess I was just wondering whether you were opening up the document and processing it in .NET or using office automation to do it.

If I was processing the file in C# or VB.NET code I would probably:
  1. Load the template into memory once
  2. Make a copy of that template in memory
  3. Insert DB fields into copy
  4. Save copy as .doc
  5. Repeat from step 2

If I was automating office I would:
  1. Load the template
  2. Insert DB fields into copy
  3. Save copy as .doc
  4. Close template without saving
  5. Repeat from step 1

Presumably once you've added the fields to the template once, your placeholders are gone, so you'd either need to make a copy of the template somewhere as in the first example or reopen the template as in the second.
Dec 17 '08 #4
vstud70
15 New Member
Hi nukefusion,

Yes I should have mentioned that I'm using the VB.Net,
I believe that I'm doing something similar to what you have described.
However when you say save copy as a doc it will rewrite the old one and
I need it to append to the same doc file, basicaly adding a new word page each iteration.



part of this snip will be looped with the matching clients data(recordset)
'Note that the M11 is the template that is changed using the d.b. fields,
than append it all to the M3.doc and then I need to rewrite the template,
append it again and etc with the loop. Hope it make sense.

Here is a code snip:

'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add("C:\Misc\MaillingLetters\M11.d ot")


' oWord.Documents.Add("C:\MaillingLetters\M1.dot")
oDoc.Bookmarks.Item("nameB").Range.Text = "client name1"
oDoc.Bookmarks.Item("lname").Range.Text = "client name1"

oDoc.SaveAs("C:\Misc\MaillingLetters\M3.doc")



If you get a chance see if you can change it and have M3.doc accumulate
the new pages that are derived from the M11 template.

Regards
Dec 18 '08 #5
nukefusion
221 Recognized Expert New Member
Ok, thanks for the code snippet, now I can see that you want to do it using office automation. How about using the clipboard to achieve what you want using the following logic:
  1. Create new document for holding records
  2. Open template
  3. Replace bookmarks with DB fields
  4. Copy the updated template to the clipboard
  5. Paste into new document
  6. Close template
  7. If there are more records repeat from step 2.
  8. Save new document
Dec 18 '08 #6
vstud70
15 New Member
Hi,

Using the clipboard was a good idea even though I didn't use the clipboard property per se (directly) instead i copied the wholestory to the new document which is similar idea.
Also fyi, instead of closing and reopenning the templ I used the "undo" methos that I found. I used it to initialize my document. it's working like a charm.

Thank you for sharing your thoughts and knowledge!
Dec 19 '08 #7

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

Similar topics

3
by: Richard Fritzler | last post by:
I was given the task of designing a complete web based document prep system. In simplest terms (using a msword explanation) create a database of merge fields, and a library of templates. Allow the...
0
by: Mr. T. | last post by:
Hi, i've got a word-template based on which i make a word-document throught automatisation from within my access vba-module. So far, no problem. Now the lay-out of the document needs to be...
7
by: Daniel Walzenbach | last post by:
Hello, I want to create a Word XML file based on the input users make in a VB.NET application. I imagine creating a template in Word and saving it as a XML file. I then want to fill the...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
2
by: Mukesh | last post by:
Hi all I m Using SQL SERVER 2005 I have a requirement to store some data in xmldatatype using stored procedure , Here is example
5
by: Jeremy | last post by:
Does anyone have a clever algorithm for generating an outline of the current document from (client-side) javascript using DOM methods? For example, let's say I predictably have a document...
0
by: 5anelli | last post by:
Hi to everyone, I need to create and print (from within a c# application) a MS Word document starting from an Access db. Half of this document must be written and printed in, let's say, January,...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: dbsog7777 | last post by:
I was trying to use the sample code below, but I encountered two errors: Application.DoEvents() and AutoText(entry). I am not sure how to correct the errors. I trying to use the sample code to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.