word vba宏_通过Word 2007了解VBA宏编码

word vba宏

The goal of this course is to help people who have never written a program before learn to write one. There's no reason why office workers, homemakers, professional engineers and pizza delivery persons shouldn't be able to take advantage of their own hand crafted custom computer programs to work faster and smarter. It shouldn't take a 'professional programmer' (whatever that is) to do the job. You know what needs to be done better than anyone else. You can do it yourself!

本课程的目的是帮助从未学习过程序的人学习编写程序。 没有理由为什么办公室工作人员,家庭主妇,专业工程师和披萨送货员不应该利用自己手工制作的定制计算机程序来更快,更智能地工作。 不需要“专业程序员”(无论是什么)来完成这项工作。 您知道什么需要比其他任何人做得更好。 你可以自己做!

(And I say this as someone who has spent many years writing programs for other people ... 'professionally'.)

(我说这是一个花了多年时间为其他人编写程序的人……“专业”。)

With that said, this is not a course in how to use a computer.

话虽如此,这不是使用计算机的课程。

This course assumes that you know how to use popular software and in particular, that you have Microsoft Word 2007 installed on your computer. You should know basic computer skills like how to create file folders (that is, directories) and how to move and copy files. But if you've always wondered what a computer program actually was, that's OK. We'll show you.

本课程假定您知道如何使用流行的软件,尤其是您在计算机上安装了Microsoft Word 2007。 您应该了解计算机的基本技能,例如如何创建文件夹(即目录)以及如何移动和复制文件。 但是,如果您一直想知道实际上是什么计算机程序,那就可以了。 我们会告诉你。

Microsoft Office isn't cheap. But you can get more value from that expensive software you already have installed. That's a big reason we use Visual Basic for Applications, or VBA, along with Microsoft Office. There are millions who have it and a handful (maybe no one) who uses everything it can do.

Microsoft Office并不便宜。 但是,您可以从已经安装的昂贵软件中获得更多价值。 这是我们将Visual Basic for Applications或VBA与Microsoft Office一起使用的重要原因。 有数以百万计的人拥有它,只有极少数(也许没有人)使用它可以做的一切。

Before we go any further, however, I need to explain one more thing about VBA. In February 2002, Microsoft made a 300 billion dollar bet on a totally new technology base for their entire company. They called it .NET. Since then, Microsoft has been moving their entire technology base into VB.NET. VBA is the very last programming tool that still uses VB6, the tried and true technology that was used before VB.NET. (You'll see the phrase "COM based" to describe this VB6 level technology.)

但是,在进行下一步之前,我需要解释一下有关VBA的另一件事。 2002年2月,Microsoft为整个公司在全新技术基础上下了3000亿美元的赌注。 他们称它为.NET。 从那时起,微软一直将其整个技术基础迁移到VB.NET中。 VBA是最后一个仍使用VB6的编程工具,VB6是VB.NET之前使用的可靠技术。 (您将看到短语“基于COM”来描述此VB6级技术。)

VSTO和VBA ( VSTO and VBA )

Microsoft has created a way to write VB.NET programs for Office 2007. It's called Visual Studio Tools for Office (VSTO). The problem with VSTO is that you have to buy and learn to use Visual Studio Professional. Excel itself is still COM based too and .NET programs have to work with Excel through an interface (called the PIA, Primary Interop Assembly).

微软已经创建了一种为Office 2007编写VB.NET程序的方法。它被称为Visual Studio Tools for Office(VSTO)。 VSTO的问题在于,您必须购买并学习使用Visual Studio Professional。 Excel本身仍然还是基于COM的,.NET程序必须通过接口(称为PIA,主要互操作程序集)与Excel一起使用。

So ... until Microsoft gets their act together and gives you a way to write programs that will work with Word and doesn't make you join the IT department, VBA macros are still the way to go.

因此,...直到Microsoft团结一致,并为您提供了一种可以与Word一起使用且不会使您加入IT部门的程序的方式,VBA宏仍然是您的最佳选择。

Another reason we use VBA is that it really is a 'fully baked' (not half baked) software development environment that has been used for years by programmers to create some of the most sophisticated systems in existence. It doesn't matter how high your programming sights are set. Visual Basic has the power to take you there.

我们使用VBA的另一个原因是,它确实是一个“完全成熟”(而非半成熟)的软件开发环境,程序员使用该环境已经多年,以创建一些现有的最复杂的系统。 您的编程目标有多高都没有关系。 Visual Basic可以带您前往那里。

什么是宏? ( What is a macro? )

You may have used desktop applications that support what is called a macro language before. Macro's are traditionally just scripts of keyboard actions grouped together with one name so you can execute them all at once. If you always start the day by opening your "MyDiary" document, entering today's date, and typing the words, "Dear Diary," -- Why not let your computer do that for you? To be consistent with other software, Microsoft calls VBA a macro language too. But it's not. It's much more.

您以前可能已经使用过支持宏语言的桌面应用程序。 传统上,宏只是将键盘操作的脚本与一个名称组合在一起,因此您可以一次执行它们。 如果您总是通过打开“ MyDiary”文档,输入今天的日期并输入单词“ Dear Diary”来开始新的一天-为什么不让计算机为您执行此操作? 为了与其他软件保持一致,Microsoft也将VBA称为宏语言。 但事实并非如此。 还有更多。

Many desktop applications include a software tool that will let you record a "keystroke" macro. In Microsoft applications, this tool is called the Macro Recorder, but the result is not a traditional keystroke macro. It's a VBA program and the difference is that it does not simply replay the keystrokes. A VBA program gives you the same end result if possible, but you can also write sophisticated systems in VBA that leave simple keyboard macros in the dust. For example, you can use Excel functions in Word using VBA. And you can integrate VBA with other systems like databases, the web, or other software applications.

许多桌面应用程序包含一个软件工具,该工具可以让您记录“按键”宏。 在Microsoft应用程序中,此工具称为“宏记录器”,但结果不是传统的按键宏。 这是一个VBA程序,不同之处在于它不只是重播按键。 如果可能,VBA程序会给您相同的最终结果,但是您也可以在VBA中编写复杂的系统,从而使简单的键盘宏成为现实。 例如,可以在使用VBA的Word中使用Excel函数。 而且,您可以将VBA与其他系统(如数据库,Web或其他软件应用程序)集成。

Although the VBA Macro Recorder is very useful for simply creating simple keyboard macros, programmers have discovered that it's even more useful to give them a running start in more sophisticated programs. That's what we're going to do.

尽管VBA宏记录器对于简单地创建简单的键盘宏非常有用,但是程序员发现,在更复杂的程序中为它们提供运行起点甚至更为有用。 那就是我们要做的。

Start Microsoft Word 2007 with a blank document and get ready to write a program.

使用空白文档启动Microsoft Word 2007 ,并准备编写程序。

Word中的“开发人员”选项卡 ( The Developer tab in Word )

One of the first things that you have to do to write Visual Basic program in Word 2007 is find Visual Basic! The default in Word 2007 is to not display the ribbon that is used. To add the Developer tab, first click the Office button (the logo in the upper left corner) and then click Word Options. Click Show Developer tab in the Ribbon and then click OK.

要在Word 2007中编写Visual Basic程序,要做的第一件事就是找到Visual Basic ! Word 2007中的默认设置是不显示所使用的功能区。 要添加“ 开发人员”标签,请先单击Office按钮(左上角的徽标),然后单击Word选项 。 单击功能区中的“显示开发人员”选项卡 ,然后单击“ 确定”

When you click the Developer tab, you have a whole new set of tools used to write VBA programs. We're going to use the VBA Macro Recorder to create your first program. (If the ribbon with all your tools keeps disappearing, you might want to right-click the ribbon and make sure Minimize the Ribbon is not checked.)

单击“ 开发人员”选项卡时,您将使用一组全新的工具来编写VBA程序。 我们将使用VBA宏记录器创建您的第一个程序。 (如果带有所有工具的功能区不断消失,则可能需要右键单击功能区,并确保未选中“ 最小化功能区” 。)

Click Record Macro. Name your macro: AboutVB1 by typing that name in the Macro Name textbox. Select your current document as the location to store your macro and click OK. See the example below.

单击记录宏 。 为您的宏命名: AboutVB1 ,方法是在“ 宏名称”文本框中键入该名称。 选择当前文档作为存储宏的位置,然后单击“确定”。 请参见下面的示例。

(Note: If you pick All Documents (Normal.dotm) from the drop down menu, this test VBA program will, in effect, become a part of Word itself because it will then become available for every document you create in Word. If you only want to use a VBA macro in a specific document, or if you want to be able to send it to someone else, it's a better idea to save the macro as part of the document. Normal.dotm is the default so you must change it.)

(注意:如果您从下拉菜单中选择“ 所有文档(Normal.dotm) ”,则该测试VBA程序实际上将成为Word的一部分,因为它将对您在Word中创建的每个文档都可用。只希望在特定文档中使用VBA宏,或者如果您希望能够将其发送给其他人,则最好将该宏另存为文档的一部分,因为Normal.dotm是默认设置,因此您必须进行更改它。)

With the Macro Recorder turned on, type the text, "Hello World." into your Word document. (The mouse pointer will change into a miniature picture of a tape cartridge to show that keystrokes are being recorded.)

在宏录制器打开的情况下,键入文本“ Hello World”。 到您的Word文档中。 (鼠标指针将变成盒带的缩影,以显示正在记录击键。)

(Note: Hello World is almost required for a "First Program" because the very first programming manual for the early computer language "C" used it. It's been a tradition ever since.)

(注意:“第一程序”几乎需要Hello World,因为早期的计算机语言“ C”的第一本编程手册就使用了它。从那以后一直是一种传统。)

Click Stop Recording. Close Word and save the document using the name: AboutVB1.docm. You have to select a Word Macro-Enabled Document from the Save as Type dropdown.

单击停止记录 。 关闭Word并使用名称AboutVB1.docm保存文档。 您必须从“ 保存类型”下拉列表中选择“ 启用Word宏的文档 ”。

That's it! You have now written a Word VBA program. Let's see what it looks like!

而已! 您现在已经编写了Word VBA程序。 让我们看看它是什么样子!

了解什么是VBA程序 ( Understanding what a VBA program is )

If you have closed Word, open it again and select the AboutVB1.docm file that you saved in the previous lesson. If everything was done correctly, you should see a banner at the top of your document window with a security warning.

如果您关闭了Word,请再次将其打开,然后选择在上一课中保存的AboutVB1.docm文件。 如果一切操作正确,您应该在文档窗口顶部看到带有安全警告的标语。

VBA和安全性 ( VBA and Security )

VBA is a real programming language. That means that VBA can do just about anything you need it to do. And that, in turn, means that if you receive a Word document with an embedded macro from some 'bad guy' that macro can do just about anything too. So Microsoft's warning is to be taken seriously. On the other hand, you wrote this macro and all it does is type "Hello World" so there's no risk here. Click the button to enable macros.

VBA是一种真正的编程语言 。 这意味着VBA几乎可以完成您需要执行的所有操作。 反过来,这意味着,如果您从某个“坏蛋”那里收到带有嵌入式宏的Word文档,则该宏也可以执行任何操作。 因此,Microsoft的警告应得到认真对待。 另一方面, 编写了此宏,它所做的只是键入“ Hello World”,因此这里没有任何风险。 单击按钮启用宏。

To see what the Macro Recorder has created (as well as to do most other things that involve VBA), you need to start the Visual Basic Editor. There's an icon to do that at the left side of the Developer ribbon.

若要查看宏记录器创建的内容(以及执行涉及VBA的大多数其他操作),您需要启动Visual Basic编辑器。 在开发人员功能区的左侧有一个图标可以做到这一点。

First, notice the left hand window. This is called the Project Explorer and it groups together the high level objects (we'll talk more about them) that are part of your Visual Basic project.

首先,请注意左侧窗口。 这称为项目资源管理器 ,它将Visual Basic项目中的高级对象(我们将详细讨论)组合在一起。

When the Macro Recorder was started, you had a choice of the Normal template or the current document as a location for your macro. If you selected Normal, then the NewMacros module will be part of the Normal branch of the Project Explorer display. (You were supposed to select the current document. If you did select Normal, delete the document and repeat the previous instructions.) Select NewMacros under Modules in your current project. If there still isn't any code window displayed, click Code under the View menu.

启动宏记录器时,您可以选择“ 普通”模板或当前文档作为宏的位置。 如果选择了Normal,则NewMacros模块将成为Project Explorer显示的Normal分支的一部分。 (应该选择当前文档。如果确实选择了Normal ,则删除该文档并重复前面的说明。)在当前项目的Modules下选择NewMacros 。 如果仍然没有显示任何代码窗口,请单击 视图”菜单下的 代码”

Word文档作为VBA容器 ( The Word document as a VBA container )

Every Visual Basic program must be in some kind of file 'container'. In the case of Word 2007 VBA macros, that container is a ('.docm') Word document. Word VBA programs can't run without Word and you can't create standalone ('.exe') Visual Basic programs like you can with Visual Basic 6 or Visual Basic .NET. But that still leaves a whole world of things you can do.

每个Visual Basic程序都必须位于某种文件“容器”中。 对于Word 2007 VBA宏,该容器是一个('.docm')Word文档。 Word VBA程序无法在没有Word的情况下运行,并且您无法像使用Visual Basic 6或Visual Basic .NET一样创建独立的(.exe)Visual Basic程序。 但这仍然留下了您可以做的所有事情。

Your first program is certainly short and sweet, but it will serve to introduce the major features of VBA and the Visual Basic Editor.

您的第一个程序肯定简短而有趣,但是它将用来介绍VBA和Visual Basic编辑器的主要功能。

The program source will normally consist of a series of subroutines. When you graduate to more advanced programming, you'll discover that other things can be part of the program besides subroutines.

程序源通常将由一系列子例程组成。 当您毕业于更高级的编程时,您会发现除子例程外,其他内容也可以成为程序的一部分。

This particular subroutine is named AboutVB1. The subroutine header must be paired with an End Sub at the bottom. The parenthesis can hold a parameter list consisting of values being passed to the subroutine. Nothing is being passed here, but they have to be there in the Sub statement anyway. Later, when we run the macro, we will look for the name AboutVB1.

该特定子例程名为AboutVB1 。 子例程头必须在底部与End Sub配对。 括号中可以包含一个参数列表,其中包含要传递给子例程的值。 这里没有传递任何内容,但是无论如何它们都必须在Sub语句中存在。 稍后,当我们运行宏时,我们将寻找名称AboutVB1

There is only one actual program statement in the subroutine:

子例程中只有一个实际的程序语句:

Selection.TypeText Text:="Hello World!"

Selection.TypeText文本:=“ Hello World!”

对象,方法和属性 ( Objects, methods and properties )

This statement contains the big three:

该语句包含三大部分:

  • an object

    一个东西
  • a method

    一个方法
  • a property

    财产

The statement actually adds the text "Hello World." to the contents of the current document.

该语句实际上添加了文本“ Hello World”。 当前文档的内容。

The next task is to run our program a few times. Just like buying a car, it's a good idea to drive it around for a while until it feels a little bit comfortable. We do that next.

下一个任务是运行我们的程序几次。 就像买车一样,最好先开车一段时间,直到感觉舒适为止。 接下来我们要做。

程序和文件 ( Programs and documents )

We have our glorious and complicated system ... consisting of one program statement ... but now we want to run it. Here's what that's all about.

我们拥有光荣而复杂的系统...由一个程序语句组成...但是现在我们要运行它。 这就是全部。

There's one concept to be learned here that is very important and it often really confuses first timers: the difference between the program and the document. This concept is foundational.

这里有一个非常重要的概念需要学习,它经常使初学者感到困惑: 程序文档之间的差异。 这个概念是基础。

VBA programs have to be contained in a host file. In Word, the host is the document. In our example, that's AboutVB1.docm. The program is actually saved inside the document.

VBA程序必须包含在主机文件中。 在Word中,宿主就是文档。 在我们的示例中,这是AboutVB1.docm 。 该程序实际上保存在文档中。

For example, if this was Excel, we would be talking about the program and the spreadsheet. In Access, the program and the database. Even in standalone Visual Basic Windows application, we would have a program and a form.

例如,如果这是Excel,那么我们将讨论程序电子表格 。 在Access中, 程序数据库 。 即使在独立的Visual Basic Windows应用程序中,我们也会有一个程序和一个form

(Note: There is a trend in programming to refer to all high level containers as a "document". This is specifically the case when XML ... another up and coming technology ... is being used. Don't let it confuse you. Although it's a slight inaccuracy, you can think of "documents" as being roughly the same as "files".)

(注意:编程中存在一种将所有高级容器称为“文档”的趋势。当使用XML ...另一种新兴技术...时尤其如此。不要让它造成混淆您。尽管这有点不准确,但您可以认为“文档”与“文件”大致相同。)

There are ... ummmmm .... about three main ways to run your VBA macro.

ummmmm ....关于运行VBA宏的三种主要方法。

  1. You can run it from the Word Document.

    您可以从Word文档运行它。

    (Note: Two subcategories are to select Macros from the Tools menu or just press Alt-F8. If you have assigned the macro to a Toolbar or Keyboard shortcut, that's one more way.))

    (注意:有两个子类别可以从“工具”菜单中选择“宏”,或者只是按Alt-F8键。如果已将宏分配给“工具栏”或“键盘”快捷方式,则是另一种方法。)

  2. You can run it from the Editor using the Run icon or Run menu.

    您可以使用“运行”图标或“运行”菜单从编辑器中运行它。
  3. You can single-step through the program in debug mode.

    您可以在调试模式下单步执行程序。

You should try every one of these methods just to become comfortable with the Word/VBA interface. When you finish, you will have a whole document filled with repeats of "Hello World!"

您应该尝试使用这些方法中的每一种,只是为了熟悉Word / VBA界面。 完成后,您将获得一个完整的文档,其中包含重复的“ Hello World!”。

Running the program from Word is fairly easy to do. Just select the macro after clicking the Macro icon under the View tab.

从Word运行程序非常容易。 单击“ 视图”选项卡下的“ 宏”图标后,只需选择宏即可。

To run it from the Editor, first open the Visual Basic editor and then either click the Run icon or select Run from the menu. Here's where the difference between the Document and the Program might become confusing to some. If you have the document minimized or perhaps have your windows arranged so the editor is covering it, you can click the Run icon over and over and nothing seems to happen. But the program is running! Switch to the document again and see.

要从编辑器中运行它,请首先打开Visual Basic编辑器,然后单击“运行”图标或从菜单中选择“运行”。 在这里,文档和程序之间的差异可能会使某些人感到困惑。 如果您将文档最小化或安排了窗口,以便编辑器覆盖它,则可以反复单击“运行”图标,似乎什么也没有发生。 但是程序正在运行! 再次切换到文档并查看。

Single stepping through the program is probably the most useful problem solving technique. This is also done from the Visual Basic editor. To try this out, press F8 or select Step Into from the Debug menu. The first statement in the program, the Sub statement, is highlighted. Pressing F8 executes the program statements one at a time until the program ends. You can see exactly when the text is added to the document this way.

单步执行程序可能是最有用的问题解决技术。 这也是从Visual Basic编辑器中完成的。 要尝试此操作,请按F8键或从“ 调试”菜单中选择“ 逐步进入 ”。 程序中的第一个语句Sub语句突出显示。 按F8一次一次执行程序语句,直到程序结束。 您可以确切地看到何时以这种方式将文本添加到文档中。

There are a lot of more refined debugging techniques such as 'Breakpoints', examining program objects in the 'Immediate Window', and the use of the 'Watch Window'. But for now, simply be aware that this is a primary debugging technique you will use as a programmer.

有许多更完善的调试技术,例如“断点”,在“即时窗口”中检查程序对象以及“监视窗口”的使用。 但是现在,仅需注意,这是您将用作程序员的主要调试技术。

面向对象编程 ( Object Oriented Programming )

The next class lesson is all about Object Oriented Programming.

下一课是关于面向对象编程的

"Whaaaattttt!" (I hear you moaning) "I just want to write programs. I didn't sign up to be a computer scientist!"

“哇!!!” (我听到你在抱怨)“我只想编写程序。我没有注册成为计算机科学家!”

Fear Not! There are two reasons why this is a great move.

不要怕! 这是一个重大举措的原因有两个。

First, in today's programming environment, you simply can't be an effective programmer without understanding object oriented programming concepts. Even our very simple one-line "Hello World" program consisted of an object, a method, and a property. In my opinion, not understanding objects is the biggest single problem beginning programmers have. So we're going to confront the beast right up front!

首先,在当今的编程环境中,如果不了解面向对象的编程概念,就无法成为有效的程序员。 甚至我们非常简单的单行“ Hello World”程序也由对象,方法和属性组成。 我认为,不了解对象是程序员开始面临的最大的单个问题。 因此,我们将直接面对野兽!

Second, we're going to make this as painless as possible. We're not going to confuse you with a load of computer science jargon.

其次,我们将使此过程尽可能轻松。 我们不会将您与大量的计算机科学术语混淆。

But right after that, we're going to jump right back into writing programming code with a lesson where we develop a VBA macro that you can probably use! We perfect that program a little more in the next lesson and finish up by showing you how to start using VBA with several applications at one time.

但是在那之后,我们将马上回到编写编程代码的一课,在那里我们开发一个可以使用的VBA宏! 在下一课中,我们将进一步完善该程序,并向您展示如何一次将VBA与多个应用程序一起使用,以完成该程序。

翻译自: https://www.thoughtco.com/learn-vba-macro-coding-with-word2007-3424057

word vba宏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值