+ Reply to Thread
Results 1 to 8 of 8

object required error trying to iterate through words collection

  1. #1
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    object required error trying to iterate through words collection

    Hey all, I'm a newbie at Word VBA so I appreciate any insights you can provide.

    My desire:

    Augment each number in the string by 1 (or user choice)
    Lorem ipsum dolor 50 sit amet, consectetur 1982 adipiscing elit, sed do 23 eiusmod tempor incididunt ut labore et 2018 dolore magna aliqua.


    My code:
    Please Login or Register  to view this content.
    In problem line, it says object required.

    The code works if I strip away the RTrim, CDbl and Cstr functions, but then it removes the trailing space and squishes the words together. If I manually add a space such as item.text + inc & " " then it doesn't move to next word. Word crashes unless I'm stepping line by line, in which case same word keeps incrementing, does not move forward. Not sure why. In the watch window, the Words collection does not increase in size.

    So, looking up the documentation, we're suppose to remove trailing spaces with RTrim and that works. When I do that, it says object required. So I thought, aha, type mismatch. Convert text to double (even though it worked before) and then back to string but no matter what combination I use, I keep getting "object required". The "item" object is still a range, that doesn't change I believe. I have plugged everything into the watch window and it still looks valid.

    Help?

  2. #2
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: object required error trying to iterate through words collection

    It's the second instance of RTrim. It makes sense why, because you are writing to this, there is no object RTrim(items.text), but if you're just reading it, you can wrap it in a function. I think that's what's going on, and the type conversions are not necessary.

    But I am still faced with the original problem. I don't know hwy it doesn't iterate to next word in collection when I insert a space break. Maybe I can just factor the code to do that in multiple steps somehow, or use variables as holders. It gets stuck in an infinite loop, whereby it makes sense why the program froze when I didn't have it on break mode.

    My thought was to step backward, but I'm not sure you can do that in a collection. Anybody?

  3. #3
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: object required error trying to iterate through words collection

    I got it working. After multiple trials and tests, this is the only way I found to make it work, but it does indeed work. At least in paragraphs. Not in Tables. The error is that the requested member of the collection does not exist. Kind of makes sense as an error. As an alternative, I could do a Regex approach. Might be more universal.

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (22 H2) 64 Bit
    Posts
    1,150

    Re: object required error trying to iterate through words collection

    You can try this; which works in tables and document body ...

    Just select the text and run the macro....

    Please Login or Register  to view this content.
    Last edited by Haluk; 01-15-2020 at 03:19 PM.

  5. #5
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: object required error trying to iterate through words collection

    haha I'll try the regex later! I'm already killing too much time on this optional project but thank you!

    BTW I found out

    A) if I add this
    Please Login or Register  to view this content.
    Then I can run the code clean without error messages.
    however, I also found out
    B) it's not a table that causes the problem. Technically I've been running it in a table, just a single large cell table, without realizing and it ran fine. Just when my selection spans multiple cells is there a problem.

    Thanks for your help. I've used regex before but I'll look into that later.

  6. #6
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: object required error trying to iterate through words collection

    When I select four cells in a table, and watch that selection, it counts 9 elements. When I debug and print them, it gives me this output below unedited. That's interesting. However I still don't understand why it's a problem, since I have an 'if' loop. If it's not numeric, then it should just ignore it. When I do
    Please Login or Register  to view this content.
    , then it works for the very last element, updating the number, but that's the only one. It makes sense the last since we're stepping backwards, but it doesn't make sense why it doesn't keep going.

    
    19

    
    2

    
    10

    
    +
    20

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: object required error trying to iterate through words collection

    Try:
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: object required error trying to iterate through words collection

    Thanks, I'll have to parse this later and try it out! Off the top of my head, I can kind of see what it's doing. I don't know why the RTrim method didn't work though or why immediate or watch window doesn't show any funky invisible characters or whatever that may be causing exceptions, was going on inside the range. The collection didn't increase in size, so maybe it just lost it's reference to the starting point of the next word or something. Anyway I'm just thinking out loud.

    Code above takes a lot of insight!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Compile error when after pass a collection object to class constructor & setter getter.
    By excelicus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-03-2018, 10:47 AM
  2. [SOLVED] Performance of iteration through Words collection degrades as collection gets bigger
    By 6StringJazzer in forum Word Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 09:44 AM
  3. [SOLVED] Simple Calendar pop up macro --> error Run-time error '424': Object required
    By am_hawk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2013, 10:38 AM
  4. [SOLVED] Adding collections to collection Object error
    By stuart010 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-28-2012, 05:34 AM
  5. Name.Object (Error Object Required)
    By dssrun209 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2010, 01:38 PM
  6. Error 424 Object Required Error when Using AddItem to ListBox Generated in Code
    By jclark419 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2010, 12:47 PM
  7. Collection - object required message
    By Alex St-Pierre in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-04-2006, 04:45 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1