The code to access the control would look something like this:
Click the Back button on your browser to return
Interfacing with VBA isn't the primary job of content controls, but you can do that too. Select Properties from the context menu above and give the control a Title. (myRichText is the Title I picked in the illustration below.)
Click the Back button on your browser to return
You may have seen (or even programmed) Word documents with bookmarks to indicate where text should be added. (I wrote a previous Word tutorial here showing how to do that.) The idea is that you can tab from one bookmark to the next to add unique information; for example, in a form letter based on a template. It's a good idea, but it's not very flexible for programming and document users can sometimes change bookmarks while editing and get the document confused. Content controls solve those problems. They're basically "super bookmarks" that don't have any of those limitations. Right click on the control in design mode to display the context menu as shown below.
In some cases, you have several choices for the same type of control. For example, there are two kinds of content control textboxes (rich text and "plain" text) and a textbox in both legacy forms and ActiveX controls. How is a coder to choose? Let's look at the differences.
I should say, second "ways" because you can now use three different kinds of controls.
Click the Back button on your browser to return
The second way we're going to program a "Hello, World" app will be to use the controls available in VBA. You'll find them in the Developer tab as shown below:
That may not be exactly what you need, but it gives you a huge head start.
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveUp Unit:=wdLine, Count:=1
One of the main reasons that recording macros is so effective is that you simply can't remember all of the VBA syntax all of the time. Recording a macro will often be the clue that tells you how things can be made to work. It's like having an expert at your call whenever you need some help. For example, if you need to know what statements will copy and paste in Word VBA, just record a macro that copies and pastes and you will see:
Notice, also, that this macro is not an "event procedure". The only way to execute it is to select it and run it. You can assign it to a toolbar or a keyboard combination, but that amounts to the same thing.
In the case of Word, the other choice is the Normal template, which is another type of document. If you're a Word expert, you know that regular Word documents can also be saved as templates so their content can be used as a starting point for similar documents. The Normal template is the Word document that is the starting point for every new document. VBA macros that you save there are available to every new document as well.
Click the Back button on your browser to return
VBA gives you two choices about which document to use. Most Office applications are "document centered" because there is an "ActiveDocument" being processed. If you save a macro in the currently active document, then the macro will only be available to that document in the future. If you have a highly specialized macro that only applies to this one document, select the currently active document.
As simple as it is, there's still a lot to learn from this macro. The first thing to learn is the file organization that Word 2007 uses for macros. One of the instructions above is to "select a location". That brings up the question, "Where does Word save macro code?" The answer, for Word and for the other Office applications, is "In the document itself." Part of the actual document you save is the macro code.
Using VBA in Office 2007All Of The Ways To Say Hello, WorldFrom , former About.com Guide
Introduction to VBA in Word for Office 2007
Комментариев нет:
Отправить комментарий