Word VBA: for speed, avoid Document.Paragraphs(idx)

In Word VBA, calling doc.Paragraphs(idx) to get a paragraph by its index is very slow.  Instead, once you have a Paragraph object, use para.Next and para.Previous to navigate, and your code will be much faster!  I just refactored an old routine this way and received a dramatic speedup.

I think doc.Paragraphs() must be iterating through the document starting from the beginning, but I don’t know that for sure.  I do know that adding paragraphs before the region my code was working with made the code run perceptibly slower, even though the code was never touching the added paragraphs.

Here are some simple helpers, in case you want to manually check for errors rather than throwing when you run off the end of the document:

Private Function NextParaOrNothing_(p As Paragraph) As Paragraph
    Set NextParaOrNothing_ = Nothing
    On Error Resume Next
    Set NextParaOrNothing_ = p.Next
End Function 'NextParaOrNothing_

Private Function PrevParaOrNothing_(p As Paragraph) As Paragraph
    Set PrevParaOrNothing_ = Nothing
    On Error Resume Next
    Set PrevParaOrNothing_ = p.Previous
End Function 'PrevParaOrNothing_

 

NBSP in Word, part 2

This is a follow-on to my previous post on the subject.  These are my notes as I’m working, so they don’t read very well as text 🙂 .

Another test, on a different file exhibiting the same problem:

In the UI:

  • Removed all the new-style equations (OMath objects)
  • Changed all OpenType fancy features to defaults (in the Character Properties)
  • Changed the proofing language for all text to English

In word/settings.xml:

  • Removed FELayout
  • Removed all w:compat/w:compatSetting except for [@w:name=”compatibilityMode”]
  • Left m:mathPr for now, but I may remove that another time. Edit: I removed that below.

Did `find . -name \*xml | xargs grep -il asia`.  That gave me word/{document,numbering,settings,styles}.xml.

Used XML Notepad 2007 to move tags onto their own lines — just open the file, then save it.

settings.xml: it was w:themeFontLang.  Leaving that in for now.

styles.xml: It’s a bunch of <w:lang/> and <w:rFonts/> (`grep -i asia word/styles.xml |sort|uniq|less` is my friend!).  Leaving them in for now.

numbering.xml: Again, just <w:rFonts/>.  Leaving them for now.

document.xml: There were two lonely <w:rFonts/> tags!  Those I left in at first.

Zipping it

I tried using 7z, both from the context menu and with `7z a -tzip -mm=Deflate -mx=0 ../draft8e.docx *` (7z command-line options reference).  However, Word 2013 didn’t like either.  I used Send To | Compressed Folder, and that was OK.  A problem for another day.

More changes

Result?  Still no luck.  Time to strip the rFonts!

  1. Remove the rFonts referencing asia (/asia/g, in fact) from document.xml.  Result? No luck.
  2.  Remove that pesky m:mathPr from settings.xml.  You know, I wonder…  Result?  No luck.
  3. Remove w:themeFontLang from settings.  In styles, leave w:val, but remove w:eastAsia and w:bidi.  Result: success!!!!

So if you do all of the above, you’ll probably be OK 🙂 .

Edit 2017/11/02

More discussion of this issue, in a LibreOffice context.  Apparently Word 2016 has gone back to fixed-width NBSPs.  I hope it is still fixable!  I will probably have 2016 by the end of the year and will post updates then.

 

 

Word 2013 non-breaking space width: an end to the insanity!

Pre-Word 2013, a persistent annoyance was that non-breaking spaces (Chr(160)) were fixed width, even in justified text.  Word 2013 changed that.  Now, in justified text, regular spacing and non-breaking spaces are the same width.

Sometimes.

If you’re lucky.

User slasza on Microsoft Answers posted this test case from Word 2013, in which everything should have been right, but the nonbreaking spaces still didn’t show up:

NBSP failure – image by slasza

I ran some test files and took a look at the Word XML, and found the culprit at last!  It is the <w:useFELayout/> tag in the <w:compat> group in word/settings.xml.  If that tag is present in a Word 2013 document, nonbreaking spaces are fixed-width.  If it is absent, nonbreaking spaces are variable-width.

More detailed instructions on how to fix this are coming soon.  But rejoice that at least now you know what the problem is!

Edit 1

I just fixed one legacy file by going on an odyssey through the UI and the XML.  I:

  1. In the UI, turned off all the fancy OpenType features
  2. Set the proofing language to US-English for all text
  3. In word/settings.xml, removed FELayout, everything under w:compat except for w:compatSetting, and I think a few other things under /w:settings.
  4. In word/*.xml, removed just about every mention of the word “asia” (case-insensitive)

And my NBSPs are finally variable-width!

Customising comment boxes in Word

Word comments are as frustrating as they are useful. I’ve used this post several times to wrestle them to the ground!

LibroEditing proofreading, editing, transcription, localisation

A lot of people find this blog when they’re trying to sort out specific problems with their comment boxes (comment boxes suddenly going tiny, or comment box text running in the wrong direction, changing the language in your comment balloons). Here are general instructions on customising your comment boxes (or balloons, as they are officially called) in Word.

Why would I want to customise my comment balloons?

To be honest, the main reason for doing this is if something goes wrong. But the standard, default text size and layout may not be suitable for your purposes, and you might want to change it to make it more readable for someone with limited vision, etc.

You might also have preferences about which margin your comment balloons appear in, and how big they are.

The principles we are going to learn about here also apply when you want…

View original post 996 more words