PDA

View Full Version : Extracting numbers in square brackets



mbr2012
09-19-2012, 04:54 AM
What I want to know is how to search for numbers in square brackets in a document using VBA and use these as bookmarks to make cross-references to. I'm a newcomer, and I know it has something to do with the inStr function, but not sure how to go about implementing it.

The context is: I am editing a manuscript, and need to make cross-references in my notes to both the page number of the Word document, and the page number in the original text. For example, "p. 3 [MS, p. 7]" would refer to p. 3 in the Word document, and p. 7 on the original manuscript. I want to automate this process using bookmarks and macros. At the moment, I have a document with no bookmarks, which has references to the MS page numbers enclosed in square brackets: for example on p. 3 I have the text "As has been reported [7] her lapse has not intended.". A cross reference to this would be "p. 3 [MS, p. 7]" or, if I can get really clever "p. 3 [MS, pp. 6-7]".

What I want is to be able to search the document for numbers in square brackets, and place bookmarks around these. Then, I can use a macro to insert two cross references to these, one with the word page number and the other with the text in the bookmark, the original manuscript page no.

Cheers.

Frosty
09-20-2012, 11:50 AM
I'm not totally following your need here, but I would steer you away from the InStr function (which is a function to search within a string, but can get rather complicated and also very slow when the "String" you are searching is the entire text of a document).

You should rather explore using the native Find Object. Before jumping to a VBA solution, try using Word's Find. There are options which allow you some fuzzy searches... i.e., you can find the character "[" followed by one or more numbers, followed by the character "]"

Can you try that a bit, see how familiar you become with that (and using the browser next/previous buttons), and then report back.

Hope this points you in the right direction.

fumei
09-20-2012, 03:21 PM
There are quite a number of issues involved in what you ask. I would agree with Frosty to start with exploring Find. However, as stated in the requirements, I am pretty sure VBA will be needed.

Finding the square bracketes is fine using Find, and even replacing things. But replacing them with bookmarks? There is no such functionality within the use of Find alone.

BTW: one of the firdt things you are going to have to come up with is a logical naming convention for the bookmarks (if in fact you stick with that). Bookmarks MUST have unique names. It is not just a simple make the content of square brackets a bookmark; each one MUST be uniquely named.