Spaces missing in text strings imported from a Word doc

Joined
Nov 23, 2023
Messages
13
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have put together a working script that opens a selected word document, searches through it for key words, and pastes results into the spreadsheet. See below my import of a song lyrics. Apologies for poor quality photo.

The problem is there are spaces missing where sentences end so that some words are merged as one in some places.

I have tried using Replace of vbLf and vbCr but I am having problems understanding the range requirement for the text Range function
( my Dim statement for the text is :

Dim docText As String
docText = ActiveDocument.Content.Text

whereas to Insert a Replace command, I need a Range for the

Range.Replace vbCR, " " line

and I am unsure how to link them up

Does anyone have any idea how to merge the Replace command into the otherwise functioning script ?

Any advice greatly appreciated,

Picture to follow

Bob :)
 
Screenshot 2025-03-09 213128.png


attached is the text result with some words joined where I would like a space to be
 
Upvote 0
You need to replace the line/paragraph breaks in Word with a placeholder before pasting into Excel, then replace the placeholder in Excel with the Chr(10) line breaks.

For example:
Excel Formula:
Sub Demo()
' Note: A reference to the Microsoft Excel # Object Library is required, set via Tools|References in the Word VBE.
Application.ScreenUpdating = False
Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Forward = True
    .Format = False
    .Text = "[^13^l]"
    .Replacement.Text = Chr(182)
    .Execute Replace:=wdReplaceAll
  End With
  .Copy
End With
ActiveDocument.Undo
'Start Excel
With xlApp
  'Add a workbook
  Set xlWkBk = .Workbooks.Add
  With xlWkBk.Sheets(1)
  'Paste the data
    .Paste 'Destination:=Range("A1")
    'Post-format the data
    On Error Resume Next
    .UsedRange.Replace What:=Chr(182), Replacement:=Chr(10)
    On Error GoTo 0
    .Rows.AutoFit
  End With
  .CutCopyMode = False
  .Visible = True
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top