Microsoft Word: vba

This is predictably the same principle as Excel and other Office components.

So again I have this file…

 

 

… that I want to reduce to this file…

 

 

Few things you have to do first:

  • Enable the developer tab (see below)
  • When you write your vba, you have to save the host doc as a docm, not  a docx

Macro that achieves the functional stuff (not the config, to be clear): (gist)

This lacks any parameterisation – can you even do that? Dunno. Another day.

 

I note BTW that this is case-insensitive – check “Uniqueid”.

Credits:

Might also be useful: https://msdn.microsoft.com/en-us/vba/vba-word

 

 

Advertisements

Microsoft Word: regex… sort of

So why don’t I just do my text manipulation in e.g. NotePad++, and then paste that into Word? Because the document I’m working on already has colour and formatting that I don’t want to lose.

What I’ve found (I might be a bit off, but I’ve proven enough to understand it for my needs):

Test 1 – note the different cases for [simpler]:

Simpler test by far
Next line of the simpler test

Search string [simple]

  • Non-wild card test finds both entries (meaning it is case in-sensitive)
  • Wild card (see below for the settings used) test finds [simpler] only, meaning a wild card search is case sensitive

Search string [simple*]

  • Non-wild card test finds neither entry (i.e. it treats the [*] in [simpler*] as a literal
  • Wild card test finds [simpler] only, meaning a wild card search is case sensitive

Test 2 – wild card search including paragraph marker

Control Line 1
Simpler test by far
Control Line 2
Next line of the Simpler test

Search string [simple*^13]

In Word regular expressions, this means “find the start of ‘simple’ anywhere on a line of text, then select that up to and including the end of the paragraph (^13 is the paragraph marker). If the search and found text differ at all in case, ignore.

Stating the obvious, a non-wild card search finds neither entry (i.e. it treats the [*^13] in [simpler*^13] as a literal… which it will never find)

A wild card search finds all entries up to and including the paragraph marker.


Test 3 – wild card replace (sure it’s the same text basically, but this now just makes it closer to the use-case I have)

Control Line 1
UniqueId test by far
Control Line 2
Next line of the UniqueId test

I want to delete the lines that contain [UniqueId], anywhere on the line, to give:

Control Line 1
Control Line 2

 

These options achieve that. I was a bit puzzled that this is enough to remove the whole of line 4, when I might have expected it to keep [Next line of the]. But good enough for my needs.