Creating a cloze test using Word VBA - vba

As a beginner, I am trying to write a Word Macro that can automatically mark and convert one third of the words in an article to a cloze format. In the past, I have manually added equal signs before the desired word and then ran the following macro to complete the process. However, I am now attempting to automate this manual step by creating a new macro that will randomly add equal signs to the prefixes of the words in one-third of the selected range. After running this new macro, I plan to use the previous macro to finish the conversion to the cloze format. However, I am unsure how to set up the new macro to perform the marking and would appreciate any suggestions or guidance.
This is the conversion macro that is already done:
Sub Convert()
Application.ScreenUpdating = False
selection.HomeKey Unit:=wdStory 'init
Dim iCount, A, i As Long
Dim RPT, CHAR, WordRpt, Eventual As Integer
iCount = 0
WordRpt = 1
Eventual = 0
selection.Find.ClearFormatting 'A and I
selection.Find.Replacement.ClearFormatting
selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find 'sum A
.text = "=a "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
A = A + 1
Loop
End With
selection.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find 'sum I
.text = "=i "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
i = i + 1
Loop
End With
With selection.Find
.text = "=a "
.Replacement.text = "_ "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
selection.Find.Execute replace:=wdReplaceAll
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "=i "
.Replacement.text = "_ "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
selection.Find.Execute replace:=wdReplaceAll
With ActiveDocument.Content.Find 'sum equals
.text = "="
.Format = False
.Wrap = wdFindStop
Do While .Execute
iCount = iCount + 1
Loop
End With
While WordRpt <= iCount
WordRpt = WordRpt + 1
With selection.Find 'next equal
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="="
selection.TypeBackspace
selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
CHAR = Len(selection) - 2
selection.MoveLeft Unit:=wdCharacter, Count:=1
selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
selection.Cut
selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
selection.PasteAndFormat (wdFormatOriginalFormatting)
UdsRpt = 1 'underscore
Do While UdsRpt <= CHAR
UdsRpt = UdsRpt + 1
selection.TypeText text:="_"
Loop
selection.TypeText text:=" "
End With
Wend
'
' patch comma
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ ,"
.Replacement.text = "__,"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch period
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ ."
.Replacement.text = "__."
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch question mark
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ ?"
.Replacement.text = "__?"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch excalmation mark
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ !"
.Replacement.text = "__!"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch slash
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ /"
.Replacement.text = "__/"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch back slash
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ \"
.Replacement.text = "__\"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch colon
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ :"
.Replacement.text = "__:"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch semi colon
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ ;"
.Replacement.text = "__;"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch dash
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ –"
.Replacement.text = "__–"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch hyphen
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ -"
.Replacement.text = "__-"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
'
' patch ellipsis
'
'
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.text = "_ …"
.Replacement.text = "__…"
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
End With
selection.Find.Execute replace:=wdReplaceAll
Eventual = A + i + iCount
MsgBox "Successfully converted" & Eventual & "words.", vbOKOnly, "Task Completed"
Application.ScreenUpdating = True
End Sub

This simple macro will insert an equals sign before words in a selection with a probability of 1 in 3.
Sub InsertEqualsSigns()
Randomize
Dim targetWord As Range
For Each targetWord In Selection.Range.Words()
targetWord.Select
Selection.MoveLeft Unit:=wdWord, Count:=1
If Rnd() < 1 / 3 Then
Selection.TypeText Text:="="
End If
Next
End Sub
Note that this is currently just set to add an equals sign, so change the text to insert "=a " or "=i " as preferred as I'm not sure the exact close formatting.

Related

Giving multiple hyperlinks in MS-Word VBA based on own delimiter

I am new to VBA scripting. I am converting TeX file to Word document using GrindEq convertor. In this TeX file there have multiple \hrefcommands. Here content only coming in MS-Word file (i.e. https://doi.org/10.1016/0167-6423(87)90036-0). So far i am putting tag before and after for that content <LaTeX-href-http>https://doi.org/10.1016/0167-6423(87)90036-0</LaTeX-href-http> using python. By this content was producing with Tag in MS-Word file.
Similarly for converting to Small Caps font, converting to Monospace font, i have recorded the macro and modify as per my own needs and used.
Sub XXX_SmallCaps()
'
' XXX_SmallCaps Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.SmallCaps = True
.AllCaps = False
End With
With Selection.Find
.Text = "\<LaTeX-SmallCaps\>(*#)\</LaTeX-SmallCaps\>"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
'
' XXX_Serif Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Size = 13
Selection.Find.Replacement.Font.Name = "Calibri"
With Selection.Find
.Text = "\<LaTeX-SerifFont\>(*#)\</LaTeX-SerifFont\>"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
'
' XXX_MonoSpace
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Size = 12
Selection.Find.Replacement.Font.Name = "Courier New"
With Selection.Find
.Text = "\<LaTeX-MonospaceFont\>(*#)\</LaTeX-MonospaceFont\>"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
But how to give the Hyperlinks for the content in between the Tags <LaTeX-href-http>...</LaTeX-href-http> and <LaTeX-href-http> and </LaTeX-href-http> tags need to deleted using in VBA? Please refer the image for my DOC file.
For example (including cleaning up your other code):
Sub ReformatLaTeX()
Application.ScreenUpdating = False
Dim StrLnk As String
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Replacement.Text = "\1"
.Replacement.Font.SmallCaps = True
.Text = "\<LaTeX-SmallCaps\>(*#)\</LaTeX-SmallCaps\>"
.Execute Replace:=wdReplaceAll
.Replacement.ClearFormatting
.Replacement.Font.Size = 13
.Replacement.Font.Name = "Calibri"
.Text = "\<LaTeX-SerifFont\>(*#)\</LaTeX-SerifFont\>"
.Execute Replace:=wdReplaceAll
.Replacement.Font.Size = 12
.Replacement.Font.Name = "Courier New"
.Text = "\<LaTeX-MonospaceFont\>(*#)\</LaTeX-MonospaceFont\>"
.Execute Replace:=wdReplaceAll
.Replacement.ClearFormatting
.Text = "\<LaTeX-href-http\>*#\<\/LaTeX-href-http\>"
.Replacement.Text = ""
.Wrap = wdFindStop
End With
Do While .Find.Execute
StrLnk = Split(Split(.Text, "</LaTeX-href-http>")(0), "<LaTeX-href-http>")(1)
.Hyperlinks.Add Anchor:=.Duplicate, Address:=StrLnk, TextToDisplay:=StrLnk
.Start = .Hyperlinks(1).Range.End
Loop
End With
Application.ScreenUpdating = True
End Sub

Find/Replace VBA code is not executing properly

For some odd reason, my code does not work in all instances. It does not catch every instance where there is only 1 space after a period. I was wondering if anyone knows why this may be. I went through every line and I can't figure out why it won't work. It's pretty basic of a code.
The goal behind this code:
period with 1 space goes to period with 2 spaces
period with 2 spaces stay the same
any double spaces in the document that do not follow a period are changed to 1 space.
Mr., Mrs., Miss., Ms. only have 1 space after them.
Sub Space_corrections()
' two spaces go to one space entire doc
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' One space after periods goes to two spaces
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ". "
.Replacement.Text = ". "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Three spaces after periods goes to two spaces
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ". "
.Replacement.Text = ". "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Two spaces after Mr. goes to one space
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Mr. "
.Replacement.Text = "Mr. "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Two spaces after Mrs. goes to one space
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Mrs. "
.Replacement.Text = "Mrs. "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Two spaces after Ms. goes to one space
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Ms. "
.Replacement.Text = "Ms. "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Two spaces after Miss. goes to one space
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Miss. "
.Replacement.Text = "Miss. "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub```
UPDATE/EDIT:
I changed over to this code, but it does to change all of the instances. I am not sure what the cause is.
There is no special font on them or anything. They are sentences that are contained by a list format, but every other instance that was changed was also contained in a list format.
These are the instances that went unchanged:
**"shipped. According"
"materials. The"**
updated code:
Sub Space_corrections_123()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(.)( {1,})"
.Replacement.Text = "\1 "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
You can simplify the find and replace for the Mr/Mrs/Miss/Ms/Dr/Professor etc by using a wildcard search
.findtext="([rs])(. )"
.replacementText="\1. "
Which will find any word ending in a r or s followed by a point and two spaces and replace with the found character plus point and one space.
You can also do a wildcard search to consolidate the replacement of . by to . two spaces using
.findtext="(.)( {1,})"
.replacementText="\1 "
Try:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = "([ ^s]){2,}"
.Replacement.Text = "\1"
.Execute Replace:=wdReplaceAll
.Text = "(. )"
.Replacement.Text = "\1 "
.Execute Replace:=wdReplaceAll
.Text = "([DM][irs]{1,3}.)[ ]{2,}"
.Replacement.Text = "\1 "
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
Note the 'D' in '[DM]'. That allows you to find Dr., & Drs. as well (in case there's more than one). You can delete the 'D' if it's unnecessary.

Is there a better way to take a list of addresses on word and seperate them down to single lines

Is there a better way to take a list of addresses on word and separate them down to single line.
I have about 200 addresses to work through and they have to go from this
To this
So it can end up being copied like this into excel
The code I am currently Using is as follows but all that does is uses the replace function to change special formatting characters.
It isn't the cleanest method and if for instance some hasnt used a paragragh break but just a new line then it won't work.
Sub AddressMacro()
'
' AddressMacro Macro
'
'
ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View. _
ShowAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^i"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
End With
ActiveWindow.ActivePane.View.ShowAll = Not ActiveWindow.ActivePane.View. _
ShowAll
End Sub
How about: (replaces)
'1) Chr$(9) to Chr$(32)
'2) Chr$(10) to Chr$(13)
'3) Chr$(11) to Chr$(13)
'4) Chr$(13) & Chr$(13) to Chr$(13)
'5) repeat 4 until there are none
'6) Chr$(13) to Chr$(9)
'7) Chr$(133) to Chr$(13)
EDIT: Here's what I had in mind: (It's a lot -- there's a bottomless pit of information on Word VBA -- http://word.mvps.org/faqs/MacrosVBA/index.htm -- good luck.)
Option Explicit
Sub main()
Call doReplace(Chr$(9), Chr$(32))
Call doReplace(Chr$(10), Chr$(13))
Call doReplace(Chr$(11), Chr$(13))
Do While doReplace(Chr$(13) & Chr$(13), Chr$(13))
Loop
Call doReplace(Chr$(13), Chr$(9))
Call doReplace(Chr$(133), Chr$(13))
End Sub
Function doReplace(OldText$, NewText$) As Boolean
With ThisDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = OldText
.Replacement.Text = NewText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
doReplace = .Execute(Replace:=wdReplaceAll)
End With
End Function

How to select two words in a paragraph VBA

I have a paragraph in word containing:
"b) Vergabeverfahren Öffentliche Ausschreibung, VOB/A Vergabenummer 13A0295S. "
Is it possible to select following two words at the same time in this paragraph.
"b) Vergabeverfahren" and "Vergabenummer "
The reason is I want to change the selection color to red. I Can select one word successfully with this code. But selecting two words is a bit problematic for me.
Here is my code:
Selection.Start = singleLine.range.Start
Selection.End = singleLine.range.End
ReplaceText = StrFrmDll ' StrFrmDll contains the paragraph.
Selection.Find.ClearFormatting
With Selection.Find
.Text = ReplaceText
.Replacement.Text = ReplaceText
.Replacement.Font.Hidden = True
.ClearFormatting
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Selection.Font.Color = wdColorRed
Just do two of the Selection.Find's
Selection.Find.ClearFormatting
With Selection.Find
.Text = Vergabeverfahren
.Replacement.Font.Hidden = True
.ClearFormatting
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Selection.Font.Color = wdColorRed
Selection.Find.ClearFormatting
With Selection.Find
.Text = Vergabenummer
.Replacement.Font.Hidden = True
.ClearFormatting
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Selection.Font.Color = wdColorRed

Modify and paste clipboard text

My goal is to:
Copy text in a PDF to the clipboard
In a single move, paste the text to MS Word while
Replacing all line breaks with a space
Matching the destination's formatting
I created a macro which replaces all line breaks with spaces in a document.
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
How to apply the replacement to the clipboard and then paste this replaced snippet?
I propose to do it this way:
remember current selection point where you would paste your clipboard data
paste what you have in clipboard
set ending point of pasted area
do replacement only for just pasted range of text.
The following solution based partially on the code from the question. What was necessary (for test) was commented.
Sub replacement_for_selection()
'Selection.Find.Execute Replace:=wdReplaceAll
'Selection.WholeStory
Dim rngFrom, rngTo
rngFrom = Selection.Start
Selection.PasteAndFormat (wdFormatOriginalFormatting)
rngTo = Selection.End
ActiveDocument.Range(rngFrom, rngTo).Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = False '!!!
.Wrap = wdFindStop '!!!
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute , , , , , , , , , , wdReplaceAll
End Sub
It worked for me too, but I improved it, adding a command to eliminate double spaces:
Sub KM()
'
' KM Macro
' Paste and eliminate line breaks and double spaces.
'
'Selection.Find.Execute Replace:=wdReplaceAll
'Selection.WholeStory
Dim rngFrom, rngTo
rngFrom = Selection.Start
Selection.PasteAndFormat (wdFormatOriginalFormatting)
rngTo = Selection.End
ActiveDocument.Range(rngFrom, rngTo).Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = False '!!!
.Wrap = wdFindStop '!!!
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute , , , , , , , , , , wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = False
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute Replace:=wdReplaceAll
End Sub