1

I have some limited experience with basic javascript regex, which I've used to manipulate some user data entries. But now, I've been asked about manipulating MS Word 2010 documents.

Specifically, my users get a long numerical list of data as such:

1. blah blah 'car' blah blah
2. blah blah 'truck' blah blah
3. blah blah 'boat' blah blah

This is in an unformatted, plain word document. They would like a formatted header after each numeral, based on the item in single quotes, ala:

1. CAR: blah blah 'car' blah blah
2. TRUCK: blah blah 'truck' blah blah
3. BOAT: blah blah 'boat' blah blah

they currently have transcriptionists who manually make these changes (sometimes with mistakes). With javascript/jQuery, I could write some code to take this data, search for words in quotes, find the closest number before the match and insert an all CAPS version of it after the period.

But, I don't have much experience with similar functions in Word. Can this be done via a macro or some type of scripting? Is it possible to write my javascript/jQuery code and have it run in a Word instance somehow?

Thanks for the guidance!

  • how big is the document? Like one page, 100 pages? How many times a day and how many documents? Is this something they will always have to do for new documents? – madamission May 15 '15 at 16:39
  • The documents average about 50-100 pages, run once a day and are placed into a transcriptionists queue each morning. Having an automated macro would speed the process considerably. – user1837608 May 15 '15 at 21:33
3

Yes! Microsoft Office VBA supports regular expressions searching natively. You have to add "Microsoft VBScript Regular Expressions 5.5" as a Reference in the VBA Editor for each .docm, .xlsm, etc. file in which you want to put Regex code:

VBA snip

Most of the object model for this regex module can be found on MSDN here. One thing that appears to be missing is the MultiLine setting.

You use it as follows:

  • Instantiate a new regex object with Dim rx as New RegExp
  • Set the rx.Global, rx.MultiLine, and rx.IgnoreCase flags as desired
  • Define the pattern by assigning the appropriate pattern string to rx.Pattern
  • If you want to know if something matches the regex, use rx.Test(str), which returns True or False appropriately
  • Calling rx.Execute(str) returns a MatchCollection object, which behaves like a zero-based, one-dimensional array of zero or more Match objects
  • Matchobjects expose the FirstIndex, Length, Value, and SubMatches properties.
    • FirstIndex is the zero-based index of the first character of the location in str at which the match was found
    • Length is the length of the matched portion of str
    • Value is the matched string result
    • SubMatches is a zero-based array of the sub-matched groups within the overall match
      • Unfortunately, Office VBA regex doesn't support named groups like, e.g., Python, so it can take some trial-and-error to match up the group index with the piece of the pattern you're interested in

I don't believe there's a way to do a sed-style replace-in-place operation -- it's a search-only functionality. But, it's easy enough to use surrounding VBA to manipulate the results as you wish.

| improve this answer | |
  • Thanks, I think this sounds like the right answer - but it looks pretty complicated. I'm not a programmer by trade, just a hobbyist trying to help some colleagues. VBA is unfamiliar to me, but I can see by logic that all the elements for what I need are there. I'll look at the reference you linked and give it a go. – user1837608 May 15 '15 at 21:32
  • You'll need to know the basics of VBA programming in order to make it work. Google searches of the form [VBA object of interest] site:msdn.microsoft.com are your best friend, for finding out syntax and other information about the object model. Good luck! – hBy2Py May 15 '15 at 22:03
0

If it's just plaintext in the word document and it's not very big and you're familiar with jquery, you could make a simple site where they can copy/paste the word document into and you do the necessary changes and let them download the transformed word document or output the desired text. It really depends on more details of your situation to say if this is a good solution.

The macro could probably be done too, but could be difficult to deploy to users depending on how many users you had.

| improve this answer | |
  • Thank you. This is exactly the "temporary" solution I'm working on at the moment - but I was hoping there would be a more elegant, one-button push method within the Word environment instead. – user1837608 May 15 '15 at 17:08
0

In case you are not VBA or Python scripting savvy and @hBy2Py answer idea not fits you competency try workaround with GoogleSheets/Excel implementing your algoritm:

  1. upload=import unformatted, plain word=txt document into G'Sheet. Lets say your list will be in column A.
  2. in column B write nested formula for all column using REGEXEXTRACT for word in quotes and making that word uppercase =ARRAYFORMULA(UPPER(REGEXEXTRACT(A:A;"\'([a-z]+)")))
  3. ...or make separate column C make that word uppercase: =UPPER(B:B)
  4. in column C (or D) combine new list with CAPS word (there are several ways to do that, for example by spliting/concatenating or just =ARRAYFORMULA(REGEXREPLACE(A:A;"\.(\s)";". "&B:B&": "))

TLDR:

=ARRAYFORMULA(REGEXREPLACE(A:A;"\.(\s)";". "&UPPER(REGEXEXTRACT(A:A;"\'([a-z]+)"))&": "))

Other workaround could be using solution described here: Find and replace in Notepad++ using wildcards/regex

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.