使用 Word 和 Visual Basic .NET 创建自定义文档应用程序

发布于 2024-09-04 21:29:47 字数 1430 浏览 1 评论 0原文

我对 Visual Studio(特别是 Visual Basic .NET)相当陌生,但已经编程很长时间了。

在我的公司,我们有一个 Access 应用程序,允许用户根据 Great Plains(一个会计程序)中的发票创建文档。发票、装箱单、出口文档等文档。

这个应用程序非常旧并且很难维护,因此我们正在重写,我选择这个项目作为我的第一个 Visual Basic .NET 项目。

基本上,应用程序要做的是允许用户执行以下操作:

  1. 提取发票信息,然后添加一些辅助信息,例如注释等
  2. 选择他们想要打印的文档
  3. 打开文档,然后允许用户编辑文件

最后一部分是棘手的部分。用户现在可以通过 Access 创建这些文档并打印为 PDF,然后使用特定于文档和客户的注释来编辑 PDF。如果他们不必这样做,那么我可以使用罐装报告之类的东西。

我想做的是创建带有文本框的Word模板,并在这些文本框中添加可寻址书签。当用户打印文档时,打开 Word,从数据库 (SQL Server) 填充书签,然后让他们绘制文本框并在打印文档之前填充他们想要的任何内容,然后保存它们。

这是我为此编写的代码(当然最终结果会复杂得多):

Public Const wdPageBreak = 7

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

'Open recordset for rsInvoice

oWord = CreateObject("Word.Application")

oDoc = oWord.Documents.Add()
oDoc.Application.Selection.Range.InsertFile("C:\document-c.dotx")
oDoc.Application.Selection.Range.InsertBreak(wdPageBreak)

oDoc.Application.Selection.Range.InsertFile("C:\document-b.dotx")
oDoc.Application.Selection.Range.InsertBreak(wdPageBreak)

oDoc.Application.Selection.Range.InsertFile("C:\document-a.dotx")

oDoc.Bookmarks("document_a_invoice_number").Range.Text = rsInvoice("invoice_number")
oDoc.Bookmarks("document_a_customer_number").Range.Text = rsInvoice("customer_number")
...
etc.
etc.
...
oWord.Visible = True

所以我想我的问题是这是处理这个项目的最合适的方法,或者是否有一种以编程方式或通过第三种方式的方法我不知道有什么聚会工具会更容易、更高效?

提前致谢。

I'm fairly new to Visual Studio (specifically Visual Basic .NET) but have been programming for quite a long time.

Here at my company we have an Access application that allows users to create documents based on invoices that are in Great Plains, an accounting program. Documents such as invoices, packing lists, export documentation, etc., etc.

This application is very old and very hard to maintain so we're doing a rewrite and I've chosen this project to be my first Visual Basic .NET project.

Basically what the application is going to do is allow users to do the following:

  1. Pull up invoice information and then add some secondary information such as comments and the like
  2. Choose which documents they want to print out
  3. Open the documents and then allow the user to edit the documents

That last part is the tricky part. The users right now create these documents out of Access and print to a PDF and then edit the PDF with comments that can be specific to the document and customer. If they didn't have to do that, then I could just used canned reports or something.

What I am thinking of doing is creating Word templates with text boxes and inside those text boxes adding addressable bookmarks. When the user goes to print the documents, open Word, populate the bookmarks from the database (SQL Server) and then that will let them draw text boxes and fill them with whatever they want before they print the documents and then they can save them.

Here's the code I worked out to do this (of course the final result will be much more complex):

Public Const wdPageBreak = 7

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

'Open recordset for rsInvoice

oWord = CreateObject("Word.Application")

oDoc = oWord.Documents.Add()
oDoc.Application.Selection.Range.InsertFile("C:\document-c.dotx")
oDoc.Application.Selection.Range.InsertBreak(wdPageBreak)

oDoc.Application.Selection.Range.InsertFile("C:\document-b.dotx")
oDoc.Application.Selection.Range.InsertBreak(wdPageBreak)

oDoc.Application.Selection.Range.InsertFile("C:\document-a.dotx")

oDoc.Bookmarks("document_a_invoice_number").Range.Text = rsInvoice("invoice_number")
oDoc.Bookmarks("document_a_customer_number").Range.Text = rsInvoice("customer_number")
...
etc.
etc.
...
oWord.Visible = True

So I guess the question I have is this the most appropriate way to approach this project or is there either a way programmatically or via a third party tool that I'm not aware of that would be easier and more efficient?

Thanks in advance.

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

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

发布评论

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

评论(1

╰沐子 2024-09-11 21:29:47

我最近参与了一个项目,该项目做了与此非常相似的事情,我们最终从 VB.Net 自动化 Word 以将数据插入书签。实际的代码很容易编写,只是要小心,以便之后正确关闭所有内容,很容易忘记某处的某些引用,从而使 Word 无法正确关闭。

除此之外,没有出现任何问题,而且他们已经使用该系统好几个月了。

但需要注意的一件事是,如果您将两个书签放在一起,有时一个书签可能会覆盖另一个书签,因此值得在它们之间添加一些空格(只需几个空格就可以了)。

I've recently been involved in a project that did something very similar to this and we ended up automating Word from VB.Net to insert data to the bookmarks. The actual code was easy enough to write, just be careful so that you close everything properly afterwards, it's easy to forget some reference somewhere so that Word won't close down properly.

Other than that there have been no problems and they've been using the system for quite a few months now.

One thing to be aware of though is if you put two bookmarks right after each other it seems like sometimes one bookmark can overwrite another, so it's worth adding some space between them (just a few spaces worked fine).

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