使用VBA创建Word或XML文档 [英] Creating Word or XML document with VBA

查看:220
本文介绍了使用VBA创建Word或XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Word 2003中使用VBA创建文档(约150个报告,信函),这些文档主要来自现有的代码段和本地数据库.然后,我需要更改这些代码段的特定部分,例如日期,词组,用户信息,& c.显然,我的程序中还会产生一些位.

当前,某些丑陋的旧版VBA可以完成(或类似)相同的任务. (嵌套的IF和FOR,以及200多个文本框,全称为TextBox#)它可以在某些时候完成工作,并通过向Word应用程序发送指令来完成工作(例如,Selection.MoveDown Unit:wdLine, Count:=1.)因此,即使生成一个简单的字母也需要30秒钟秒,然后锁定Office.

我完全能够使用范围和书签以及改进的逻辑来执行相同的操作,但是我的直觉是,使用XML可以很容易做到这一点,并且将来这样做会带来好处,因为这些报告/letters将被许多不同的用户和应用程序访问,并且可能会以编程方式多次读取.

我一直在阅读有关XML和WordML架构的信息,但感觉好像我遗漏了一些东西.

我希望代码在Submit上执行的操作是这样的:

  1. 获取预定义的xml代码段(页眉,页脚等)
  2. 生成新字符串并连接预定义的XML字符串
  3. 另存为XML
  4. 更改XML文件中现有标签的值(日期,用户名等)

我可以做到.

我遇到的问题是:

  • 我很难理解模式和名称空间.数据已经通过VBA代码验证,那么我需要什么模式?

  • 通过创建一个包含整个文档的XML的长连接字符串来创建文档是更好的方法,请记住,在我的模型中,许多XML都将来自现有代码片段,或者使用XML Dom和以编程方式创建它:

20 Set oElementName = oDOM.createElement("Name")

30 oElement.appendChild oElementName

40 oElementName.Text = "This is the text of name"

  • 如何设置文档样式? (从概念上讲,这是给我造成最大的麻烦.)我特别不想从Word保存时生成的WordML对其进行反向工程.我认为我需要先将其转换为HTML吗?我可以在Word中创建样式表以使其自动设置格式吗?

这有道理吗?我应该只使用范围和书签吗?

XML对我来说似乎很强大,但是它可能是锤子和钉子问题的一个扭曲案例,即我想学习Hammer,所以每个问题看起来都像钉子,这使我有充分的理由学习Hammer."

(我知道我不应该像做这些事情那样学习如何做事!但是我只是一个笨拙的无人机,他对我们的文档生成系统的质量抱怨得足够多,他们说:然后关闭并修复它. !哦,您不知道VBA吗?好吧,我们负担不起向您发送任何课程或任何事情的经验,所以请随身携带学习.这是我工作生涯中最美好,最糟糕的两个月.)

如果您已阅读所有内容,以及可能获得的任何帮助/建议,我将不胜感激!

解决方案

我不知道您的任职情况,也许有充分的理由继续推进您的技术栈,但是看来投资如此之大( 150个报告很大)应该重新考虑到Office 2003中. 尽管微软说VBA不会消失,但我认为Open XML SDK的前途一片光明.

这是XML标记中的出色的介绍.

I need to create documents, (~150 reports, letters) mostly from pre-existing snippets and local databases, with VBA, in Word 2003. I then need to change specific parts of those snippets, such as dates, phrases, user information, &c. There are also, obviously, some bits that I will generate in my program.

Currently, this same task is done (sort of) by some hideous legacy VBA. (Nested IFs and FORs, and over 200 textboxes all called TextBox#) It gets the job done SOMETIMES, and does so by sending instructions to the Word application (Selection.MoveDown Unit:wdLine, Count:=1, for example.) Consequently, generating even a simple letter takes 30 seconds, and locks up Office.

I'm perfectly able to do the same thing with ranges and bookmarks and improved logic, but my gut feeling is that this should be easy to do this with XML and that doing so will have advantages in the future, as these reports/letters are to be accessed, and likely read programmatically, many times by many different users and applications.

I have been reading about XML and the WordML schema, but feel like I'm missing something.

What I want the code to do on Submit is this:

  1. Grab predefined xml snippets (header, footer, etc.)
  2. Generate new strings and concatenate predefined XML strings
  3. Save as XML
  4. Change value of existing tags in XML file (date, username, etc)

I can do this.

The issues I have are:

  • I been having trouble getting my head around schema and namespaces. The data is already validated by the VBA code, so what do I need a schema for?

  • Is it better to create the document by creating one long concatenated string containing the XML for the entire document, bearing in mind in my model much of the XML would be from existing snippets, or to use the XML Dom and create it programmatically:

20 Set oElementName = oDOM.createElement("Name")

30 oElement.appendChild oElementName

40 oElementName.Text = "This is the text of name"

  • HOW DO I STYLE THE DOCUMENT? (This has been causing me the most trouble, conceptually.) I don't particularly want to reverse engineer it from the WordML that Word generates when it saves. I take it I need to convert it to HTML first? Can I create a stylesheet in Word so it automatically formats it?

Does any of this make sense? Should I just use ranges and bookmarks instead?

XML seems powerful to me, but it could be a twisted case of the hammer and nail problem, i.e. "I want to learn Hammer, so every problem looks like a nail, giving me a good reason to learn Hammer."

(I know I shouldn't be learning how to do things as I do them! But I'm just a mindless drone who complained enough about the quality of our document generation system that they said "Shut up and fix it then! Oh, you don't know any VBA? Well, we can't afford to send you on a course or anything, so learn as you go." It's been the best and worst 2 months of my working life.)

I appreciate you reading through all of that, if you did, and any help/advice you might have!

解决方案

I don't know the circumstances of your assignment and there may be good reasons to move ahead with your technology stack, but it seems that such a big investment (150 reports is big) into Office 2003 should be reconsidered. While Microsoft is saying that VBA will not go away, I think that the Open XML SDK has a brighter future.

Here is an excellent introduction into the XML markup.

这篇关于使用VBA创建Word或XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆