开发者

How do I append Word templates to a new document in VB.NET?

开发者 https://www.devze.com 2022-12-29 11:54 出处:网络
I\'m poking around to see if this app can be done.Basically the end user needs to create a bunch of export documents that are populated from a database.

I'm poking around to see if this app can be done. Basically the end user needs to create a bunch of export documents that are populated from a database.

There will be numerous document templates (.dot) and the end result will be the user choosing templates x y and z to include for documentation, click a button and have the app create a new Word document, append the templates, and then populate the templates with the appropriate data.

The reason it needs to be done in Word as opposed to something like Crystal Reports is that the user may customize some fields before printing the documents as it can vary from export to export.

Is this possible to do through VB.NET (VS 2010)?

I assume it is but I'm having difficulty tracking down a solution.

Or alternatively is there a better solution?

Here's what I have so far (not much I know)

Import Microsoft.Office.Interop

Public Class Form1

Private Sub Button1_Click(ByVal sender As System开发者_StackOverflow中文版.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim oWord As Word.Application
Dim oDoc As Word.Document

oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Add
'Open templates x.dot, y.dot, z.dot

'Append above templates to new document created

'Populate new document

oWord.Visible = True

End Sub

End Class


Word documents can only be based on one .dot template: to create a new document based on a template you would pass the name of the template into the Documents.Add method. There's no way to apply multiple templates.

If you're targeting Word 2007 though you could accomplish this using 'building blocks'


try
oSelection.InsertFile (template path)
(assuming you're using word selection methods)
this will of course drop the file whereever your selection pointer is. so you'll probably want to move to end and toss a page break in beforehand.


and I expect you will want to define bookmarks within the templates that can be populated following:

oDoc.ActiveWindow.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, Name:="Bookmark1")

for example:

Table3 = oDoc.ActiveWindow.Document.Tables.Add(Range:=oDoc.ActiveWindow.Selection.Range, _
                                                              NumRows:=5, _
                                                              NumColumns:=4, _
                                                              DefaultTableBehavior:=Word.WdDefaultTableBehavior.wdWord9TableBehavior, _
                                                              AutoFitBehavior:=Word.WdAutoFitBehavior.wdAutoFitContent)

... kind of thing

0

精彩评论

暂无评论...
验证码 换一张
取 消