Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting 'empty' ranges in cells using VBA (Word XP)

    Hi, all. This question is academic, since I developed a workaround (which is more efficient anyway, so YAY me!) But I thought something might be learned anyway from knowing the answer:

    I have a HUGE piece of code which, among other things, renumbers items in a table by emptying out any numbers currently in column 1 and replacing them with new numbers. Sometimes, though, there is no number in the first cell. When I wrote the code on my machine, I originally had the following two lines.

    Selection.Tables(1).Cell(rRow + 1, 1).Range.Delete
    Selection.Tables(1).Cell(rRow + 1, 1).Range.InsertAfter RefIDs(rRow, 1) 'Insert the value from column two of the array called RefIDs

    It has run fine for years in our department, but I found that a particular user sometimes gets an error on the first line of code shown above. I assume it's because the cell is empty on those occasions, as it was today. (I replaced both lines with the single line "Selection.Tables(1).Cell(rRow + 1, 1).Range.Text = RefIDs(rRow, 1)" which runs fine and is arguably more efficient, anyway.)

    My question is: Why would that line cause a problem on her machine and not others? In light of that problem, I wonder what other inconsistencies lie waiting for me!
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Deleting 'empty' ranges in cells using VBA (Word XP)

    Even if a cell is empty, deleting its range shouldn't cause an error (a cell is never really empty, by the way). What was the error that occurred?

  3. #3
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting 'empty' ranges in cells using VBA (Word XP)

    Interestingly, it was an "object not found" error.

    To figure out what the problem was, I debugged it on her machine while simultaneously working on mine. We used the exact same file and copies of the exact same template. The table in question looked exactly the same on both machines, i.e. the cell was empty. The only difference between our two machines is that she has the "Windows in Taskbar" option turned on. At first, I thought that option might "confuse" word when using the selection object, but when I changed the command to the single line that sets the text, I figured that Word indeed knew what object "Selection.Tables(1).Cell(rRow + 1, 1).Range" was, since it was able to work with it.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •