Want Wordbasic to delete text between two markers - how is it done?

HI. I'm a 'newbie' and I'm trying to learn how to delete unwanted text in Word. I'm managing to move the cursor around a bit with Wordbasic but that's about all. . Example text below, I want to delete everything between JFG and Part, and then repeat this operation for hundreds of occurrences in the document. The fields on the left hand side (i.e 'Entry', 'By' etc.) are invariant, as are the spaces between the fields and the hyphen; only the text after the hyphen ever changes. Many thanks!

Entry - 12 June 2012

By  - JFG

Dept - 03

VA code  - 003

Part - 03405075

Despatched - 17 JUNE 2012


Needs to look like:

Entry - 12 June 2012

By  - JFG

Part - 03405075

Despatched - 17 JUNE 2012

Assuming you are using Office 2013, you probably won't be using Wordbasic to do this - That was superceded in Office 1997 when vba replaced wordbasic. However, you can do it with a VBA macro but it is best done as a search and replace so I will explain how. If you want to record this as a macro then you will have the code you require.

Open the Find and Replace dialog by pressing Ctrl-H

In the Find what box type the following

(JFG^13)*(Part )

In the Replace with box type the following

\1\2

Turn on Use Wildcards

Click the Replace All button

Andrew Lockton
Melbourne Australia

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Andrew. Thanks, but I couldn't get this to work. Maybe there is a typo or something? In any event, I think this is not a total solution since the text after the hyphen varies. So your suggestion could take out the first segment, but 'JFG' will not be present in the other records. This is why I was trying to locate the invariant field 'By' and then track forward (perhaps to end of paragraph?) before deleting everything after JFG and before 'Part'. Finally, you are correct on the wordbasic/VBA. I was playing with some old code that mentioned 'wordbasic'. It still seemed to work to some extent but I could not find a way of identifying the correct segment for automated deletion from multiple records.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I missed the fact that JFG was variable. In that case you could change the find string so you are now searching for the following with wildcards turned on

(By*^13)*(Part )

If that still doesn't work for you then you may need to post a larger sample of your content so we can see what the variants actually are.

Andrew Lockton
Melbourne Australia

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I'd be inclined to use a wildcard Find/Replace with:

Find = (By [!^13^l]@[^13^l])*[^13^l](Part)

Replace = \1\2

Note that you don't need a macro for this, though having one might be useful if you need to do it often.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I'd be inclined to use a wildcard Find/Replace with:

Find = (By [!^13^l]@[^13^l])*[^13^l](Part)

Replace = \1\2

Note that you don't need a macro for this, though having one might be useful if you need to do it often.

Thanks Paul for the suggestion. When I paste the text directly into the search box (CTRL + H) I just get the message cannot find what you were looking for. I've tried pasted everything after the equals sign, which seems to be the logical thing to do, but I also tried with the equals sign too in desperation. Still no joy. Unless your suggestion is not being displayed properly at my end I'm not sure what is wrong. (I'm seeing exclamation marks, caret, at signs and asterisks). Quite why this formula should work and the significance of all the 13's is a mystery to me, so my fiddling around with the formula isn't going to help. If you paste the data that I originally posted into Word does this formula work for you?  Thanks.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Pauls suggestion is not working because it is looking for a regular space after the 'By'. In your sample content this is actually a non-breaking space and therefore you need to change that space in the Find what to a non-breaking space by pressing Ctrl-Shift-Space

Note that Paul's suggested wildcard string is 'greedy' - it has a tendency to select multiple separate instances with the found content extending from the beginning of the first one to the end of the second one.

The ^13 is a paragraph marker (aka a hard return).

The ^l is an end of line marker (aka a soft return).


Are you sure the search string code I posted doesn't work for you?

Andrew Lockton
Melbourne Australia

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Andrew's reference to a non-breaking space, can be accommodated by changing the Find/Replace to:

Find = (By[ ^0160][!^13^l]@[^13^l])*[^13^l](Part)

Replace = \1\2

(and, no, you don't insert the '=' signs).

I'm not sure what his reference to the wildcard string being 'greedy', though, as I haven't been able to get it to span more than one complete expression of the kind you posted. The only way I can get it to span more than one range is with something like:

Entry - 12 June 2012
By  - JFG
Dept - 03
VA code  - 003
Despatched - 17 JUNE 2012
Entry - 12 June 2012
By  - JFG
Dept - 03
VA code  - 003
Part - 03405075
Despatched - 17 JUNE 2012

Note the two paragraphs beginning with 'By', without an intervening paragraph beginning with 'Part'. In this case, all of the bolded content would be counted as a single range. But, then, Andrew's own version is no different.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Paul

Your search string gets greedy if there is any records where the intervening paragraphs have already been removed. If the 'By' paragraph is immediately followed by a 'Part' paragraph then the selected text stretches down to the next 'Part' paragraph.

If you copy ALL the content in the first post and paste it multiple times into a document you will see the difference between my search string and yours. Alternatively, run the replace a second time to see the difference.

As you say, either search string will fail if the trigger paragraphs are not paired up - however there is a difference if any cleanups have already been done.

Andrew Lockton
Melbourne Australia

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 642 Applies to: