iWebOffice使用VBA控制字体

来源:互联网 发布:尼龙网眼布淘宝 编辑:程序博客网 时间:2024/06/11 02:50


一、
前言

随着金格iWebOffice系列中间件产品的广泛应用,庞大的客户实体带来了丰富而复杂的应用需求,产品的更新速度也随着这些需求不断地加快。有些客户不仅想的快速的阅读文档,还想快捷方便的设置文档,其中文字设置最为常见,下面主要将介绍如何使用VBA实现控制这些字体。

二、控制说明

下面代码中“WebOffice”是iWebOffice控件对象,“WebObject”是iWebOffice控件提供用于访问VBA的对象。

1)光标选中的字体,字体变为红色。

  1. <!--光标选择的字体-->
  2. WebOffice.WebObject.Application.Selection.Font.Color = 255;     //字体变红
复制代码

2)书签中的字体,字体变为红色。

  1. <!--书签中的字体-->
  2. WebOffice.WebObject.Bookmarks("Caption").Range.Font.Color = 255;   //字体变红
复制代码

3)文档中第二个词组的颜色变为红色。

  1. <!--文档中第二个词组的颜色-->
  2. WebOffice.WebObject.Application.ActiveDocument.Words(2).Font.Color = 255;  //字体变红
复制代码

4)光标所在段落的字体。

  1. <!--光标所在段落的字体-->
  2. WebOffice.WebObject.Application.Selection.Paragraphs(1).Range.Font.Color = 255;  
复制代码

5)文档开头字体中,3到第9的字体变为红色。

  1. <!--文档开头字体设置 -->
  2. WebOffice.WebObject.Application.ActiveDocument.Range(3,9).Font.Color = 255;
复制代码

6)设置所有字体样式为Color的字体

  1. <!--设置所有字体样式为Color的字体-->
  2. WebOffice.WebObject.Application.ActiveDocument.Styles(“Color”).Font.Color = 255;
  3. //字体变红
复制代码

图解:A、在word工具栏“样式”中设置一个新的样式Color


(图2-1:样式Color)

B、用改样式标记文档中的文字,选中样式为Color,如图所示


(图2-2:样式Color效果)

C、讲所有样式为Color字体变为红色


(图2-2:样式Color设置为红色效果)

三、文字属性设置

1)设置文字的中文字体为“华文中宋”。

  1. <!--设置一种东亚字体名称-->
  2. WebOffice.WebObject.Application.Selection.Font.NameFarEast = "华文中宋";
复制代码

2)设置选中的文字中所有英文字体为“Times New Roman”。

  1. <!--设置使用的英文字体-->
  2. WebOffice.WebObject.Application.Selection.Font.NameAscii = "Times New Roman";
复制代码

3)设置所有选中的文字的字体(不区分中英文)为“宋体”。

  1. <!--设置所有文字的字体-->
  2. WebOffice.WebObject.Application.Selection.Font.Name = "宋体";
复制代码

4)设置选中字体的大小为14

  1. <!--设置所有字体大小-->
  2. WebOffice.WebObject.Application.Selection.Font.Size =14;
复制代码

5)设置选中的字体为粗体。

  1. <!--粗体-->
  2. WebOffice.WebObject.Application.Selection.Font.Bold = 1;
复制代码

6)设置选中的字体为斜体。

  1. <!--斜体-->
  2. WebOffice.WebObject.Application.Selection.Font.Italic = 1;
复制代码

7)为选中的字体添加删除线。

  1. <!--删除线-->
  2. WebOffice.WebObject.Application.Selection.Font.StrikeThrough = 1;
复制代码

图解:


(图3-1:删除线效果)

8)为选中的字体添加双删除线。

  1. <!--双删除线-->
  2. WebOffice.WebObject.Application.Selection.Font.DoubleStrikeThrough =1;
复制代码

图解:


(图3-2:双删除线效果)

9)选中的文字添加阴影。

  1. <!--阴影-->
  2. WebOffice.WebObject.Application.Selection.Font.Shadow  =1;
复制代码

10)把选中的字体隐藏。

  1. <!--隐藏文字-->
  2. WebOffice.WebObject.Application.Selection.Font.Hidden  =1;
复制代码

11)选中的文字中全部英文字体设置为大写字母。

  1. <!--全部大写字母-->
  2. WebOffice.WebObject.Application.Selection.Font.AllCaps = 1;
复制代码

12)选中的字体设置为上标。

  1. <!--上标-->
  2. WebOffice.WebObject.Application.Selection.Font.Superscript  = 1;
复制代码

图解:


(图3-3:
选中的字体设置为上标)

13)选中的字体设置为下标。

  1. <!--下标-->
  2. WebOffice.WebObject.Application.Selection.Font.Subscript = 1;
复制代码

图解:


(图3-3:
选中的字体设置为下标)

14)设置选中文字的字符间距。

  1. <!--字符间距-->
  2. WebOffice.WebObject.Application.Selection.Font.Spacing  = 10;
复制代码

15)设置选中文字的字符缩放。

  1. <!--字符缩放-->
  2. WebOffice.WebObject.Application.Selection.Font.Scaling = 1;                        //数字表示距离
复制代码

16)添加下划线。

  1. <!--下划线-->
  2. WebOffice.WebObject.Application.Selection.Font.Underline = 1;
复制代码

17)添加双下划线。

  1. <!--双下划线-->
  2. WebOffice.WebObject.Application.Selection.Font.Underline = 3;                //下划线的条数
  3. WebOffice.WebObject.Application.Selection.Font.UnderlineColor = 255;        //下划线颜色
复制代码

图解:


(图3-4:
添加双下划线)

18)选中字体添加文字效果,当前为闪烁效果。

  1. <!--设置文字闪动的效果-->
  2. WebOffice.WebObject.Application.Selection.Font.Animation = 2;
复制代码
0 0
原创粉丝点击