如何在 VB.NET 中将 Word 模板附加到新文档?

发布于 2024-08-31 13:05:41 字数 835 浏览 2 评论 0原文

我正在四处寻找这个应用程序是否可以完成。基本上,最终用户需要创建一堆从数据库填充的导出文档。

将有许多文档模板 (.dot),最终结果将是用户选择要包含在文档中的模板 xy 和 z,单击按钮并让应用程序创建一个新的 Word 文档,附加模板,然后填充模板与适当的数据。

它需要在 Word 中而不是在 Crystal Reports 中完成的原因是,用户可以在打印文档之前自定义某些字段,因为每次导出的情况可能会有所不同。

这可以通过 VB.NET (VS 2010) 来完成吗?

我认为是这样,但我很难找到解决方案。

或者还有更好的解决方案吗?

这是我到目前为止所拥有的(我知道的不多)

Import Microsoft.Office.Interop

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.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

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.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热风软妹 2024-09-07 13:05:41

Word 文档只能基于一个 .dot 模板:要基于模板创建新文档,您需要将模板的名称传递到 Documents.Add 方法中。无法应用多个模板。

如果您的目标是 Word 2007,则可以使用'构建块'< 来完成此操作/a>

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'

混浊又暗下来 2024-09-07 13:05:41

尝试
oSelection.InsertFile(模板路径)
(假设您使用单词选择方法)
这当然会将文件放置在您的选择指针所在的位置。所以你可能想要移动到末尾并提前插入分页符。

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.

太阳男子 2024-09-07 13:05:41

我希望您会想要在可以填充以下内容的模板中定义书签:

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

例如:

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

...之类的东西

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文