我正在尝试编写一个将格式化文本的宏。
这是原始数据的样子:
This is sentence one of paragraph one. This is
sentence two of paragraph one. This is
sentence three of paragraph one. This is sentence
four of paragraph one. This is sentence five of
paragraph one.
This is sentence one of paragraph two. This is
sentence two of paragraph two. This is
sentence three of paragraph two. This is sentence
four of paragraph two. This is sentence five of
paragraph two.
这就是我希望文本的样子:
This is sentence one of paragraph one. This is sentence two of paragraph one. This is
sentence three of paragraph one. This is sentence four of paragraph one. This is
sentence five of paragraph one.
This is sentence one of paragraph two. This is sentence two of paragraph two. This is
sentence three of paragraph two. This is sentence four of paragraph two. This is
sentence five of paragraph two.
该宏将确保文本填满整个页面,并且每个单词之间只有一个空格。它需要保留段落结构。
我从 Excel 调用此宏并从 Word 运行可读性统计信息。
这是我到目前为止的代码:
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Sub Test_Button1()
Dim file As String
Dim StatText As String
Dim rs As Variant
Dim row_count As Integer
Dim header_count As Integer
row_count = 0
header_count = 0
Sheets("Sheet1").Select
Range("B5").Select
Set appWD = New Word.Application
appWD.Visible = True
Do Until IsEmpty(ActiveCell)
row_count = row_count + 1
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
ActiveCell.Copy
appWD.Documents.Add
appWD.Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
appWD.ActiveDocument.Select
With appWD.Selection.ParagraphFormat
.SpaceBefore = 0
.SpaceAfter = 0
.LineSpacingRule = wdLineSpaceSingle
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
End With
If row_count = 1 Then
ActiveCell.Offset(-1, 0).Select
For Each rs In appWD.ActiveDocument.readabilitystatistics
header_count = header_count + 1
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = rs.Name
Next rs
ActiveCell.Offset(1, -header_count).Select
End If
For Each rs In appWD.ActiveDocument.readabilitystatistics
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = rs.Value
StatText = StatText & rs.Name & " - " & rs.Value & vbCr
Next rs
appWD.ActiveDocument.Select
appWD.Selection.Delete
appWD.ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges
ActiveCell.Offset(1, -header_count).Select
Loop
appWD.Quit SaveChanges:=wdDoNotSaveChanges
Set appWD = Nothing
End Sub