VBA for Technical Writers a.k.a what to do when the macro recorder isn’t enough Adrian Morse Documentation Manager at Picis
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
<ul><li>Macros automate tasks that we expect to repeat   </li></ul><ul><li>Macros can be stored in the document or the “No...
<ul><li>Guesses the user intentions — interprets actions in the most primitive way </li></ul><ul><li>No user interface (fo...
The blue monkey DEMO
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
<ul><li>ALT+F11  opens the VBA Editor </li></ul><ul><li>There is a “project” for the Normal template and each open documen...
<ul><li>ThisDocument  stores any code you want to run in response to an action (event). </li></ul><ul><li>Example:  When t...
Macros start with “Sub” Comments added automatically Actions performed  by the user Macro name The VBA Editor:  Inside the...
The  bold  blue monkey DEMO
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
<ul><li>VBA is based on the idea of “objects” </li></ul><ul><li>An object is anything that users can see and manipulate in...
<ul><li>Objects are based on classes, which are like templates for the object. You can create objects from existing classe...
<ul><li>Its  properties  are its physical characteristics—model, colour, engine size… </li></ul><ul><li>Its  methods  are ...
Basic VBA Concepts:  Methods and properties Selection.Font. Color Selection. TypeText Object s Method Property <ul><li>The...
Basic VBA Concepts:  Events <ul><li>The object is the active document. The event is “open” </li></ul><ul><li>Many objects ...
<ul><li>Search for an object name in the help  TOC  to see methods, properties & events for that object  </li></ul>Basic V...
The  responsive  blue monkey DEMO
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
Adding a User Interface:  What you can do
The  inquisitive  blue monkey DEMO
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
<ul><li>For x = 1 To 10 </li></ul><ul><li>Selection.TypeText Text:=&quot;monkey&quot; </li></ul><ul><li>Selection.MoveLeft...
The  loopy  blue monkey DEMO
<ul><li>For x = 1 To 10 </li></ul><ul><li>Selection.TypeText Text:=&quot;monkey&quot; </li></ul><ul><li>Selection.MoveLeft...
The  increasingly loopy  blue monkey DEMO
<ul><li>Load UserForm1 </li></ul><ul><li>UserForm1.Show  </li></ul>Adding Intelligence:  Conditions There are different wa...
The  conditional  blue monkey DEMO
<ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding ...
<ul><li>Play around with recording and editing macros </li></ul><ul><li>Look at the Word MVPS site (http://word.mvps.org/F...
That’s all folks!
Upcoming SlideShare
Loading in …5
×

VBA for technical writers

1,242 views

Published on

A session delivered by Adrian Morse at Technical Communication UK 2011. A sample script file is also available from the Technical Communication UK website.
There is a sample script available (http://www.slideshare.net/TCUK/vba-for-technical-writers-demo-script).

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,242
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • introduction to the Visual Basic for Applications functionality that ships with Microsoft Office products. VBA taster session _______________________
  • _______________________
  • _______________________
  • _______________________
  • Cannot handle errors - for example, if a macro changes the border colour of a selected table and we run the macro when no table is selected we’ll get an error message. _______________________
  • The primary objective of the demos is to reinforce the concepts needed to start creating and editing macros. So, realism sacrificed for simplicity. All demos are variations of a simple macro called the blue monkey . _______________________
  • _______________________
  • _______________________
  • _______________________
  • Comments are ignored when the code is run. When you start editing and creating macros it is good practice to add comments throughout the code. You do this by starting the line with an apostrophe. Note the value for bolding is “wdToggle”. This relates to the limitation– the macro recorder guesses the user’s intentions. It assumes when I clicked the B button on the toolbar while creating macro my intention was to toggle the state of the button rather than to apply bold to the selected text. So, now if I were to click the B button before running the macro the code would toggle the bold off and the word “monkey” would be in regular font. This is not what I wanted. To overcome this I am going to edit the macro. _______________________
  • First I am going to show you the problem… &gt;Side by side, bold turned off, F8 to run macro. Now bold turned on and run macro again. Now I am going to fix this by changing the selection.font.bold setting. &gt;fix and rerun macro _______________________
  • _______________________
  • Last bullet point….The hierarchy is known as the Object Model. Each MS Office application has a different object model. _______________________
  • You can go a long way without ever needing to create new classes. _______________________
  • _______________________
  • This slide shows an example of a method and a property for the object called “Selection”. The Blue Monkey macro code uses this object and in fact the macro recorder often bases its code on this object. Typetext is an action that the object performs. It means it places the text on the page. Color is a property of the selection that can be changed. _______________________
  • Some objects have events associated with them. Example…the “Document” object can be opened so it has an event called “open” associated with it. VBA makes it particularly easy to access events for the Document object. It does this using the ThisDocument file. This is one of the standard files that I showed you earlier. If you click in the dropdown at the top right you will see a list of the most common event types for the Document object. So you just need to choose the event that you want to trigger your code and then add the code. _______________________
  • There are various ways to get help with creating macros… Note that Methods and properties are collectively referred to as “members” in the Object Browser and help file. _______________________
  • _______________________
  • _______________________
  • You can add all sorts of controls to a form. It’s easy…you just drag, drop and resize them… _______________________
  • So now the blue monkey is going to ask us a question…. &gt;Create userform and add code to Thisdocument. Reopen doc. _______________________
  • There are many ways of adding intelligence to a macro. I am going to talk about two of those: looping and applying conditions.
  • So we’ve seen that when we record a macro it can set the properties of an object and makes that object do something. Now suppose we want to make that object do something repeatedly. If we had all day to spare we could just record a much longer macro where we repeat tasks over and over again. However, the VB Editor gives us an easy way to repeat a segment of code using a technique called looping . _______________________
  • So now we’re going to code the blue monkey to repeat itself… &gt;Open yes button code and add For x = 1 to 10, next x …. &gt;Reopen doc ______________________
  • Now let’s suppose we not only want to repeat the code but we also want to make a change each time the code runs. X is already incrementing by 1 with each loop so we’re going to take advantage of that to increment the point size in the code in the loop. When x is 1 we add 2pts. When x is 2 we add 4pts Etc _______________________
  • So now I am going to add the code for increasing the font size… &gt;Open yes button code and add change font setting to 24 + 2*x , Reopen the doc…show increasing font size _______________________
  • Now suppose we only want a segment code to be run in certain conditions. There are various ways to do this. One is with an IF…THEN…ELSE construction. For this example, we only want to run the blue monkey macro if the document filename is Blue.docm…
  • So now I’m going to edit the macro so that the window only pops up if the document has the name blue.docm. &gt;Add the following to the code in the ThisDocument and reopen If ActiveDocument.Name = &amp;quot;Blue.docm&amp;quot; Then … Else: MsgBox (“Rename the document to Blue.docm&amp;quot;) End If &gt;Reopen doc. &gt;Close doc and rename to Blue.docm &gt;Reopen doc. _______________________
  • _______________________
  • _______________________
  • Actually blue monkeys do exist… in Tanzania! _______________________
  • VBA for technical writers

    1. 1. VBA for Technical Writers a.k.a what to do when the macro recorder isn’t enough Adrian Morse Documentation Manager at Picis
    2. 2. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Topics
    3. 3. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Topics
    4. 4. <ul><li>Macros automate tasks that we expect to repeat </li></ul><ul><li>Macros can be stored in the document or the “Normal” template </li></ul><ul><li>Can create a toolbar button and keyboard shortcut for the macro. </li></ul>The Macro Recorder: Introduction
    5. 5. <ul><li>Guesses the user intentions — interprets actions in the most primitive way </li></ul><ul><li>No user interface (for showing data or allowing user to enter it) </li></ul><ul><li>No “intelligence” — for example, it cannot take action based on the outcome of previous events </li></ul><ul><li>Cannot handle errors </li></ul>The Macro Recorder: Limitations
    6. 6. The blue monkey DEMO
    7. 7. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Topics
    8. 8. <ul><li>ALT+F11 opens the VBA Editor </li></ul><ul><li>There is a “project” for the Normal template and each open document </li></ul><ul><li>You may also see a project for any addins you have </li></ul>The VBA Editor: The project pane i
    9. 9. <ul><li>ThisDocument stores any code you want to run in response to an action (event). </li></ul><ul><li>Example: When the doc is opened or closed </li></ul><ul><li>Forms stores any GUI windows that you want to show to the reader (to provide info or so the user can enter data) </li></ul><ul><li>Modules stores macros you create with the recorder or directly in the VBA Editor </li></ul>The VBA Editor: The project pane ii
    10. 10. Macros start with “Sub” Comments added automatically Actions performed by the user Macro name The VBA Editor: Inside the blue monkey Macros end with “End Sub”
    11. 11. The bold blue monkey DEMO
    12. 12. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Topics
    13. 13. <ul><li>VBA is based on the idea of “objects” </li></ul><ul><li>An object is anything that users can see and manipulate in some way e.g. a paragraph, a table, a text selection… </li></ul><ul><li>Objects are arranged in a hierarchy with object classes including other objects. </li></ul><ul><li>Example: The ‘Document’ object contains the ‘Paragraph’ object </li></ul>Basic VBA Concepts: Objects i
    14. 14. <ul><li>Objects are based on classes, which are like templates for the object. You can create objects from existing classes or define your own classes. </li></ul><ul><li>Example : You could create an object class to keep track of the time you spend working on documents during the day. </li></ul><ul><li>You can interact with an object in three ways: </li></ul><ul><li>Change a property associated with it </li></ul><ul><li>Make it perform a task by activating an associated method </li></ul><ul><li>Define a procedure that runs whenever an event affects the object </li></ul>Basic VBA Concepts: Objects ii
    15. 15. <ul><li>Its properties are its physical characteristics—model, colour, engine size… </li></ul><ul><li>Its methods are actions the car can do—accelerate, brake, turn… </li></ul><ul><li>Its events are actions that can be performed on it—turn the ignition key, press the horn, open the door… </li></ul>Basic VBA Concepts: Object analogy You can think of an object like a car…
    16. 16. Basic VBA Concepts: Methods and properties Selection.Font. Color Selection. TypeText Object s Method Property <ul><li>The macro recorder often bases its code on the “Selection” object, one of the most important objects in Word VBA </li></ul>
    17. 17. Basic VBA Concepts: Events <ul><li>The object is the active document. The event is “open” </li></ul><ul><li>Many objects do not have events associated with them </li></ul><ul><li>This code in the ThisDocument file… </li></ul>… causes this message box to be shown when the document is opened
    18. 18. <ul><li>Search for an object name in the help TOC to see methods, properties & events for that object </li></ul>Basic VBA Concepts: Getting help <ul><li>Type a dot after an object name to see all possible methods and properties for that object </li></ul><ul><li>Use the object browser </li></ul>
    19. 19. The responsive blue monkey DEMO
    20. 20. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Topics
    21. 21. Adding a User Interface: What you can do
    22. 22. The inquisitive blue monkey DEMO
    23. 23. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Topics
    24. 24. <ul><li>For x = 1 To 10 </li></ul><ul><li>Selection.TypeText Text:=&quot;monkey&quot; </li></ul><ul><li>Selection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend </li></ul><ul><li>Selection.Font.Bold = True </li></ul><ul><li>Selection.Font.Name = &quot;Arial&quot; </li></ul><ul><li>Selection.Font.Size = 24 </li></ul><ul><li>Selection.Font.Color = 15773696 </li></ul><ul><li>Selection.MoveRight Unit:=wdCharacter, Count:=1 </li></ul><ul><li>Selection.TypeParagraph </li></ul><ul><li>Next x </li></ul>Adding Intelligence: Loops i Repeating the code is called looping . There are many ways to do it. This example shows a simple ‘For…Next’ loop. Means “repeat the code below for each value of x between 1 and10”.
    25. 25. The loopy blue monkey DEMO
    26. 26. <ul><li>For x = 1 To 10 </li></ul><ul><li>Selection.TypeText Text:=&quot;monkey&quot; </li></ul><ul><li>Selection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend </li></ul><ul><li>Selection.Font.Bold = True </li></ul><ul><li>Selection.Font.Name = &quot;Arial&quot; </li></ul><ul><li>Selection.Font.Size = 24 + 2*x </li></ul><ul><li>Selection.Font.Color = 15773696 </li></ul><ul><li>Selection.MoveRight Unit:=wdCharacter, Count:=1 </li></ul><ul><li>Selection.TypeParagraph </li></ul><ul><li>Next x </li></ul>Adding Intelligence: Loops ii In this loop we increase the font size by 2 points each loop
    27. 27. The increasingly loopy blue monkey DEMO
    28. 28. <ul><li>Load UserForm1 </li></ul><ul><li>UserForm1.Show </li></ul>Adding Intelligence: Conditions There are different ways to apply conditions. This example shows a simple ‘If…Then’ loop If ActiveDocument.Name = &quot;Blue.docm&quot; Then Else: MsgBox (“Rename the document to Blue.docm&quot;) End If Means “run the code below if the document file name is Blue.docm ” Means “otherwise (if the document is not named Blue.docm ) show this message…
    29. 29. The conditional blue monkey DEMO
    30. 30. <ul><li>The macro recorder </li></ul><ul><li>The VBA Editor </li></ul><ul><li>Basic VBA concepts </li></ul><ul><li>Adding a user interface </li></ul><ul><li>Adding “intelligence” </li></ul>Key Points to Remember Great for getting started but many limitations ALT+F11 opens the editor Macros are based on objects that have properties , methods & events . You can easily create a GUI and add code “behind” its controls You can enhance code with looping, conditions…
    31. 31. <ul><li>Play around with recording and editing macros </li></ul><ul><li>Look at the Word MVPS site (http://word.mvps.org/FAQs/MacrosVBA/) </li></ul><ul><li>Get a book! </li></ul><ul><li>Join a forum…e.g. the VBA forum on the MSDN site </li></ul>What Next?
    32. 32. That’s all folks!

    ×