Add Hard Return around Paragraph spacing Before/After in Microsoft Word

Hello,

I am "hunting" for AppleScript that allow me to add Hard Return to text where Paragraph Spacing Before/After is used.

For instance I am receiving a Word Document with Space Before:

After AppleScript hard returns will be added:

Sometimes Word Document have mixture of Spacing Before and After in the document so would like to include both conditions. Now, since I am receiving Word Documents with different values for Space Before/After - I am unable to give a fixed value.

Is this possible?

This is one way you might do it. I haven't tested this thoroughly, you may need to adjust it further. Highlight the text in question and trigger this macro.


Add Hard Return To Paragraphs.kmmacros (3.8 KB)


@cfriend Thank you for your solution. I am afraid that this may not work correctly on longer documents with tables and images. I would like to avoid any copying and pasting

I am more concerned about these attributes (note that they are different from doc to doc):

Hence the question about AppleScript solutions.

1 Like

Yes, Word’s AppleScript dictionary definitely knows the properties space before and space after. So, it should be doable with AppleScript. Unfortunately ATM I don’t the time to write a script.

1 Like

OK, found the time :grinning:

Try this:

‌

— [deleted non-working script; new script see below] —

‌

Every paragraph that had a space before gets a Return at the beginning, and every paragraph that had a space after gets a Return at the end. The spaces before/after are set to 0.

@Tom this almost works! Here's what I am getting:

Can you post the text sample?

Here’s weTransfer link with the document: https://we.tl/KZjuDac1n3

Thanks. FYI: you can also zip a file and upload it here.

OK, this should work now:

tell application "Microsoft Word"
  tell active document
    set spacedBeforePars to paragraph_id of every paragraph where space before is greater than 0
    set spacedAfterPars to paragraph_id of every paragraph where space after is greater than 0
    repeat with theID in spacedBeforePars
      set thePar to (every paragraph whose paragraph_id is theID)
      set thePar to item 1 of the thePar
      insert paragraph at before text object of thePar
    end repeat
    repeat with theID in spacedAfterPars
      set thePar to (every paragraph whose paragraph_id is theID)
      set thePar to item 1 of the thePar
      insert paragraph at after text object of thePar
    end repeat
    set space before of every paragraph to 0
    set space after of every paragraph to 0
    execute find find object of text object find text "^13{3,}" replace with "^p^p" replace replace all match wildcards yes
  end tell
end tell

Sorry for the crap script in my first answer :wink:

2 Likes

@Tom I can confirm, this works! However for more complex leaflets it fails :confused: and it's very slow:

space_after_space_before_word.zip (128.7 KB)

I think I would have to tinker in JavaScript (InDesign) instead...

Write a Word macro (VBA). This will – probably – be faster.

OK, try this:

tell application "Microsoft Word"
  tell active document
    set spacedPars to every paragraph whose (space before is greater than 0 or space after is greater than 0)
    repeat with thePar in spacedPars
      if space before of thePar is greater than 0 then
        set space before of thePar to 0
        insert text "†" at before text object of thePar
      end if
      if space after of thePar is greater than 0 then
        set space after of thePar to 0
        insert text "†" at after text object of thePar
      end if
    end repeat
    execute find find object of text object find text "†{1,}" replace with "^p" replace replace all match wildcards yes
  end tell
end tell

It is considerably faster. (20s instead of >2min.)

Replace the temporary placeholder character (†) with a unique character that is not present in your text. You can also use a string of more than one characters.
‌
However, your new sample text poses a new challenge:

Since the additionally inserted empty paragraphs take the style of the “parent” paragraph , we end up with empty paragraphs that are styled as list, that is, an empty line with a leading bullet.

If this is a problem (and I fear it is) I have to come up with a more sophisticated replacement that removes the style…

@Tom thank you for your support here, the script works now! I am placing the Word Document into InDesign and strip out any Word Document Styles :slight_smile:

So, do you want to remove all styles? I was assuming you wanted to keep the styles.

Removing all styles at the end of the script will should be fairly easy…

@Tom my workflow in InDesign involves removing all Paragraph and Character styles from Word Doc (but preserving the local overrides), I then run script to look for Character attributes (such as Bold, Italic etc.) - that script create Character Styles based on the mentioned attributes. Paragraph Style: I use one Body Text.

So at the end I don’t care about WordDocument Paragraph/Character styles as long as the Bold, Italics etc. are preserved (if that makes sense)

Got it.

For the sake of delivering clean work, here is a variant that sets the style of any newly inserted empty paragraph to the base style “plain text”.

That is, no lonely list bullets in empty paragraphs. Styles of the existing paragraphs are left untouched.

tell application "Microsoft Word"
  tell active document
    set spacedPars to every paragraph whose (space before is greater than 0 or space after is greater than 0)
    repeat with thePar in spacedPars
      if space before of thePar is greater than 0 then
        set space before of thePar to 0
        insert text "†" at before text object of thePar
      end if
      if space after of thePar is greater than 0 then
        set space after of thePar to 0
        insert text "†" at after text object of thePar
      end if
    end repeat
    set makePars to find object of text object
    set content of makePars to "†{1,}"
    set content of replacement of makePars to "^p"
    # Optional: style of the inserted empty paragraphs
    set style of replacement of makePars to style plain text
    execute find makePars match wildcards yes replace replace all
  end tell
end tell

Please give it test run. If it’s fine I will also update my answer to your post on StackOverflow.

Another update :wink:

I think this is what you really need:

It sets every style to the style “plain text”, while preserving local overrides (it seems). Since it doesn’t set the spacing of individual paragraphs to zero, it will be faster than the other variants!

tell application "Microsoft Word"
  tell active document
    set spacedPars to every paragraph whose (space before is greater than 0 or space after is greater than 0)
    repeat with thePar in spacedPars
      if space before of thePar is greater than 0 then
        insert text "†" at before text object of thePar
      end if
      if space after of thePar is greater than 0 then
        insert text "†" at after text object of thePar
      end if
    end repeat
    set style of text object to style plain text
    set makePars to find object of text object
    set content of makePars to "†{1,}"
    set content of replacement of makePars to "^p"
    execute find makePars match wildcards yes replace replace all
  end tell
end tell

PS: it takes only 12s for your large text sample!

@Tom thank you! It works! The last script loses some of the formatting, second last seem to handle the text better :slight_smile:

Great!

If you don’t care about the space before/after of the existing paragraphs (since you are removing the Word styles in InDesign) then you can remove these lines from the second last variant:

set space before of thePar to 0
set space after of thePar to 0

This will make it way faster. (~ As fast the last variant).

1 Like