【发布时间】:2014-08-13 20:09:53
【问题描述】:
我有一个当前格式的文档
标题
副标题
上半年
示例文本示例文本示例文本示例文本 样本文本样本文本样本文本样本文本 示例文本示例文本示例文本示例文本
H2
示例文本示例文本示例文本示例文本 样本文本样本文本样本文本样本文本 示例文本示例文本示例文本示例文本
H3
示例文本示例文本示例文本示例文本 样本文本样本文本样本文本样本文本 示例文本示例文本示例文本示例文本
H4
示例文本示例文本示例文本示例文本 样本文本样本文本样本文本样本文本 示例文本示例文本示例文本示例文本
这里的段落标题 H1,H2,H3,H4 是粗体 我刚刚用示例文本代替了标题下方显示的段落
我需要将该文档格式化为
标题副标题
- H1:SampleText SampleText SampleText SampleText SampleText SampleTextSampleTextSampleText SampleText SampleText SampleText SampleText(para1)
- H2:示例文本示例文本示例文本示例文本示例文本示例文本 SampleText SampleText SampleText SampleText SampleText SampleText(para2)
- H3:示例文本示例文本示例文本示例文本示例文本示例文本 SampleText SampleText SampleText SampleText SampleText SampleText(para3)
- H4:示例文本示例文本示例文本示例文本示例文本示例文本 SampleText SampleText SampleText SampleText SampleText SampleText(para4)li>
目前我在标题的开头添加一个 * 并在末尾添加一个冒号。使用它们作为参考我正在格式化段落。 这是我目前正在考虑的代码
Sub wordfor()
Dim oRng As Word.Range
Dim flag As Integer
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = ""
.Font.Bold = True
While .Execute
oRng.Text = "*" + oRng.Text
oRng.Font.Underline = True
oRng.Text = oRng.Text + ":"
oRng.Collapse wdCollapseEnd
Wend
End With
Selection.HomeKey Unit:=wdStory
Do Until Selection.Information(wdFirstCharacterLineNumber) = ThisDocument.BuiltInDocumentProperties("Number of lines").Value
'MsgBox (ThisDocument.BuiltInDocumentProperties("Number of lines").Value)
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If Selection.Text = "*" Then
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" "
Selection.Delete Unit:=wdCharacter, Count:=1
Else
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" "
Selection.Delete Unit:=wdCharacter, Count:=1
End If
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Loop
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" "
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "*"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = ChrW(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = InchesToPoints(0.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5)
.TabPosition = InchesToPoints(0.5)
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = "Symbol"
End With
.LinkedStyle = ""
End With
ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdBulletGallery).ListTemplates(1), ContinuePreviousList:=False, ApplyTo:= _
wdListApplyToWholeList, DefaultListBehavior:=wdWord10ListBehavior
End Sub
但是代码进入了无限循环。
【问题讨论】:
-
您的编程相关问题是什么?你试过什么吗?你知道 Stack Overflow 不是代码编写服务,对吧?
-
我的代码没有正确编辑。我也添加了代码。