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
Guy hart-Davies Word VBA programming.
You have to turn something around in your head:
rephrased into:sub snb() activedocument.content=mid(activedocument.content,instr(activedocument.content,"@@@")) End sub
orsub snb() with activedocument .content=mid(.content,instr(.content,"@@@")) end with End Sub
Sub snb() activedocument.content=split(activedocument.content,"@@@")(1) End Sub
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
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:
or retain the rest of the document using Midc01=left(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.c01=mid(activedocument.content,instr(activedocument.content,"@@@"))
Hope this helps.
Last edited by snb; 07-12-2011 at 09:05 AM.
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks