ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 4417|回复: 4

[求助] 如何用VBA自定义样式并将其应用到相应的文字上去啊?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2008-11-3 22:49 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
如何用VBA自定义样式并将其应用到相应的文字上去啊?
好比下面的文字:
1)我要将“TEST FOR ENGLISH MAJORS (2008) - GRADE EIGHT - ”设置为标题1,我想基于“正文”样式自定义,好比,黑体、居中、段前2行、段后1.5行、行距默认,然后好比用替换的办法将其应用到2008,2007等标题;
2)我要设置一个名称为paragraph的自定义样式,并将其应用到其他文字上。这个样式好比是:字体Times New Roman,字号12,首行缩进2字符,行距1.25。
我用录制了一个,照葫芦画瓢,倒是可以定义样式,但一应用后,字体咋就变成宋体的了呢,特别难看,所以请各位赐教啊!

TEST FOR ENGLISH MAJORS (2008) - GRADE EIGHT -
TIME LIMIT: 195 MIN
PARTI LISTENING COMPREHENSION (35 MIN)
SECTION A MINI - LECTURE
In this section you will hear a mini - lecture. You will hear the lecture ONCE ONLY. While listening, take notes on the important points. Your notes will not be marked, but you will need them to complete a gap - filling task after the mini - lecture. When the lecture is over, you will be given two minutes to check your notes, and another ten minutes to complete the gap - filling task on ANSWER SHEET ONE. Use the blank sheet for note - taking.
SECTION
B. CONVERSATION.
In this section you will hear everything ONCE ONLY. Listen carefully and then answer the questions that follow. Mark the correct answer to each question on your coloured answer sheet.
Questions 1 to 5 are based on a conversation. At the end of the conversation you will be given 10 seconds to answer each of the following five questions.
Now listen to the conversation.
1.        Mary doesn't seem to favour the idea of a new the idea of a new the idea of a new the idea of a new the idea of a new the idea of a new airport because ________.
A. the existing airports are to the idea of a new the idea of a new the idea of a new the idea of a new the idea of a new the idea of a new the idea of a new be wasted
B. more people will be encouraged to travel
C. more oil will be consumed
D. more airplanes will be purchased

................

TEST FOR ENGLISH MAJORS (2007) - GRADE EIGHT -

...................

[ 本帖最后由 gemj 于 2008-11-3 23:04 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-11-4 17:38 | 显示全部楼层
好久没来,看到守柔,如意版主又出新著了,实在感到高兴!你们的书永远是精品!

强烈建议,啥时候版主及众高手们出一本WORD VBA方面的书啊?尽量详尽些,我第一个预订啊!

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-11-4 17:31 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
没人理我啊,呵呵!再补充点啊,下面是我录制的宏代码:

Sub DefineStyle()
'
' DefineStyle Macro
' Macro recorded 2008-11-3 by TopSage
'
    ActiveDocument.Styles.Add Name:="paragraphTS", Type:=wdStyleTypeParagraph
    ActiveDocument.Styles("paragraphTS").AutomaticallyUpdate = False
    With ActiveDocument.Styles("paragraphTS").Font
        .NameFarEast = "宋体"
        .NameAscii = "Times New Roman"
        .NameOther = "Times New Roman"
        .Name = "宋体"
        .Size = 12
        .Bold = False
        .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("paragraphTS").ParagraphFormat
        .LeftIndent = CentimetersToPoints(0)
        .RightIndent = CentimetersToPoints(0)
        .SpaceBefore = 2.5
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(1.25)
        .Alignment = wdAlignParagraphJustify
        .WidowControl = False
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = CentimetersToPoints(0.85)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 2
        .LineUnitBefore = 0.5
        .LineUnitAfter = 0
        .AutoAdjustRightIndent = True
        .DisableLineHeightGrid = False
        .FarEastLineBreakControl = True
        .WordWrap = True
        .HangingPunctuation = True
        .HalfWidthPunctuationOnTopOfLine = False
        .AddSpaceBetweenFarEastAndAlpha = True
        .AddSpaceBetweenFarEastAndDigit = True
        .BaseLineAlignment = wdBaselineAlignAuto
    End With
    ActiveDocument.Styles("paragraphTS").NoSpaceBetweenParagraphsOfSameStyle = _
         False
    ActiveDocument.Styles("paragraphTS").ParagraphFormat.TabStops.ClearAll
    With ActiveDocument.Styles("paragraphTS").ParagraphFormat
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorAutomatic
        End With
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        With .Borders
            .DistanceFromTop = 1
            .DistanceFromLeft = 4
            .DistanceFromBottom = 1
            .DistanceFromRight = 4
            .Shadow = False
        End With
    End With
    ActiveDocument.Styles("paragraphTS").LanguageID = wdSimplifiedChinese
    ActiveDocument.Styles("paragraphTS").NoProofing = False
    ActiveDocument.Styles("paragraphTS").LanguageID = wdSimplifiedChinese
    ActiveDocument.Styles("paragraphTS").NoProofing = False
    ActiveDocument.Styles("paragraphTS").Frame.Delete
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2008-11-4 17:35 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
可不管我直接使用上述的代码,还是简化后的这个代码:

Sub DefineStyle()
'
    ActiveDocument.Styles.Add Name:="paragraphTS", Type:=wdStyleTypeParagraph
    With ActiveDocument.Styles("paragraphTS").Font
        .Size = 12
    End With
    With ActiveDocument.Styles("paragraphTS").ParagraphFormat
        .LineSpacing = LinesToPoints(1.25)
        .CharacterUnitFirstLineIndent = 2
    End With
End Sub

所定义的样式,再用它应用到文本的时候,所有字体(包括英文)就都成了宋体了,这是咋回事啊?如何改进?

TA的精华主题

TA的得分主题

发表于 2018-4-27 09:55 | 显示全部楼层
    With ActiveDocument.Styles("paragraphTS").Font
        .NameFarEast = "宋体"
        .NameAscii = "Times New Roman"
        .NameOther = "Times New Roman"
        .Name = "宋体"
这段的“宋体”改成你要的字体试一试
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-12 03:52 , Processed in 0.020800 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表