vc输出word,标题、大纲目录如何控制?
为了在“文档结构图”中看到整个word文档的逻辑结构图,所以想在VC输出word的时候,设置该word文档的标题和一级目录,但是却遇到麻烦,
我在office2007中录制了宏
' 宏2 宏
'
'
Selection.Style = ActiveDocument.Styles( "标题 1 ")
Selection.MoveDown Unit:=wdLine, Count:=5
Selection.Style = ActiveDocument.Styles( "标题 2 ")
Selection.MoveDown Unit:=wdLine, Count:=5
Selection.Style = ActiveDocument.Styles( "标题 2 ")
End Sub
但是在转换成VC代码的时候发现Selection对象和Document对象都没有style方法。
同学用office2003帮我录制了以下的宏:
ActiveDocument.Styles.Add Name:= "word1 ", Type:=wdStyleTypeParagraph
ActiveDocument.Styles( "word1 ").AutomaticallyUpdate = False
With ActiveDocument.Styles( "word1 ").Font
.NameFarEast = "黑体 "
.NameAscii = "Times New Roman "
.NameOther = "Times New Roman "
.Name = "黑体 "
.Size = 16
.Bold = True
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Scaling = 100
.Kerning = 1
.Animation = wdAnimationNone
.DisableCharacterSpaceGrid = False
.EmphasisMark = wdEmphasisMarkNone
End With
With ActiveDocument.Styles( "word1 ").ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = CentimetersToPoints(1.11)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 2
.LineUnitBefore = 0
.LineUnitAfter = 0
.AutoAdjustRightIndent = True
.DisableLineHeightGrid = False
.FarEastLineBreakControl = True
.WordWrap = True
.HangingPunctuation = True
.HalfWidthPunctuationOnTopOfLine = False
.AddSpaceBetweenFarEastAndAlpha = True
.AddSpaceBetweenFarEastAndDigit = True
.BaseLineAlignment = wdBaselineAlignAuto
End With
我定义了Style对象,但是还是不知道该如何操作,请各位高手多多指点,谢谢!!
[解决办法]
可以使用:_ParagraphFormat对象,示例如下:
COleVariant vUnit((long)5);
COleVariant vCount((long)1);
COleVariant vExtend((long)1);
COleVariant vStyle((long)-2); // -2为一级标题,...,-10为九级标题
Selection mySel = oWord.GetSelection();
mySel.MoveDown(&vUnit, &vCount, &vExtend); // 假设光标在段落头,选择当前段
_ParagraphFormat pf = mySel.GetParagraphFormat();
pf.SetStyle(&vStyle);
[解决办法]
下面的示例设置段落格式为正文:
COleVariant vStyle((long)-67);
pf.SetStyle(&vStyle);