+ Reply to Thread
Results 1 to 5 of 5

Thread: looping backwards to delete in word

  1. #1
    Valued Forum Contributor
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Excel 2010 / Excel 2000 & 2003 for some at work
    Posts
    213

    looping backwards to delete in word

    Having never had a go at Word VBA I have have beem scouring the web for what I would have thought would have been a simple task (it is in excel).

    What I have been looking for is an example of the syntax for first of all finding and selecting a word and then looping (unless there is a better method) backwards to delete all the previous text to the start of the document.

    I can record the find easy enough and can get it to delete 1 line back (I assume at the moment that it is deleting a whole line as I currently have only put in a single character on each line above) but I haven't seen anything I have been able to understand enough to adapt for the loop.

    I have seen on other forums people asking virtually the same question but as yet have not seen a helpful enough reply to provide me with an answer.

    Any guidence would be appreciated (as would any reading resources you can recommend on basic programming in Word VBA)
    Last edited by WasWodge; 07-12-2011 at 12:50 PM.
    Using Excel 2010
    Normally need to save as Excel 97-2003 for other colleagues

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  2. #2
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: looping backwards to delete in word

    Guy hart-Davies Word VBA programming.
    You have to turn something around in your head:

    sub snb()
      activedocument.content=mid(activedocument.content,instr(activedocument.content,"@@@"))
    End sub
    rephrased into:
    sub snb()
      with activedocument
       .content=mid(.content,instr(.content,"@@@"))
      end with
    End Sub
    or
    Sub snb()
      activedocument.content=split(activedocument.content,"@@@")(1)
    End Sub



  3. #3
    Valued Forum Contributor
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Excel 2010 / Excel 2000 & 2003 for some at work
    Posts
    213

    Re: looping backwards to delete in word

    Cheers snb for the code (I used the mid version). I t works perfectly. The only thing is, it isn't how I would have expected it to look. When you get a quiet minute one day can you provide me with an explanation of how it works

    Thanks as well for the book suggestion I am hoping you mean "Word 2007 Macros & VBA Made Easy by Guy hart-Davies" as that seems like the nearest match I can see at the moment .

    (trying to turn my head around at the moment but it is making it a bit difficult seeing the screen)

    Again thanks for all the help
    Last edited by WasWodge; 07-11-2011 at 05:00 PM. Reason: Spelling skills
    Using Excel 2010
    Normally need to save as Excel 97-2003 for other colleagues

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  4. #4
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: looping backwards to delete in word

    I's better turning around inside your head than turning it itself.

    In Excel you have to think in arrays (every worksheet is an 256*65536 array).
    In Word you have to think in textstrings: a worddocument is 1 textstring.
    the whole content of a worddocument is a textstring.
    In you want to delete a certain part (which is equivalent to retaining hte opposite part) you can use all kinds of teststringmethods to achieve your goal.
    In VBA you can determine the position of a character or group of characters using Instr. Once you found that you can either retain the string from the beginning to that point using Left:

    c01=left(activedocument.content,instr(activedocument.content,"@@@"))
    or retain the rest of the document using Mid
    c01=mid(activedocument.content,instr(activedocument.content,"@@@"))
    In VBA the second argument in Mid is optional: if you don't specify the length, the selection will be extended to the last character of the string.

    Hope this helps.
    Last edited by snb; 07-12-2011 at 09:05 AM.



  5. #5
    Valued Forum Contributor
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Excel 2010 / Excel 2000 & 2003 for some at work
    Posts
    213

    Re: looping backwards to delete in word

    Thanks, that makes perfect sense. I much appreciate the code and explanation
    Using Excel 2010
    Normally need to save as Excel 97-2003 for other colleagues

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0