Class: WordWps::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/word_wps.rb

Overview

对文本控件的调用

Instance Method Summary collapse

Constructor Details

#initialize(document, word) ⇒ Document

Returns a new instance of Document.



47
48
49
50
51
52
# File 'lib/word_wps.rb', line 47

def initialize(document, word)
  @doc_work = document
  @word = word
  #@text = Text.new(@word.Selection, self)
  create_style
end

Instance Method Details

#add_table(row, col) ⇒ Object

设置表格



108
109
110
# File 'lib/word_wps.rb', line 108

def add_table(row, col)
  @doc_work.tables.add(now.Range, row, col)
end

#add_textObject

返回Text类



97
98
99
100
# File 'lib/word_wps.rb', line 97

def add_text
  return Text.new(@word.Selection, self)
  entry
end

#cent_to_point(int) ⇒ Object

将厘米换算成磅



170
171
172
# File 'lib/word_wps.rb', line 170

def cent_to_point(int)
  @word.CentimetersToPoints(int)
end

#create_catalogObject

创建目录



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/word_wps.rb', line 113

def create_catalog
  #range = doc_work.Range(0, 0)
  range = now.range
  doc_work.TablesOfContents.Add(
    range,    # Range
    true,     # UseHeadingStyles
    1,        # UpperHeadingLevel
    3,        # LowerHeadingLevel default: 9
    false,    # UseFields
    nil,      # TableId
    true,     # RightAlignPageNumbers
    true,     # IncludePageNumbers
    "",       # AddedStyles
    true,     # UseHyperlinks
    true,     # HidePageNumbersInWeb
    true,     # UseOutlineLevels default: false
  )

  # 换到下一行
  move_down
end

#create_styleObject

创建样式



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/word_wps.rb', line 65

def create_style

  # 正文 楷体五号
  sty = @doc_work.styles("正文")
  sty.font.size = 10
  sty.font.NameFarEast = "宋体"
  sty.font.NameAscii = "Times New Roman"
  sty.font.NameOther = "Times New Roman"
  sty.ParagraphFormat.Alignment = 3
  sty.ParagraphFormat.LineSpacingRule = 1
  sty.NextParagraphStyle = "正文"

  # 楷体3号字体
  sty = @doc_work.styles.add("Cover 1", 1)
  sty.font.size = 16
  sty.font.NameFarEast = "楷体"
  sty.font.NameAscii = "Times New Roman"
  sty.font.NameOther = "Times New Roman"
end

#create_style_self(name) {|sty| ... } ⇒ Object

由实例自己创建风格

Yields:

  • (sty)


86
87
88
89
# File 'lib/word_wps.rb', line 86

def create_style_self(name)
  sty = @doc_work.styles.add(name, 1)
  yield sty
end

#doc_workObject

ActiveDocument



55
56
57
# File 'lib/word_wps.rb', line 55

def doc_work
  @doc_work
end

#end_keyObject

移动到行尾



151
152
153
# File 'lib/word_wps.rb', line 151

def end_key
  now.EndKey(5)
end

#entryObject

回车



141
142
143
# File 'lib/word_wps.rb', line 141

def entry
  now.TypeParagraph
end

#home_keyObject

移动到行首



146
147
148
# File 'lib/word_wps.rb', line 146

def home_key
  now.HomeKey(5)
end

#move_downObject

移动到下一行



156
157
158
159
# File 'lib/word_wps.rb', line 156

def move_down
  self.end_key
  self.entry
end

#move_right(count = nil, ext = nil, unit = nil) ⇒ Object

右移count 数值, 移动距离ext 0or1 , 是否扩展unit wdCharachter



165
166
167
# File 'lib/word_wps.rb', line 165

def move_right(count=nil, ext=nil, unit=nil)
  now.MoveRight(unit, count, ext)
end

#nowObject

返回Selection



92
93
94
# File 'lib/word_wps.rb', line 92

def now
  @word.Selection
end

#styles(name) ⇒ Object

风格设置



103
104
105
# File 'lib/word_wps.rb', line 103

def styles(name)
  @doc_work.Styles(name)
end

#update_catalogObject

更新目录



136
137
138
# File 'lib/word_wps.rb', line 136

def update_catalog
  word_basic.UpdateTableOfContents
end

#word_basicObject

自动化对象



60
61
62
# File 'lib/word_wps.rb', line 60

def word_basic
  doc_work.Application.WordBasic
end