分享
 
 
 

通过VBA操纵Microsoft Word的通用类 - 孙振芳

王朝vb·作者佚名  2006-06-27
窄屏简体版  字體: |||超大  

#Region "文件说明"

'***************************************************************************

' * * 文件名:WordLibrary.vb

' * * 创建人:孙振芳

' * * 日 期:2004-08-27

' * * 描 述:本类完成对Word的相关操作。

' * *

' * * 修改人:

' * * 日 期:

' * * 描 述:

' * *

' * * 版 本:1.0.0

' ***************************************************************************

#End Region

Public Class WordLibrary

'Word应用程序对象

Private mWordApp As Word.ApplicationClass

'Word文档对象

Private mDoc As Word.Document

'空值

Dim missing As Object = System.Reflection.Missing.Value

'构造函数

Public Sub New()

mWordApp = New Word.ApplicationClass

End Sub

'打开一个已经存在的Word文档并激活它

Public Sub Open(ByVal strFileName As String)

Dim fileName As Object = strFileName

Dim ro As Object = False

Dim isVisible As Object = True

mDoc = mWordApp.Documents.Open(fileName, missing, ro, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)

mDoc.Activate()

End Sub

'打开一个新建立的Word文档并激活它

Public Sub Open()

mDoc = mWordApp.Documents.Add(missing, missing, missing, missing)

mDoc.Activate()

End Sub

'退出Word应用程序

Public Sub Quit()

mWordApp.Application.Quit(missing, missing, missing)

End Sub

'保存Word文档

Public Sub Save()

mDoc.Save()

End Sub

'另存为Word文档

Public Sub SaveAs(ByVal strFileName As String)

Dim fileName As Object = strFileName

mDoc.SaveAs(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)

End Sub

Public Sub CloseDoc()

mDoc.Close(missing, missing, missing)

End Sub

'将Word文档另存为Html格式

Public Sub SaveAsHtml(ByVal strFileName As String)

Dim fileName As Object = strFileName

Dim Format As Object = CType(Word.WdSaveFormat.wdFormatHTML, Integer)

mDoc.SaveAs(fileName, Format, missing, missing, missing, missing, missing, missing, missing, missing, missing)

End Sub

'插入指定文本+一行

Public Sub InsertText(ByVal strText As String)

mWordApp.Selection.TypeText(strText)

mWordApp.Selection.TypeParagraph()

End Sub

'插入指定文本

Public Sub InsertSingleText(ByVal strText As String)

mWordApp.Selection.TypeText(strText)

End Sub

'插入一行

Public Sub InsertLineBreak()

mWordApp.Selection.TypeParagraph()

End Sub

'插入若干行

Public Sub InsertLineBreak(ByVal nline As Integer)

Dim i As Integer

For i = 0 To nline

mWordApp.Selection.TypeParagraph()

Next i

End Sub

'设定字体样式

Public Sub SetStyle(ByVal iStyle As Formats.StyleList)

Dim obj As Object

Select Case iStyle

Case Formats.StyleList.Normal

obj = "正文"

Case Formats.StyleList.Title1

obj = "标题 1"

Case Formats.StyleList.Title2

obj = "标题 2"

Case Formats.StyleList.Title3

obj = "标题 3"

Case Formats.StyleList.Title4

obj = "标题 4"

Case Formats.StyleList.Title5

obj = "标题 5"

Case Formats.StyleList.Title

obj = "标题"

End Select

mWordApp.Selection.Style = mWordApp.ActiveDocument.Styles.Item(obj)

End Sub

'设定段落格式

Public Sub SetAlignment(ByVal iType As Formats.AlignType)

Select Case iType

Case Formats.AlignType.Center

mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter

Case Formats.AlignType.Left

mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft

Case Formats.AlignType.Right

mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight

Case Formats.AlignType.Justify

mWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify

End Select

End Sub

'设置字体格式

Public Sub SetFont(ByVal iType As Formats.FontType, ByVal isSet As Boolean)

Select Case iType

Case Formats.FontType.Bold

mWordApp.Selection.Font.Bold = IIf(isSet = True, 1, 0)

Case Formats.FontType.Italic

mWordApp.Selection.Font.Italic = IIf(isSet = True, 1, 0)

Case Formats.FontType.Underlined

mWordApp.Selection.Font.Subscript = IIf(isSet = True, 1, 0)

End Select

End Sub

'设定字体名称

Public Sub SetFontName(ByVal strName As String)

mWordApp.Selection.Font.Name = strName

End Sub

'设定字体大小

Public Sub SetFontSize(ByVal iSize As Integer)

mWordApp.Selection.Font.Size = iSize

End Sub

'插入分页符

Public Sub InsertPageBreak()

Dim pBreak As Object = CInt(Word.WdBreakType.wdPageBreak)

mWordApp.Selection.InsertBreak(pBreak)

End Sub

'定位到一个预先定义好的书签,并指定该书签的值

Public Sub GotoBookMark(ByVal strBookMarkName As String)

Dim Bookmark As Object = CInt(Word.WdGoToItem.wdGoToBookmark)

Dim NameBookMark As Object = strBookMarkName

mWordApp.Selection.GoTo(Bookmark, missing, missing, NameBookMark)

End Sub

'定位到文档结尾

Public Sub GoToTheEnd()

Dim unit As Object

unit = Word.WdUnits.wdStory

mWordApp.Selection.EndKey(unit, missing)

End Sub

'定位到文档开头

Public Sub GoToTheBeginning()

Dim unit As Object

unit = Word.WdUnits.wdStory

mWordApp.Selection.HomeKey(unit, missing)

End Sub

'指定当前活动区域

Public Sub SetActivePane(ByVal iPane As Formats.ActivePane)

Select Case iPane

Case Formats.ActivePane.MainDocument

mDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument

Case Formats.ActivePane.CurrentPageHeader

mDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader

Case Formats.ActivePane.CurrentPageFooter

mDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter

End Select

End Sub

'创建表格

Public Sub CreateTable(ByVal rowCount As Integer, ByVal columnCount As Integer)

mDoc.Tables.Add(mWordApp.Selection.Range, rowCount, columnCount)

End Sub

'插入下面的行

Public Sub InsertBelowRow()

mWordApp.Selection.InsertRowsBelow()

End Sub

'移动到右单元格

Public Sub GoToRightCell()

Dim direction As Object

direction = Word.WdUnits.wdCell

mWordApp.Selection.MoveRight(direction, missing, missing)

End Sub

'移动到左单元格

Public Sub GoToLeftCell()

Dim direction As Object

direction = Word.WdUnits.wdCell

mWordApp.Selection.MoveLeft(direction, missing, missing)

End Sub

'移动到下单元格

Public Sub GoToDownCell()

Dim direction As Object

direction = Word.WdUnits.wdLine

mWordApp.Selection.MoveDown(direction, missing, missing)

End Sub

'移动到上单元格

Public Sub GoToUpCell()

Dim direction As Object

direction = Word.WdUnits.wdLine

mWordApp.Selection.MoveUp(direction, missing, missing)

End Sub

End Class

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有