我从哪里开始在 Word 2007 中进行 VBA 和宏编程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3210286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where do I get started with VBA and macros programming in Word 2007?
提问by pecker
I just came to know that macros in Word/Excel/PowerPoint can be programmed. That is awesome because I've a Word document with 70 tables for styling.
我刚开始知道 Word/Excel/PowerPoint 中的宏可以编程。这很棒,因为我有一个 Word 文档,其中包含 70 个用于样式设置的表格。
I'm a programmer but I don't know VB, VBA or VB .NET. I'm confused with these three. I'm familiar with .NET programming using C#. Now I want to write new macros. Where should I get started? What are VB, VBA, VB.NET and which one should I learn? Please suggest some material.
我是一名程序员,但我不知道 VB、VBA 或 VB .NET。我对这三个感到困惑。我熟悉使用 C# 的 .NET 编程。现在我想写新的宏。我应该从哪里开始?什么是 VB、VBA、VB.NET,我应该学习哪一个?请推荐一些材料。
If I learn for Word 2007 will that help with other Office applications life Excel & PowerPoint?
如果我学习 Word 2007 对其他 Office 应用程序 Excel 和 PowerPoint 有帮助吗?
回答by Ben McCormack
Here's a brief explanation of the different Visual Basics:
下面是对不同 Visual Basics 的简要说明:
- Visual Basic 6 (VB6, or classic VB)
- Released around 1998, this was the last iteration of Microsoft's original "Visual Basic." It has the beginnings of object-oriented development, but it requires the Visual Basic Runtime for applications to run. A lot of companies have used VB6 for internal business applications. It was superceded by VB.NET and the .NET Framework.
- Visual Basic for Applications (VBA)
- VBA shares the same code base as VB6 and in 1996 was available to be licensed to developers to include in their own applications. This is how VBA appears in Microsoft Office, as an embedded language that can be used to control Office's various applications. It's important to remember that VBA, which is still used to code office applications, is over a decade old and may feel as such when one is used to working with .NET.
- Visual Basic .NET (VB.NET)
- VB.NET was a radical departure from VB6. Though subsequent iterations of VB.NET have been referred to in sequence (e.g. VB7, VB8, etc.) by many developers, VB.NET shares very little with VB6 and VBA other than the BASIC syntax. Many consider it more of a new evolution in BASIC rather than an evolution in Visual Basic. Because it's entirely different from VBA and VB6, you cannot not use VB.NET code directly in VBA.
- Because VB.NET code compiles down to the same managed intermediate language code as C# and shares the same .NET APIs, you may feel more commonality between C# and VB.NET than VB6 and VB.NET from a programming perspective.
- Visual Basic 6(VB6 或经典 VB)
- 大约在 1998 年发布,这是 Microsoft 原始“Visual Basic”的最后一次迭代。它具有面向对象开发的开端,但它需要 Visual Basic Runtime 才能运行应用程序。许多公司已将 VB6 用于内部业务应用程序。它被 VB.NET 和 .NET Framework 取代。
- Visual Basic for Applications (VBA)
- VBA 与 VB6 共享相同的代码库,并且在 1996 年被授权给开发人员以包含在他们自己的应用程序中。这就是 VBA 在 Microsoft Office 中的显示方式,作为一种可用于控制 Office 各种应用程序的嵌入式语言。重要的是要记住,仍然用于编写办公应用程序的 VBA 已经有十多年的历史了,当人们习惯使用 .NET 时可能会感觉如此。
- Visual Basic .NET (VB.NET)
- VB.NET 与 VB6 完全不同。尽管 VB.NET 的后续迭代已被许多开发人员按顺序引用(例如 VB7、VB8 等),但除了 BASIC 语法之外,VB.NET 与 VB6 和 VBA 的共享很少。许多人认为它更像是 BASIC 的新演变,而不是 Visual Basic 的演变。因为它与 VBA 和 VB6 完全不同,所以不能直接在 VBA 中使用 VB.NET 代码。
- 因为 VB.NET 代码编译成与 C# 相同的托管中间语言代码并共享相同的 .NET API,所以从编程的角度来看,您可能会觉得 C# 和 VB.NET 之间比 VB6 和 VB.NET 更通用。
If you anticipate doing a lot of development in VBA, I would highly recommend the VBA Developer's Handbook, Second Edition, by Getz and Gilbert.
如果您打算在 VBA 中进行大量开发,我强烈推荐Getz 和 Gilbert 编写的VBA 开发人员手册,第二版。
Learning the VBA syntax for Word will certainly help you when you go to use Excel, Access, etc. However, each application has its own API that provides a set of objects and methods unique to its domain. For example, I'm very familiar with programming in VBA in Excel and Access, but I have never done macro programming in Word. Although the code syntax would be the same, I'd have to learn Word's API to be able to program against it.
当您开始使用 Excel、Access 等时,学习 Word 的 VBA 语法肯定会对您有所帮助。但是,每个应用程序都有自己的 API,它提供了一组独特的对象和方法。例如,我非常熟悉Excel和Access中的VBA编程,但我从未在Word中进行过宏编程。尽管代码语法是相同的,但我必须学习 Word 的 API 才能对其进行编程。
The nice thing about some of the Office applications (Excel, for example) is that you can record a macro, see what code it generates, and then tweak that code to do what you want. That's largely how I got started in programming.
某些 Office 应用程序(例如 Excel)的好处在于您可以录制宏,查看它生成的代码,然后调整该代码以执行您想要的操作。这主要是我开始编程的方式。
回答by Todd Main
There are some good answers here - I'd like to offer one more set of suggestions:
这里有一些很好的答案 - 我想再提供一组建议:
First, if supported in your environment, you can use Visual Studio 2005/8/10 and your C# skills to program against Office with "Visual Studio Tools for Office". See this threadfor more details.
首先,如果您的环境支持,您可以使用 Visual Studio 2005/8/10 和您的 C# 技能通过“Visual Studio Tools for Office”针对 Office 进行编程。有关更多详细信息,请参阅此线程。
If you want to delve into VBA instead (which I personally love because development is so fast compared to VS), start with this article Ten Code Conversions for VBA, Visual Basic .NET, and C#which will show you samples from all three languages. Next, watch this webcast: Using Visual Basic for Applications (VBA) Every Day Is Easier Than You Think.
如果您想深入研究 VBA(我个人喜欢它,因为与 VS 相比,开发速度如此之快),请从这篇文章开始,VBA、Visual Basic .NET 和 C# 的十种代码转换,它将向您展示所有三种语言的示例。接下来,观看此网络广播:每天使用 Visual Basic for Applications (VBA) 比您想象的要容易。
Thirdly- MSDN, read through this: Getting Started with VBA in Word 2010. 99% of it applies to Word 2007. There are many other articles linked from this one or you can always go to the main page of Office VBA Developer Centeras a single jump page.
第三 - MSDN,通读这篇文章:Word 2010 中的 VBA 入门。99% 都适用于 Word 2007。从这篇文章中链接了许多其他文章,或者您可以随时以单个跳转页面的形式转到Office VBA 开发人员中心的主页。
Then it's probably time to get started to directly program. See how things work, learn Word's Object Model, etc. You can always browse SO under the word-vbatag to see what other people are trying to do and the answers.
那么可能是时候开始直接编程了。看看事情是如何工作的,学习 Word 的对象模型等。你总是可以在word-vba标签下浏览 SO ,看看其他人正在尝试做什么和答案。
回答by Default
You always have MSDN.
If you go to Microsoft Word > Developer > Visual Basic and open up ThisDocument in the left menu, you will have the editor. If you press F2you will get the libraries that can be used in Microsoft Word (the Object Browser). The easiest program would be the following:
你总是有MSDN。
如果您转到 Microsoft Word > Developer > Visual Basic 并在左侧菜单中打开 ThisDocument,您将拥有编辑器。如果按F2,您将获得可在 Microsoft Word(对象浏览器)中使用的库。最简单的程序如下:
Sub Hello()
MsgBox "Hello World"
End Sub
When you have the sub marked, press F5(to run).
标记子项后,按F5(运行)。
In the Object Browseryou will have three different objects, properties, functions and events. When you see something that strikes your interest, go to the referencefor word vba and locate it.
[If the link changes, you can find it in the tree under MSDN Library > Office Development > 2007 Microsoft Office System > Word 2007 > Word 2007 Developer Reference > Word Object Model Reference]
在对象浏览器中,您将拥有三个不同的对象、属性、函数和事件。当您看到引起您兴趣的内容时,请转到单词 vba的参考并找到它。
[如果链接发生变化,可以在MSDN Library > Office Development > 2007 Microsoft Office System > Word 2007 > Word 2007 Developer Reference > Word Object Model Reference 下的树中找到它]
I think the easiest thing to do is to define a problem you need fixed and try to program it, similar to learning any other language. Don't make the problem to easy, but not to hard that you give up.
我认为最简单的方法是定义一个需要修复的问题并尝试对其进行编程,类似于学习任何其他语言。不要让问题变得容易,但不要让你放弃。