{"id":4300,"date":"2014-06-09T04:00:52","date_gmt":"2014-06-09T08:00:52","guid":{"rendered":"http:\/\/americaneditor.wordpress.com\/?p=4300"},"modified":"2016-09-16T11:46:53","modified_gmt":"2016-09-16T17:46:53","slug":"lyonizing-word-formatting-with-macros","status":"publish","type":"post","link":"https:\/\/editorium.com\/archive\/lyonizing-word-formatting-with-macros\/","title":{"rendered":"Lyonizing Word: Formatting with Macros"},"content":{"rendered":"<h3 style=\"text-align:center;\">Formatting with Macros<\/h3>\n<p style=\"text-align:center;\"><em><strong>by Jack Lyon<\/strong><\/em><\/p>\n<p>Most users of Microsoft Word format text by selecting a paragraph and then applying a font. More advanced users apply a style. Why? Because then if they need to change the formatting of, say, level-2 headings, they can simply modify the style rather than tediously selecting each heading and applying a different font. (If you\u2019re reading this, you\u2019re probably one of those advanced users.) But there is a way to handle formatting that is even more powerful.<\/p>\n<p>Suppose that you\u2019ve dutifully applied styles to the various parts of a document, but then your client asks you to change the font\u2014everywhere in the document\u2014from Times New Roman to Adobe Garamond. You could manually modify each style, but if there are dozens of styles in use, there is a better way. That way is a macro, like this one:<\/p>\n<p style=\"padding-left:30px;\">Sub SetFontInAllStyles()<br \/>\nDim aStyle As Style<br \/>\nFor Each aStyle In ActiveDocument.Styles<\/p>\n<p style=\"padding-left:60px;\">aStyle.Font.Name = \"Adobe Garamond\"<\/p>\n<p style=\"padding-left:30px;\">Next<br \/>\nEnd Sub<\/p>\n<p>Well, that was easy. Let's look at each line of the macro (excluding the first and last lines, which simply define the beginning and end of the macro).<\/p>\n<p style=\"padding-left:30px;\">Dim aStyle As Style<\/p>\n<p>That line dimensions (defines) a variable, aStyle, as a style. (As with all variables, I just made up the name \u201caStyle.\u201d) At one point as the macro runs, aStyle might represent the style Heading 1. At another point it might represent Heading 3. But it will always represent a style.<\/p>\n<p style=\"padding-left:30px;\">For Each aStyle In ActiveDocument.Styles<\/p>\n<p>Here's where things get interesting. That line tells the macro to cycle through each style (represented by aStyle) in all of the styles in the active document (the document in which your cursor is currently sitting).<\/p>\n<p style=\"padding-left:30px;\">aStyle.Font.Name = \"Adobe Garamond\"<\/p>\n<p>That line tells Word to set the font for the style currently being represented by aStyle to be Adobe Garamond.<\/p>\n<p style=\"padding-left:30px;\">Next<\/p>\n<p>That line tells Word to go to the next style in the document.<\/p>\n<p>When you run the macro, it will cycle through each style in the document (For Each\u2026Next) and set Adobe Garamond as the font used in that style.<\/p>\n<p>But what if you want to change the font only in heading styles (Heading 1, Heading 2, and so on)? Try this:<\/p>\n<p style=\"padding-left:30px;\">Dim aStyle As Style<br \/>\nFor Each aStyle In ActiveDocument.Styles<\/p>\n<p style=\"padding-left:60px;\">If InStr(aStyle.NameLocal, \"Heading\") Then aStyle.Font.Name = \"Adobe Garamond\"<\/p>\n<p style=\"padding-left:30px;\">Next<br \/>\nEnd Sub<\/p>\n<p>Here's the line of interest:<\/p>\n<p style=\"padding-left:30px;\">If InStr(aStyle.NameLocal, \"Heading\") Then aStyle.Font.Name = \"Adobe Garamond\"<\/p>\n<p>The line uses a macro command we haven't seen before, InStr, which checks to see if a specific string of text is used somewhere. In this case, it checks to see if the text \u201cHeading\u201d appears in the name (NameLocal) of the style currently represented by aStyle. If it does, then the name of the font used in that style is set to Adobe Garamond.<\/p>\n<p>You could even specify the exact name of the style to be changed:<\/p>\n<p style=\"padding-left:30px;\">If aStyle.NameLocal = \"Block Quote\" Then aStyle.Font.Name = \"Adobe Garamond\"<\/p>\n<p>And that should give you an idea of how to modify a bunch of styles, all at once (between \u201cFor Each\u201d and \u201cNext\u201d), to use various fonts:<\/p>\n<p style=\"padding-left:30px;\">If aStyle.NameLocal = \"Poem\" Then aStyle.Font.Name = \"Arial\"<\/p>\n<p style=\"padding-left:30px;\">If aStyle.NameLocal = \"Author\" Then aStyle.Font.Name = \"Apple Boy\"<\/p>\n<p style=\"padding-left:30px;\">If aStyle.NameLocal = \"Subtitle\" Then aStyle.Font.Name = \"Constantia\"<\/p>\n<p>Much more can be done to automate the formatting of text using macros. I hope this brief article will get you started.<\/p>\n<h4>How to Add Macro to Word &amp; to the QAT<\/h4>\n<p>Here\u2019s how to put this macro (or any other) into Microsoft Word so it will be available when you need it:<\/p>\n<ol>\n<li>Copy the text of the macro, starting with the first \u201cSub\u201d and ending with the last \u201cSub.\u201d<\/li>\n<li>Click the \u201cView\u201d tab on Microsoft Word\u2019s ribbon.<\/li>\n<li>Click the \u201cMacros\u201d button.<\/li>\n<li>Type a name for the macro in the \u201cMacro name\u201d box \u2014 probably the name used after the first \u201cSub.\u201d For this macro, that\u2019s \u201cCleanCellEndSpaces.\u201d<\/li>\n<li>Click the \u201cCreate\u201d button.<\/li>\n<li>Delete the \u201cSub [macro name]\u201d and \u201cEnd Sub\u201d lines that Word created in the macro window. The macro window should now be completely empty (unless you already have other macros in there).<\/li>\n<li>Paste the macro text at the current insertion point.<\/li>\n<li>Click \u201cFile,\u201d then \u201cClose and Return to Microsoft Word.\u201d<\/li>\n<\/ol>\n<p>To actually use the macro:<\/p>\n<ol>\n<li>Place your cursor at the beginning of the document.<\/li>\n<li>Click the \u201cView\u201d tab on Microsoft Word\u2019s ribbon.<\/li>\n<li>Click the \u201cMacros\u201d button.<\/li>\n<li>Click the name of your macro to select it.<\/li>\n<li>Click the \u201cRun\u201d button. (If you wanted to delete the macro, you could press the\u00a0\u201cDelete\u201d button instead.)<\/li>\n<\/ol>\n<p>Here\u2019s how to put the macro on Word\u2019s QAT (Quick Access Toolbar):<\/p>\n<ol>\n<li>Locate the QAT (it\u2019s probably on the top left of your screen either above or below Word\u2019s Ribbon interface).<\/li>\n<li>Right-click the QAT.<\/li>\n<li>Click \u201cCustomize Quick Access Toolbar.\u201d<\/li>\n<li>Under \u201cChoose commands from:\u201d click the dropdown list and select \u201cMacros.\u201d<\/li>\n<li>Find and select your macro in the list on the left.<\/li>\n<li>Click the \u201cAdd\u201d button to add it to the QAT.<\/li>\n<li>Click the \u201cOK\u201d button to finish.<\/li>\n<\/ol>\n<p><em>Jack Lyon (<\/em><a href=\"mailto:editor@editorium.com\"><em>editor@editorium.com<\/em><\/a><em>)\u00a0owns and operates the <\/em><a title=\"The Editorium\" href=\"http:\/\/www.editorium.com\" target=\"_blank\"><em>Editorium<\/em><\/a><em>, which provides macros and information to help editors and publishers do mundane tasks quickly and efficiently. He is the author of <\/em><a title=\"Microsoft Word for Publishing Professionals\" href=\"http:\/\/www.editorium.com\/msword4pubpros.htm\" target=\"_blank\">Microsoft Word for Publishing Professionals<\/a> <em>and of <\/em><a title=\"Macro Cookbook at Barnes &amp; Noble\" href=\"http:\/\/www.barnesandnoble.com\/w\/macro-cookbook-for-microsoft-word-jack-m-lyon\/1107868228?ean=9781434103321\" target=\"_blank\">Macro Cookbook for Microsoft Word<\/a>.<em> Both books will help you learn more about macros and how to use them.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/editorium.com\/archive\/lyonizing-word-formatting-with-macros\/\" rel=\"bookmark\" title=\"Permalink to Lyonizing Word: Formatting with Macros\"><p>Most users of Microsoft Word format text by selecting a paragraph and then applying a font. More advanced users apply a style. Why? Because then if they need to change the formatting of, say, level-2 headings, they can simply modify the style rather than tediously selecting each heading and applying a different font. But there is a way to handle formatting that is even more powerful.<\/p>\n<\/a>","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[77,175,409],"tags":[553,267,446,491],"class_list":{"0":"post-4300","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-computers-and-software","7":"category-editing-tools-editorial-matters","8":"category-lyonizing-word","9":"tag-formatting-word-documents","10":"tag-jack-lyon","11":"tag-macros","12":"tag-microsoft-word","13":"h-entry","14":"hentry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3gfno-17m","_links":{"self":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/4300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/comments?post=4300"}],"version-history":[{"count":1,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/4300\/revisions"}],"predecessor-version":[{"id":6012,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/4300\/revisions\/6012"}],"wp:attachment":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/media?parent=4300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/categories?post=4300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/tags?post=4300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}