我在下面编写了VBA宏,目的是强化一些在我的报表创作过程中易于解开的Word 2010样式规范。宏编译时没有错误,但是当我在测试报告上运行它时,除了样式名称赋值之外的所有格式都被删除了,根本没有任何事情发生。
Sub ntsReportFormatting()
Dim ntsReportDoc As Word.Document
Dim ntsNormal As Style
Dim ntsTOC1 As Style
Dim ntsTOC2 As Style
Dim ntsTOC3 As Style
Set ntsReportDoc = ActiveDocument
Set ntsNormal = ntsReportDoc.Styles("Normal")
With ntsNormal
.Font.Name = "Arial"
.Font.Size = 12
.ParagraphFormat.LeftIndent = 0.5
.ParagraphFormat.SpaceAfter = 0.6
End With
Set ntsTOC1 = ntsReportDoc.Styles("TOC 1")
With ntsTOC1
.Font.Name = "Arial"
.Font.Size = 12
.Font.Bold = True
.ParagraphFormat.LeftIndent = 0
.ParagraphFormat.SpaceBefore = 0.6
.ParagraphFormat.SpaceAfter = 0.6
End With
Set ntsTOC2 = ntsReportDoc.Styles("TOC 2")
With ntsTOC2
.Font.Name = "Arial"
.Font.Size = 12
.ParagraphFormat.LeftIndent = 0.17
.ParagraphFormat.SpaceBefore = 0.6
.ParagraphFormat.SpaceAfter = 0.6
.NoSpaceBetweenParagraphsOfSameStyle = True
End With
Set ntsTOC3 = ntsReportDoc.Styles("TOC 3")
With ntsTOC3
.Font.Name = "Arial"
.Font.Size = 12
.ParagraphFormat.LeftIndent = 0.33
.ParagraphFormat.SpaceBefore = 0.6
.ParagraphFormat.SpaceAfter = 0.6
.NoSpaceBetweenParagraphsOfSameStyle = True
End With
End Sub
我已经咨询了Lynda.com,MSDN文档,Youtube和Google,但我找不到明确的解决方案。
答案 0 :(得分:0)
我无法确认问题,宏工作正常,根据定义的参数更改样式
答案 1 :(得分:0)
我遇到的问题是它更新了样式,但没有更新文档文本。
我在重置Loop
后运行Styles
。
在上一个样式编辑声明之后添加此内容。
For p = 1 To ActiveDocument.Paragraphs.Count
ActiveDocument.Paragraphs(p).Range.Select
ParaStyle = ActiveDocument.Paragraphs(p).Range.style
Selection.Range.style = ParaStyle
Next
我不确定是否还有其他办法。
答案 2 :(得分:0)
它对我来说也很好,但根据Jean-Pierre的回答,我不得不怀疑是否已对文本应用了直接格式化。这将覆盖样式格式,因此样式的更改将不会显示。
有一组命令可以从当前选择中删除各种类型的格式。它们都以“清除”一词开头。
例如,我选择了一个段落并对其应用了不同的字体。直到我第一次这样做才改变风格:
ntsReportDoc.Select
Selection.ClearCharacterDirectFormatting
但是你必须要知道,如果没有使用样式应用这种东西也可以删除斜体等。