Hiding certain footnote texts with VBA in a Word table

Posted on 2012-09-05
Last Modified: 2012-09-07
Using VBA with Word 2010, I have a table with some footnotes inside certain cells. The footnote texts appear at the bottom of the page.
     From time to time I need to hide some rows in the table, which I do by moving those rows to the end of the table, selecting the rows, and setting the font to just 3 points, the font color to transparent, and the font hidden status to true. That seems far easier to do than to copy the rows and the footnotes into document variables and then restoring them, including formatting and footnotes, from the document variables.  
     With the method of hiding rows at the end and reformatting them to be “invisible,” the rows disappear, but the footnote texts from the hidden rows still appear at the bottom of the page.

     If I could find out which footnotes are in the hidden rows at the end, I suppose I could use something like

     ActiveDocument.Footnotes.Item(i).Reference.Font.Hidden = True
     ActiveDocument.Footnotes.Item(i).Range.Font.Hidden = True

But how could I find the value of the “i" in the above lines?

Thanks for any suggestions.

j.r.a. in Priddis, Alberta
Question by:JohnRobinAllen
    LVL 10

    Accepted Solution

    If you leave the rows where they are, select the entire row, and set the Hidden attribute of the font, the rows and the corresponding footnotes disappear except when you display all. (At least that's what happens in Word 2007.)

    I've highlighted the places you click on the home tab to change the font to hidden and to toggle "Display All".


    Author Comment

    Thanks for the suggestion. I will work on it and get back to you in about ten minutes.
        Something has been bothering me: sometimes when I worked on the program in the past, the footnotes did disappear when I hid the rows, but not they are not doing so now. Your suggestion might be the reason why it sometimes worked. That would save me a bushel of programming work.
    LVL 23

    Expert Comment

    Also, you want to put some code in to ensure hidden text is not set to be printed, otherwise that will defeat the purpose of setting the text font to hidden - e.g.

    Application.Options.PrintHiddenText = False

    Author Closing Comment

    I tested tdleewis’s solution manually, first by selecting the cell with the footnote and then I pressed Ctrl + D to control the font settings. I clicked the Hidden button to hide the selection, pressed Enter, and the cell disappeared. The footnote remained. That was the problem.
         I then tried selecting all three visible cells (some invisible cells are to the right of the table for sorting purposes). I again manually hid the content of the cells, including the cell with the footnote reference. The cells disappeared and the footnotes remained at the bottom of the page.
         Going back over your response, I saw that you said I should select the entire row to hide, so I did. That worked. The key is to select the whole row, even though what you select may already be hidden.
         Indeed, it worked so well that I am reconsidering my method of hiding rows by moving them to the bottom of the table before I hide them. I would like to simplify life by not using that method, but I have two groups of rows hidden and I want the user to be able to restore one group without the other. The first group of rows hidden is major: the user selects the rows she wants to study, and then she tells the computer to hide all the other rows. The second set of hidden rows is multiple: the user sees that she no longer needs a visible row or two and so deletes them. She deletes more as she continues to work.
         Eventually she will want to undelete the rows she deleted by selecting and deleting, without restoring the first group of rows that she deleted by selecting words to keep while deleting the rows not selected.
         Solution: When a user selects rows to isolate them by hiding the non-selected rows, move the latter (both the rows above the selection and below the selection) to the bottom of the table and hide them à la tdlewis. (I.e., select the entire row, even the parts that are already hidden, before specifying that the selection should be hidden.)
         As the user then selects rows to delete (i.e., by selecting what she wants to delete), then delete them again à la tdlewis but do not bother moving them. When the user wants to restore the latter, we undelete them by selecting all the rows down to the start of the rows hidden at the bottom of the table. We then make the selection visible.
         Later, when the user wants to restore the original rows hidden at the bottom of the table, we restore everything: both groups of hidden stuff. Select the whole table and designate everything visible.
         tdlewis has saved me a lot of trouble not only with the footnote problem, but also with my my hiding code. I’m grateful to him (or her, whichever is correct) and grateful to EE which almost always comes through with a solution to insolvable problems.

         John Robin (Allen)
         Priddis, Alberta, Canada
    LVL 10

    Expert Comment

    It's "him".... and you're welcome.

    Author Comment

    irudyk add a comment about ensuring that hidden text should not be set to print, a comment I saw only after I had finished writing my evaluation and had awarded the points.

    Thank you very much, irudyk, for the heads-up. After I read your comment, I did indeed check it and, for what it is worth, there seems to be no problem. Rows I had hidden with tdlewis's command do not get printed, at least on my machine. Perhaps that comment applies to some other language than VBA. I do not even know how I would set a font not to print with VBA.


    Author Comment

    I have a further note about selecting rows in a table. I appreciated tdleewis’s suggestion that if I want to hide footnote text at the botttom of a page, I should select an entire row rather than just a cell with a footnote reference number. With the row selected, (Selection.Font.Hidden = True), that solved my problem on hiding footnotes beautifully.
         However, it also raised another problem that took me a couple of days to to solve. When I hide a row (and its footnote in a cell), I also cut and paste the row to the bottom of the table where I hide it. (The reasons why I have to move the rows are not important in this discussion. I just have to move the data.)
         My code worked well until I implemented the solution above. After I began routinely using .RowSelect to select all the cells in a row, I couldn’t move the rows to the end without creating extra empty rows.
         Details: Assume that
              Tbl is the table with the data to hide
              a cell the row(s) to hide is selected,
              EndOfData is the last row in the table after the deletion,
              RowsToHide is the number of rows we will hide,

         Then here is the code that gives the problem:
              With Selection
                      .Rows.Delete  '  Cut won't delete the rows, so I use Copy & Delete
                      .InsertRowsBelow RowsToHide
                      .PasteAndFormat (wdFormatOriginalFormatting)
              End With
         (Then use .RowSelect to select the moved data and then set Selection.Font.Hidden = True)

         The problem with the code is that the .PasteAndFormat command produces strange results. Usually it puts the data beneath the inserted row, which then means I have to go back and delete the empty inserted row. If I do not insert the rows below, then the PasteAndFormat wipes out rows in the table.
         The solution is not to use .RowSelect when selecting the row to copy.
         Specify exactly the cells to select. I have a small function SelectRange() that selects specified cells in a table. When I use that, and copy the selection, the PasteAndFormat works perfectly. Then when I select the row for hiding it, I use tdleewis’s .RowSelect before setting the .Font.Hidden = True.
         I realize that tdlewis never suggested I use SelectRow on a line I want to copy. It is just that I found the command so useful I put it in other places where I wanted to select rows.
         I hope that posting this solution here will prevent others from wasting a couple of days trying to solve the problem.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
    Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
    This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now