Public
ClassWordOpLib
ClassWordOpLib
PrivateoWordApplicAsWord.Application
PrivateoDocumentAsWord.Document
PrivateoRangeAsWord.Range
PrivateoSelectionAsWord.Selection
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubNew()SubNew()
'激活comword接口
oWordApplic=NewWord.Application
oWordApplic.Visible=True
EndSub
'设置选定文本
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSetRange()SubSetRange(ByValparaAsInteger)
oRange=oDocument.Paragraphs(para).Range
oRange.Select()
EndSub
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSetRange()SubSetRange(ByValparaAsInteger,ByValsentAsInteger)
oRange=oDocument.Paragraphs(para).Range.Sentences(sent)
oRange.Select()
EndSub
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSetRange()SubSetRange(ByValstartpointAsInteger,ByValendpointAsInteger,ByValflagAsBoolean)
Ifflag=TrueThen
oRange=oDocument.Range(startpoint,endpoint)
oRange.Select()
Else
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EndIf
EndSub
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'生成空的新文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubNewDocument()SubNewDocument()
Dimmissing=System.Reflection.Missing.Value
DimisVisibleAsBoolean=True
oDocument=oWordApplic.Documents.Add(missing,missing,missing,missing)
oDocument.Activate()
EndSub
'使用模板生成新文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubNewDocWithModel()SubNewDocWithModel(ByValFileNameAsString)
Dimmissing=System.Reflection.Missing.Value
DimisVisibleAsBoolean=True
DimstrNameAsString
strName=FileName
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oDocument=oWordApplic.Documents.Add(strName,missing,missing,isVisible)
oDocument.Activate()
EndSub
'打开已有文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubOpenFile()SubOpenFile(ByValFileNameAsString)
DimstrNameAsString
DimisReadOnlyAsBoolean
DimisVisibleAsBoolean
Dimmissing=System.Reflection.Missing.Value
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strName=FileName
isReadOnly=False
isVisible=True
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oDocument=oWordApplic.Documents.Open(strName,missing,isReadOnly,missing,missing,missing,missing,missing,missing,missing,missing,isVisible,missing,missing,missing,missing)
oDocument.Activate()
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EndSub
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubOpenFile()SubOpenFile(ByValFileNameAsString,ByValisReadOnlyAsBoolean)
DimstrNameAsString
DimisVisibleAsBoolean
Dimmissing=System.Reflection.Missing.Value
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strName=FileName
isVisible=True
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oDocument=oWordApplic.Documents.Open(strName,missing,isReadOnly,missing,missing,missing,missing,missing,missing,missing,missing,isVisible,missing,missing,missing,missing)
oDocument.Activate()
EndSub
'退出Word
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubQuit()SubQuit()
Dimmissing=System.Reflection.Missing.Value
oWordApplic.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)
oWordApplic=Nothing
EndSub
'关闭所有打开的文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubCloseAllDocuments()SubCloseAllDocuments()
oWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
EndSub
'关闭当前的文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubCloseCurrentDocument()SubCloseCurrentDocument()
oDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
EndSub
'保存当前文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSave()SubSave()
Try
oDocument.Save()
Catch
MsgBox(Err.Description)
EndTry
EndSub
'另存为文档
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSaveAs()SubSaveAs(ByValFileNameAsString)
DimstrNameAsString
Dimmissing=System.Reflection.Missing.Value
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strName=FileName
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oDocument.SaveAs(strName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing)
EndSub
'保存为Html文件
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSaveAsHtml()SubSaveAsHtml(ByValFileNameAsString)
Dimmissing=System.Reflection.Missing.Value
DimstrNameAsString
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strName=FileName
Dimformat=CInt(Word.WdSaveFormat.wdFormatHTML)
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
oDocument.SaveAs(strName,format,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing)
EndSub
'插入文本
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubInsertText()SubInsertText(ByValtextAsString)
oWordApplic.Selection.TypeText(text)
EndSub
'插入一个空行
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubInsertLineBreak()SubInsertLineBreak()
oWordApplic.Selection.TypeParagraph()
EndSub
'插入指定行数的空行
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubInsertLineBreak()SubInsertLineBreak(ByVallinesAsInteger)
DimiAsInteger
Fori=1Tolines
oWordApplic.Selection.TypeParagraph()
Next
EndSub
'插入表格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubInsertTable()SubInsertTable(ByReftableAsDataTable)
DimoTableAsWord.Table
DimrowIndex,colIndex,NumRows,NumColumnsAsInteger
rowIndex=1
colIndex=0
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NumRows=table.Rows.Count+1
NumColumns=table.Columns.Count
oTable=oDocument.Tables.Add(oWordApplic.Selection.Range(),NumRows,NumColumns)
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'初始化列
DimRowAsDataRow
DimColAsDataColumn
ForEachColIntable.Columns
colIndex=colIndex+1
oTable.Cell(1,colIndex).Range.InsertAfter(Col.ColumnName)
Next
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'将行添入表格
ForEachRowIntable.Rows
rowIndex=rowIndex+1
colIndex=0
ForEachColIntable.Columns
colIndex=colIndex+1
oTable.Cell(rowIndex,colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next
oTable.AllowAutoFit=True
oTable.ApplyStyleFirstColumn=True
oTable.ApplyStyleHeadingRows=True
EndSub
'设置对齐
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSetAlignment()SubSetAlignment(ByValstrTypeAsString)
SelectCasestrType
Case"center"
oWordApplic.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter
Case"left"
oWordApplic.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft
Case"right"
oWordApplic.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight
Case"justify"
oWordApplic.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphJustify
EndSelect
EndSub
'设置字体
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSetStyle()SubSetStyle(ByValstrFontAsString)
SelectCasestrFont
Case"bold"
oWordApplic.Selection.Font.Bold=1
Case"italic"
oWordApplic.Selection.Font.Italic=1
Case"underlined"
oWordApplic.Selection.Font.Subscript=1
EndSelect
EndSub
'取消字体风格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubDissableStyle()SubDissableStyle()
oWordApplic.Selection.Font.Bold=0
oWordApplic.Selection.Font.Italic=0
oWordApplic.Selection.Font.Subscript=0
EndSub
'设置字体字号
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubSetFontSize()SubSetFontSize(ByValnSizeAsInteger)
oWordApplic.Selection.Font.Size=nSize
EndSub
'跳过本页
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubInsertPageBreak()SubInsertPageBreak()
DimpBreakAsInteger
pBreak=CInt(Word.WdBreakType.wdPageBreak)
oWordApplic.Selection.InsertBreak(pBreak)
EndSub
'转到书签
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoBookMark()SubGotoBookMark(ByValstrBookMarkAsString)
Dimmissing=System.Reflection.Missing.Value
DimBookMark=CInt(Word.WdGoToItem.wdGoToBookmark)
oWordApplic.Selection.GoTo(BookMark,missing,missing,strBookMark)
EndSub
'判断书签是否存在
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicFunctionBookMarkExist()FunctionBookMarkExist(ByValstrBookMarkAsString)AsBoolean
DimExistAsBoolean
Exist=oDocument.Bookmarks.Exists(strBookMark)
ReturnExist
EndFunction
'转到文档结尾
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoTheEnd()SubGotoTheEnd()
Dimmissing=System.Reflection.Missing.Value
Dimunit=Word.WdUnits.wdStory
oWordApplic.Selection.EndKey(unit,missing)
EndSub
'转到文档开头
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoTheBegining()SubGotoTheBegining()
Dimmissing=System.Reflection.Missing.Value
Dimunit=Word.WdUnits.wdStory
oWordApplic.Selection.HomeKey(unit,missing)
EndSub
'转到表格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoTheTable()SubGotoTheTable(ByValntableAsInteger)
'Dimmissing=System.Reflection.Missing.Value
'Dimwhat=Word.WdGoToItem.wdGoToTable
'Dimwhich=Word.WdGoToDirection.wdGoToFirst
'Dimcount=ntable
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'oWordApplic.Selection.GoTo(what,which,count,missing)
'oWordApplic.Selection.ClearFormatting()
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'oWordApplic.Selection.Text=""
oRange=oDocument.Tables(ntable).Cell(1,1).Range
oRange.Select()
EndSub
'转到表格的某个单元格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoTableCell()SubGotoTableCell(ByValntableAsInteger,ByValnRowAsInteger,ByValnColumnAsInteger)
oRange=oDocument.Tables(ntable).Cell(nRow,nColumn).Range
oRange.Select()
EndSub
'表格中转到右面的单元格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoRightCell()SubGotoRightCell()
Dimmissing=System.Reflection.Missing.Value
Dimdirection=Word.WdUnits.wdCell
oWordApplic.Selection.MoveRight(direction,missing,missing)
EndSub
'表格中转到左面的单元格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoLeftCell()SubGotoLeftCell()
Dimmissing=System.Reflection.Missing.Value
Dimdirection=Word.WdUnits.wdCell
oWordApplic.Selection.MoveLeft(direction,missing,missing)
EndSub
'表格中转到下面的单元格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoDownCell()SubGotoDownCell()
Dimmissing=System.Reflection.Missing.Value
Dimdirection=Word.WdUnits.wdCell
oWordApplic.Selection.MoveDown(direction,missing,missing)
EndSub
'表格中转到上面的单元格
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubGotoUpCell()SubGotoUpCell()
Dimmissing=System.Reflection.Missing.Value
Dimdirection=Word.WdUnits.wdCell
oWordApplic.Selection.MoveUp(direction,missing,missing)
EndSub
'插入图片
![ExpandedSubBlockStart.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
PublicSubInsertPic()SubInsertPic(ByValFileNameAsString)
Dimmissing=System.Reflection.Missing.Value
oWordApplic.Selection.InlineShapes.AddPicture(FileName,False,True,missing)
EndSub
![InBlock.gif](https://supersand.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EndClass
转载于:https://www.cnblogs.com/starstarfire/archive/2006/05/24/408429.html