{"id":1004,"date":"2014-06-05T12:35:50","date_gmt":"2014-06-05T18:35:50","guid":{"rendered":"http:\/\/editorium.com\/archive\/?p=1004"},"modified":"2014-06-05T12:36:56","modified_gmt":"2014-06-05T18:36:56","slug":"removing-spaces-at-the-end-of-table-cells","status":"publish","type":"post","link":"https:\/\/editorium.com\/archive\/removing-spaces-at-the-end-of-table-cells\/","title":{"rendered":"Removing Spaces at the End of Table Cells"},"content":{"rendered":"<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<h3>The Macro<\/h3>\n<pre style=\"padding-left: 30px;\">Sub CleanCellEndSpaces()<\/pre>\n<pre style=\"padding-left: 30px;\"><span style=\"color: #008000;\">'Define variables (that is, containers for information)<\/span>\r\n Dim aTable As Table\r\n Dim aCell As Cell\r\n Dim aRow As Row\r\n Dim aColumn As Column\r\n Dim aRange As Range <span style=\"color: #008000;\">'A specific area of the document\r\n<\/span> Dim aLen As Integer <span style=\"color: #008000;\">'A number<\/span>\r\n Dim LastChar As String <span style=\"color: #008000;\">'A string of characters (text)<\/span><\/pre>\n<pre style=\"padding-left: 30px;\">Dim Tracking As Boolean <span style=\"color: #008000;\">'True or False<\/span>\r\n Tracking = ActiveDocument.TrackRevisions <span style=\"color: #008000;\">'Get setting of revision tracking<\/span>\r\n ActiveDocument.TrackRevisions = False <span style=\"color: #008000;\">'Turn off revision tracking<\/span><\/pre>\n<pre style=\"padding-left: 30px;\">On Error Resume Next <span style=\"color: #008000;\">'In case of tables with \"vertically merged\" cells<\/span>\r\n <span style=\"color: #008000;\">'Cycle through tables, rows, and cells<\/span><\/pre>\n<pre style=\"padding-left: 60px;\">For Each aTable In ActiveDocument.Tables\r\n For Each aRow In aTable.Rows\r\n For Each aCell In aRow.Cells<\/pre>\n<pre style=\"padding-left: 30px;\">CheckAgain:<\/pre>\n<pre style=\"padding-left: 60px;\">Set aRange = aCell.Range <span style=\"color: #008000;\">'Set aRange variable to the contents of the current cell<\/span>\r\n aRange.End = aRange.End \u2013 1 <span style=\"color: #008000;\">'Don\u2019t include the end-of-cell marker<\/span>\r\n aLen = Len(aRange.Text) <span style=\"color: #008000;\">'Get the length of the cell\u2019s text<\/span>\r\n aString = aRange.Text <span style=\"color: #008000;\">'Assign the text to a variable<\/span>\r\n LastChar = Right(aString, 1) <span style=\"color: #008000;\">'Get the last character of the text<\/span>\r\n If LastChar = \" \" Then <span style=\"color: #008000;\">'If the last character is a space<\/span><\/pre>\n<pre style=\"padding-left: 90px;\">aRange.Text = Left(aRange.Text, aLen \u2013 1) <span style=\"color: #008000;\">'Set the text to be itself minus the trailing space<\/span>\r\n GoTo CheckAgain <span style=\"color: #008000;\">'Go back and check for another space (there may be several)<\/span><\/pre>\n<pre style=\"padding-left: 60px;\">End If\r\n Next aCell\r\n Next aRow\r\n Next aTable<\/pre>\n<pre style=\"padding-left: 30px;\">ActiveDocument.TrackRevisions = Tracking <span style=\"color: #008000;\">'Set revision tracking back to its original state<\/span><\/pre>\n<p style=\"padding-left: 30px;\">End Sub<\/p>\n<h3>The Explanation<\/h3>\n<p>Here\u2019s how the macro works.<\/p>\n<p>We start by \u201cdimensioning\u201d (defining) our variables, like this:<\/p>\n<pre style=\"padding-left: 30px;\">Dim aTable As Table<\/pre>\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<pre style=\"padding-left: 30px;\">Dim Tracking As Boolean\r\n Tracking = ActiveDocument.TrackRevisions\r\n ActiveDocument.TrackRevisions = False<\/pre>\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<pre style=\"padding-left: 30px;\">On Error Resume Next<\/pre>\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<pre style=\"padding-left: 30px;\">For Each aTable In ActiveDocument.Tables<\/pre>\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<pre style=\"padding-left: 30px;\">For Each aRow In aTable.Rows<\/pre>\n<p>That tells Word to work on each row in the current table. Similarly, this \u2014<\/p>\n<pre style=\"padding-left: 30px;\">For Each aCell In aRow.Cells<\/pre>\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<pre style=\"padding-left: 30px;\">Set aRange = aCell.Range<\/pre>\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<pre style=\"padding-left: 30px;\">aRange.End = aRange.End \u2013 1<\/pre>\n<p>And, using the Len command, we\u2019ll find out the length (number of characters) included in the range\u2019s text:<\/p>\n<pre style=\"padding-left: 30px;\">aLen = Len(aRange.Text)<\/pre>\n<p>Now let\u2019s get that text by putting it into the variable called \u201caString\u201d:<\/p>\n<pre style=\"padding-left: 30px;\">aString = aRange.Text<\/pre>\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<pre style=\"padding-left: 30px;\">LastChar = Right(aString, 1)<\/pre>\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<pre style=\"padding-left: 30px;\">If LastChar = \" \" Then<\/pre>\n<p>If the last character is a space, we need to get rid of it, which we can do like this:<\/p>\n<pre style=\"padding-left: 30px;\">aRange.Text = Left(aRange.Text, aLen \u2013 1)<\/pre>\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<pre style=\"padding-left: 30px;\">GoTo CheckAgain<\/pre>\n<p>That sends the macro back to the \u201clabel\u201d we\u2019ve created at this line:<\/p>\n<pre style=\"padding-left: 30px;\">CheckAgain:<\/pre>\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<pre style=\"padding-left: 30px;\">ActiveDocument.TrackRevisions = Tracking<\/pre>\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<h2>Programs from the Editorium<\/h2>\n<p>Have you checked out the Editorium's latest Microsoft Word add-ins to help with your work?<\/p>\n<p><a href=\"http:\/\/www.editorium.com\/IndexLinker.htm\" target=\"_blank\">IndexLinker<\/a> creates hyperlinks from index page numbers back to the text to which they refer. If you're creating ebooks or PDFs with indexes, you need this program.<\/p>\n<p><a href=\"http:\/\/www.editorium.com\/BookMaker.htm\" target=\"_blank\">BookMaker<\/a> automates typesetting and page layout in Microsoft Word. Stop fighting with page breaks, headers, and footers. Let BookMaker do the heavy lifting.<\/p>\n<p><a href=\"http:\/\/www.editorium.com\/lyxconverter.htm\" target=\"_blank\">LyXConverter<\/a> converts Word documents into <a href=\"http:\/\/www.lyx.org\" target=\"_blank\">LyX<\/a> documents.<\/p>\n<p>And, of course, many other useful add-ins are available as well, including <a href=\"http:\/\/www.editorium.com\/14857.htm\" target=\"_blank\">Editor's ToolKit Plus<\/a>.<\/p>\n<h2 id=\"line\">A Special Deal: Editor's Toolkit Ultimate!<\/h2>\n<p><a href=\"http:\/\/www.editorium.com\/etkultimate.htm\" target=\"_blank\">Editor's ToolKit Ultimate<\/a> combines three great products:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.editorium.com\/14857.htm\" target=\"_blank\">Editor's ToolKit Plus<\/a> from The Editorium.<\/li>\n<li><a href=\"http:\/\/www.intelligentediting.com\/download.aspx\" target=\"_blank\">PerfectIt Pro<\/a> from Intelligent Editing.<\/li>\n<li><a href=\"http:\/\/wordsnsync.com\/download.php\" target=\"_blank\">EditTools<\/a> from WordnSync.<\/li>\n<\/ul>\n<p>The three products work together to create a powerful editing package to take you through <a href=\"http:\/\/americaneditor.wordpress.com\/2010\/08\/03\/the-3-stages-of-copyediting-i-the-processing-stage\/\" target=\"_blank\">three separate stages of copyediting<\/a>.<\/p>\n<h2 id=\"line\">Communication Conference<\/h2>\n<p>Wondering how to launch or improve an editorial business, whether you offer writing, editing, proofreading, indexing or other related services? Come to \u201cBe a Better Freelancer! (Re)Invent Your Business,\u201d the ninth annual Communication Central conference for freelancers, September 26\u201327, 2014, in Rochester, NY, with an Editorial Bootcamp on September 28 at the same location.<\/p>\n<p>Topics include launching your business, macros and other efficiency\/productivity tools, working with MS Office, organization tips, a self-publishing roundtable, balancing freelancing and family life, resources, benefiting from social media, and more. Keynote speaker is Jake \u201cDr. Freelance\u201d Poinier. Other speakers include Erin Brenner, Ally Machate, April Michelle Davis, Daniel Heuman, Katharine O\u2019Moore-Klopf, Dick Margulis, Greg Ioannou, Geoff Hart, Jack Lyon, Laura Poole, Ben Davis, Amy Schneider, and Ruth E. Thaler-Carter.<\/p>\n<p>Interested in Laura Poole\u2019s editorial bootcamp? Info for the Editorial Bootcamp is included in the conference registration PDF. The Editorial Bootcamp may be taken without attending the conference.<\/p>\n<p>You'll find more information here:<\/p>\n<p>http:\/\/www.communication-central.com\/<\/p>\n<p>Questions? Contact Communication Central owner Ruth E. Thaler-Carter, at <a href=\"mailto:conference@communication-central.com\">conference@communication-central.com<\/a> or 585-248-0318.<\/p>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/editorium.com\/archive\/removing-spaces-at-the-end-of-table-cells\/\" rel=\"bookmark\" title=\"Permalink to 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. One such problem is spaces at the ends of table cells. A table cell should end with the text it contains. If [&hellip;]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","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":[3,6],"tags":[],"class_list":{"0":"post-1004","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-editing","7":"category-macros","8":"h-entry","9":"hentry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3gfno-gc","_links":{"self":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/1004","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/comments?post=1004"}],"version-history":[{"count":5,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/1004\/revisions"}],"predecessor-version":[{"id":1009,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/posts\/1004\/revisions\/1009"}],"wp:attachment":[{"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/media?parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/categories?post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editorium.com\/archive\/wp-json\/wp\/v2\/tags?post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}