{"id":4204,"date":"2014-05-05T04:00:52","date_gmt":"2014-05-05T08:00:52","guid":{"rendered":"http:\/\/americaneditor.wordpress.com\/?p=4204"},"modified":"2016-09-16T11:46:52","modified_gmt":"2016-09-16T17:46:52","slug":"lyonizing-word-removing-spaces-at-the-ends-of-table-cells","status":"publish","type":"post","link":"https:\/\/editorium.com\/archive\/lyonizing-word-removing-spaces-at-the-ends-of-table-cells\/","title":{"rendered":"Lyonizing Word: Removing Spaces at the End of Table Cells"},"content":{"rendered":"<h3 style=\"text-align:center;\">Removing Spaces at the End of Table Cells<\/h3>\n<p style=\"text-align:center;\"><em><strong>by Jack Lyon<\/strong><\/em><\/p>\n<p>Authors do funny things. Sometimes these things are inadvertent; sometimes they\u2019re the result of trying to \u201cprettify\u201d documents for publication. In either case, editors have to clean up what the authors have done.<\/p>\n<p>One such problem is spaces at the ends of table cells. A table cell should end with the text it contains. If there are spaces after that text, they can cause alignment (and other) problems if they\u2019re allowed to persist into typesetting.<\/p>\n<p>It should be a simple matter to clean up the extraneous spaces: Search for a space followed by an end-of-cell marker and replace with just an end-of-cell marker. But what magic code can we use to find or replace an end-of-cell marker? As it turns out, there isn\u2019t one. But we can still get rid of those spaces with a macro. Here it is, with comments about what\u2019s going on (text following a single quotation mark is a \u201ccomment\u201d, which is also in <span style=\"color:#008000;\">green<\/span> for clarity):<\/p>\n<h4>The Macro<\/h4>\n<p style=\"padding-left:30px;\">Sub CleanCellEndSpaces()<\/p>\n<p style=\"padding-left:30px;\"><span style=\"color:#008000;\">'Define variables (that is, containers for information)<\/span><br \/>\nDim aTable As Table<br \/>\nDim aCell As Cell<br \/>\nDim aRow As Row<br \/>\nDim aColumn As Column<br \/>\nDim aRange As Range <span style=\"color:#008000;\">'That is, a specific area of the document<\/span><br \/>\nDim aLen As Integer <span style=\"color:#008000;\">'That is, a number<\/span><br \/>\nDim LastChar As String <span style=\"color:#008000;\">'That is, a string of characters (text)<\/span><\/p>\n<p style=\"padding-left:30px;\">Dim Tracking As Boolean <span style=\"color:#008000;\">'True or False<\/span><br \/>\nTracking = ActiveDocument.TrackRevisions <span style=\"color:#008000;\">'Get setting of revision tracking<\/span><br \/>\nActiveDocument.TrackRevisions = False <span style=\"color:#008000;\">'Turn off revision tracking<\/span><\/p>\n<p style=\"padding-left:30px;\">On Error Resume Next <span style=\"color:#008000;\">'In case of tables with \"vertically merged\" cells<\/span><br \/>\n<span style=\"color:#008000;\">'Cycle through tables, rows, and cells<\/span><\/p>\n<p style=\"padding-left:60px;\">For Each aTable In ActiveDocument.Tables<br \/>\nFor Each aRow In aTable.Rows<br \/>\nFor Each aCell In aRow.Cells<\/p>\n<p style=\"padding-left:30px;\">CheckAgain:<\/p>\n<p style=\"padding-left:60px;\">Set aRange = aCell.Range <span style=\"color:#008000;\">'Set aRange variable to the contents of the current cell<\/span><br \/>\naRange.End = aRange.End - 1 <span style=\"color:#008000;\">'Don't include the end-of-cell marker<\/span><br \/>\naLen = Len(aRange.Text) <span style=\"color:#008000;\">'Get the length of the cell's text<\/span><br \/>\naString = aRange.Text <span style=\"color:#008000;\">'Assign the text to a variable<\/span><br \/>\nLastChar = Right(aString, 1) <span style=\"color:#008000;\">'Get the last character of the text<\/span><br \/>\nIf LastChar = \" \" Then <span style=\"color:#008000;\">'If the last character is a space<\/span><\/p>\n<p style=\"padding-left:90px;\">aRange.Text = Left(aRange.Text, aLen - 1) <span style=\"color:#008000;\">'Set the text to be itself minus the trailing space<\/span><br \/>\nGoTo CheckAgain <span style=\"color:#008000;\">'Go back and check for another space (there may be several)<\/span><\/p>\n<p style=\"padding-left:60px;\">End If<br \/>\nNext aCell<br \/>\nNext aRow<br \/>\nNext aTable<\/p>\n<p style=\"padding-left:30px;\">ActiveDocument.TrackRevisions = Tracking <span style=\"color:#008000;\">'Set revision tracking back to its original state<\/span><\/p>\n<p style=\"padding-left:30px;\">End Sub<\/p>\n<h4>The Explanation<\/h4>\n<p>Here\u2019s how the macro works.<\/p>\n<p>We start by \u201cdimensioning\u201d (defining) our variables, like this:<\/p>\n<p style=\"padding-left:30px;\">Dim aTable As Table<\/p>\n<p>\u201caTable\u201d is an arbitrary name; I just made it up. But that whole line tells Word that aTable will represent a table in our document. The other \u201cDim\u201d statements follow suit, with \u201caCell\u201d representing a table cell, \u201caRow\u201d representing a table row, and so on.<\/p>\n<p>These three lines deserve special attention:<\/p>\n<p style=\"padding-left:30px;\">Dim Tracking As Boolean<br \/>\nTracking = ActiveDocument.TrackRevisions<br \/>\nActiveDocument.TrackRevisions = False<\/p>\n<p>Dimensioning the \u201cTracking\u201d variable as Boolean tells Word that the variable will be either true or false; those are the only two values it can hold.<\/p>\n<p>Next, we set \u201cTracking\u201d to the value of ActiveDocument.TrackRevisions. If revision tracking is on, \u201cTracking\u201d will be set to \u201cTrue.\u201d If revision tracking is off, \u201cTracking\u201d will be set to \u201cFalse.\u201d We do that to remember the current setting for revision tracking, because the next line, \u201cActiveDocument.TrackRevisions = False\u201d actually turns revision tracking off (we\u2019ll reset it later). This is necessary because (1) tracking the deletion of those extraneous spaces isn\u2019t needed, and (2) using revision tracking may send this macro into an endless loop as it keeps \u201cfinding\u201d the character that it just deleted (but is still there as a tracked revision).<\/p>\n<p>The next line \u2014<\/p>\n<p style=\"padding-left:30px;\">On Error Resume Next<\/p>\n<p>\u2014 needs to be there in case a table includes \u201cmerged\u201d cells, which will cause an error when the macro runs. If that happens, the macro will skip to the next line, which means that tables with \u201cmerged\u201d cells will be ignored. There may be a better way to deal with such tables, but I don\u2019t know what it is.<\/p>\n<p>After that line, things get really interesting:<\/p>\n<p style=\"padding-left:30px;\">For Each aTable In ActiveDocument.Tables<\/p>\n<p>This tells Word to work on \u201cEach\u201d table in ActiveDocument.Tables. \u201cWhat\u2019s that?\u201d you ask. Well, obviously \u201cActiveDocument\u201d is the active document \u2014 the document that\u2019s open in Word with our cursor moving around in it. (Other documents may be open but not active.) In the active document, there\u2019s a collection of objects called \u201cTables\u201d \u2014 which are, of course, the tables in the document.<\/p>\n<p>And now, a brief digression: As far as macros are concerned, a Microsoft Word document is \u201csimply\u201d a collection of various objects, such as tables, headers, footers, footnotes, endnotes, paragraphs, words, and much, much more. All of these objects have certain \u201cproperties.\u201d For example, a paragraph may have the property of FirstLineIndent set to \u201cTrue\u201d \u2014 in other words, its first line is indented. Objects can also contain other objects. For example, a table always contains at least one row and one column. So, in our macro, we have this:<\/p>\n<p style=\"padding-left:30px;\">For Each aRow In aTable.Rows<\/p>\n<p>That tells Word to work on each row in the current table. Similarly, this \u2014<\/p>\n<p style=\"padding-left:30px;\">For Each aCell In aRow.Cells<\/p>\n<p>\u2014 tells Word to work on each cell in the current row.<\/p>\n<p>Next, we\u2019re going to set the range of text we want to use (that is, aRange) to be the current cell:<\/p>\n<p style=\"padding-left:30px;\">Set aRange = aCell.Range<\/p>\n<p>Then we\u2019ll reduce the end of that range by one character, so we don\u2019t include the end-of-cell marker:<\/p>\n<p style=\"padding-left:30px;\">aRange.End = aRange.End - 1<\/p>\n<p>And, using the Len command, we\u2019ll find out the length (number of characters) included in the range\u2019s text:<\/p>\n<p style=\"padding-left:30px;\">aLen = Len(aRange.Text)<\/p>\n<p>Now let\u2019s get that text by putting it into the variable called \u201caString\u201d:<\/p>\n<p style=\"padding-left:30px;\">aString = aRange.Text<\/p>\n<p>And we\u2019ll use the Right command to find out the last character of the text string (that is, the first character on the right of the string):<\/p>\n<p style=\"padding-left:30px;\">LastChar = Right(aString, 1)<\/p>\n<p>That looks a little complicated, but it\u2019s actually fairly simple. Let\u2019s say our text string is \u201cHi, Mom!\u201d The \u201c1\u201d tells the Right command at which character to start counting (from the right of the string). In other words, LastChar is assigned the last character of the string, which in this case is an exclamation mark (\u201cHi, Mom!\u201d).<\/p>\n<p>But what if the last character is a space? That\u2019s what we really we want to know. The next line will tell us if that\u2019s the case:<\/p>\n<p style=\"padding-left:30px;\">If LastChar = \" \" Then<\/p>\n<p>If the last character is a space, we need to get rid of it, which we can do like this:<\/p>\n<p style=\"padding-left:30px;\">aRange.Text = Left(aRange.Text, aLen - 1)<\/p>\n<p>That line changes the text of our range to itself minus its last character (if the previous line identified its last character as a space). But what if there\u2019s more than one space? We want to get rid of those spaces too! And that\u2019s where the next line comes in:<\/p>\n<p style=\"padding-left:30px;\">GoTo CheckAgain<\/p>\n<p>That sends the macro back to the \u201clabel\u201d we\u2019ve created at this line:<\/p>\n<p style=\"padding-left:30px;\">CheckAgain:<\/p>\n<p>And the operation is repeated on the cell until no more spaces remain at the end of the cell.<\/p>\n<p>All of the \u201cNext\u201d commands that follow repeat the whole operation for every cell in every row in every table of the active document. Powerful stuff!<\/p>\n<p>Finally, the macro restores revision tracking to its original setting as stored in the \u201cTracking\u201d variable:<\/p>\n<p style=\"padding-left:30px;\">ActiveDocument.TrackRevisions = Tracking<\/p>\n<p>As they taught us in kindergarten, it\u2019s good to clean up after yourself.<\/p>\n<p>This article is a brief introduction to manipulating Word \u201cobjects\u201d with macros. Future articles may explore more of those objects, along with their \u201cproperties\u201d and \u201cmethods.\u201d If that\u2019s more than you want to know, you may still find the macros themselves to be useful.<\/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-removing-spaces-at-the-ends-of-table-cells\/\" rel=\"bookmark\" title=\"Permalink to Lyonizing Word: Removing Spaces at the End of Table Cells\"><p>Authors do funny things. Sometimes these things are inadvertent; sometimes they\u2019re the result of trying to \u201cprettify\u201d documents for publication. In either case, editors have to clean up what the authors have done.<\/p>\n<p>One such problem is spaces at the ends of table cells. A table cell should end with the text it contains. If there are spaces after that text, they can cause alignment (and other) problems if they\u2019re allowed to persist into typesetting.<br \/>\nThis macro solves that problem.<\/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,409],"tags":[489,490,267,446,491],"class_list":{"0":"post-4204","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-computers-and-software","7":"category-lyonizing-word","8":"tag-cleaning-up-manuscripts","9":"tag-delete-space-at-end-of-table-cell","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-15O","_links":{"self":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/4204","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=4204"}],"version-history":[{"count":1,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/4204\/revisions"}],"predecessor-version":[{"id":6001,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/4204\/revisions\/6001"}],"wp:attachment":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/media?parent=4204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/categories?post=4204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/tags?post=4204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}