Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

Hiding certain footnote texts with VBA in a Word table

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
ASKER CERTIFIED SOLUTION
Avatar of tdlewis
tdlewis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnRobinAllen

ASKER

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.
      j.r.a.
Avatar of irudyk
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
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
It's "him".... and you're welcome.
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.

j.r.a.
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
                  .RowSelect
                  .Copy    
                  .Rows.Delete  '  Cut won't delete the rows, so I use Copy & Delete
                  Tbl.Rows(EndOfData).Select
                  .InsertRowsBelow RowsToHide
                  .EscapeKey
                  .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.
     j.r.a.