原文出处 http://blog.csdn.net/szg3827/archive/2008/07/06/2618130.aspx

 

  1. 有时候我们需要在程序中操作Word文档,虽然C#可以用来写这样的一个类,但由于VB先天的优势(对VBA的完全支持),用VB来写显然是更好的选择。下面这个类基于网上的一个类,修改了其中的一些错误,添加了几个方法,在我的运行环境(VS.NET,Office2003)中运行通过。使用时,在“引用”中添加这个类的引用,就可以直接用C#调用类中的方法来操作Word文档了。  
  2.   
  3. Public Class WordOpLibClass WordOpLib  
  4.   
  5.     Private oWordApplic As Word.Application  
  6.   
  7.     Private oDocument As Word.Document  
  8.   
  9.     Private oRange As Word.Range  
  10.   
  11.     Private oSelection As Word.Selection  
  12.   
  13.     Public Sub New()Sub New()  
  14.   
  15.         '激活com  word接口   
  16.   
  17.         oWordApplic = New Word.Application  
  18.   
  19.         oWordApplic.Visible = True  
  20.   
  21.     End Sub  
  22.   
  23.     '设置选定文本   
  24.   
  25.     Public Sub SetRange()Sub SetRange(ByVal para As Integer)  
  26.   
  27.         oRange = oDocument.Paragraphs(para).Range  
  28.   
  29.         oRange.Select()  
  30.   
  31.     End Sub  
  32.   
  33.     Public Sub SetRange()Sub SetRange(ByVal para As IntegerByVal sent As Integer)  
  34.   
  35.         oRange = oDocument.Paragraphs(para).Range.Sentences(sent)  
  36.   
  37.         oRange.Select()  
  38.   
  39.     End Sub  
  40.   
  41.     Public Sub SetRange()Sub SetRange(ByVal startpoint As IntegerByVal endpoint As IntegerByVal flag As Boolean)  
  42.   
  43.         If flag = True Then  
  44.   
  45.             oRange = oDocument.Range(startpoint, endpoint)  
  46.   
  47.             oRange.Select()  
  48.   
  49.         Else  
  50.   
  51.   
  52.   
  53.         End If  
  54.   
  55.     End Sub  
  56.   
  57.   
  58.   
  59.     '生成空的新文档   
  60.   
  61.     Public Sub NewDocument()Sub NewDocument()  
  62.   
  63.         Dim missing = System.Reflection.Missing.Value  
  64.   
  65.         Dim isVisible As Boolean = True  
  66.   
  67.         oDocument = oWordApplic.Documents.Add(missing, missing, missing, missing)  
  68.   
  69.         oDocument.Activate()  
  70.   
  71.     End Sub  
  72.   
  73.     '使用模板生成新文档   
  74.   
  75.     Public Sub NewDocWithModel()Sub NewDocWithModel(ByVal FileName As String)  
  76.   
  77.         Dim missing = System.Reflection.Missing.Value  
  78.   
  79.         Dim isVisible As Boolean = True  
  80.   
  81.         Dim strName As String  
  82.   
  83.         strName = FileName  
  84.   
  85.   
  86.   
  87.         oDocument = oWordApplic.Documents.Add(strName, missing, missing, isVisible)  
  88.   
  89.         oDocument.Activate()  
  90.   
  91.     End Sub  
  92.   
  93.     '打开已有文档   
  94.   
  95.     Public Sub OpenFile()Sub OpenFile(ByVal FileName As String)  
  96.   
  97.         Dim strName As String  
  98.   
  99.         Dim isReadOnly As Boolean  
  100.   
  101.         Dim isVisible As Boolean  
  102.   
  103.         Dim missing = System.Reflection.Missing.Value  
  104.   
  105.   
  106.   
  107.         strName = FileName  
  108.   
  109.         isReadOnly = False  
  110.   
  111.         isVisible = True  
  112.   
  113.   
  114.   
  115.         oDocument = oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)  
  116.   
  117.         oDocument.Activate()  
  118.   
  119.   
  120.   
  121.     End Sub  
  122.   
  123.     Public Sub OpenFile()Sub OpenFile(ByVal FileName As StringByVal isReadOnly As Boolean)  
  124.   
  125.         Dim strName As String  
  126.   
  127.         Dim isVisible As Boolean  
  128.   
  129.         Dim missing = System.Reflection.Missing.Value  
  130.   
  131.   
  132.   
  133.         strName = FileName  
  134.   
  135.         isVisible = True  
  136.   
  137.   
  138.   
  139.         oDocument = oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)  
  140.   
  141.         oDocument.Activate()  
  142.   
  143.     End Sub  
  144.   
  145.     '退出Word   
  146.   
  147.     Public Sub Quit()Sub Quit()  
  148.   
  149.         Dim missing = System.Reflection.Missing.Value  
  150.   
  151.         oWordApplic.Quit()  
  152.   
  153.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)  
  154.   
  155.         oWordApplic = Nothing  
  156.   
  157.     End Sub  
  158.   
  159.     '关闭所有打开的文档   
  160.   
  161.     Public Sub CloseAllDocuments()Sub CloseAllDocuments()  
  162.   
  163.         oWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)  
  164.   
  165.     End Sub  
  166.   
  167.     '关闭当前的文档   
  168.   
  169.     Public Sub CloseCurrentDocument()Sub CloseCurrentDocument()  
  170.   
  171.         oDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)  
  172.   
  173.     End Sub  
  174.   
  175.     '保存当前文档   
  176.   
  177.     Public Sub Save()Sub Save()  
  178.   
  179.         Try  
  180.   
  181.             oDocument.Save()  
  182.   
  183.         Catch  
  184.   
  185.             MsgBox(Err.Description)  
  186.   
  187.         End Try  
  188.   
  189.     End Sub  
  190.   
  191.     '另存为文档   
  192.   
  193.     Public Sub SaveAs()Sub SaveAs(ByVal FileName As String)  
  194.   
  195.         Dim strName As String  
  196.   
  197.         Dim missing = System.Reflection.Missing.Value  
  198.   
  199.   
  200.   
  201.         strName = FileName  
  202.   
  203.   
  204.   
  205.         oDocument.SaveAs(strName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)  
  206.   
  207.     End Sub  
  208.   
  209.     '保存为Html文件   
  210.   
  211.     Public Sub SaveAsHtml()Sub SaveAsHtml(ByVal FileName As String)  
  212.   
  213.         Dim missing = System.Reflection.Missing.Value  
  214.   
  215.         Dim strName As String  
  216.   
  217.   
  218.   
  219.         strName = FileName  
  220.   
  221.         Dim format = CInt(Word.WdSaveFormat.wdFormatHTML)  
  222.   
  223.   
  224.   
  225.         oDocument.SaveAs(strName, format, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)  
  226.   
  227.     End Sub  
  228.   
  229.     '插入文本   
  230.   
  231.     Public Sub InsertText()Sub InsertText(ByVal text As String)  
  232.   
  233.         oWordApplic.Selection.TypeText(text)  
  234.   
  235.     End Sub  
  236.   
  237.     '插入一个空行   
  238.   
  239.     Public Sub InsertLineBreak()Sub InsertLineBreak()  
  240.   
  241.         oWordApplic.Selection.TypeParagraph()  
  242.   
  243.     End Sub  
  244.   
  245.     '插入指定行数的空行   
  246.   
  247.     Public Sub InsertLineBreak()Sub InsertLineBreak(ByVal lines As Integer)  
  248.   
  249.         Dim i As Integer  
  250.   
  251.         For i = 1 To lines  
  252.   
  253.             oWordApplic.Selection.TypeParagraph()  
  254.   
  255.         Next  
  256.   
  257.     End Sub  
  258.   
  259.     '插入表格   
  260.   
  261.     Public Sub InsertTable()Sub InsertTable(ByRef table As DataTable)  
  262.   
  263.         Dim oTable As Word.Table  
  264.   
  265.         Dim rowIndex, colIndex, NumRows, NumColumns As Integer  
  266.   
  267.         rowIndex = 1  
  268.   
  269.         colIndex = 0  
  270.   
  271.   
  272.   
  273.         NumRows = table.Rows.Count + 1  
  274.   
  275.         NumColumns = table.Columns.Count  
  276.   
  277.         oTable = oDocument.Tables.Add(oWordApplic.Selection.Range(), NumRows, NumColumns)  
  278.   
  279.   
  280.   
  281.   
  282.   
  283.         '初始化列   
  284.   
  285.         Dim Row As DataRow  
  286.   
  287.         Dim Col As DataColumn  
  288.   
  289.         For Each Col In table.Columns  
  290.   
  291.             colIndex = colIndex + 1  
  292.   
  293.             oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)  
  294.   
  295.         Next  
  296.   
  297.   
  298.   
  299.         '将行添入表格   
  300.   
  301.         For Each Row In table.Rows  
  302.   
  303.             rowIndex = rowIndex + 1  
  304.   
  305.             colIndex = 0  
  306.   
  307.             For Each Col In table.Columns  
  308.   
  309.                 colIndex = colIndex + 1  
  310.   
  311.                 oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))  
  312.   
  313.             Next  
  314.   
  315.         Next  
  316.   
  317.         oTable.AllowAutoFit = True  
  318.   
  319.         oTable.ApplyStyleFirstColumn = True  
  320.   
  321.         oTable.ApplyStyleHeadingRows = True  
  322.   
  323.     End Sub  
  324.   
  325.     '设置对齐   
  326.   
  327.     Public Sub SetAlignment()Sub SetAlignment(ByVal strType As String)  
  328.   
  329.         Select Case strType  
  330.   
  331.             Case "center"  
  332.   
  333.                 oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter  
  334.   
  335.             Case "left"  
  336.   
  337.                 oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft  
  338.   
  339.             Case "right"  
  340.   
  341.                 oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight  
  342.   
  343.             Case "justify"  
  344.   
  345.                 oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify  
  346.   
  347.         End Select  
  348.   
  349.     End Sub  
  350.   
  351.     '设置字体   
  352.   
  353.     Public Sub SetStyle()Sub SetStyle(ByVal strFont As String)  
  354.   
  355.         Select Case strFont  
  356.   
  357.             Case "bold"  
  358.   
  359.                 oWordApplic.Selection.Font.Bold = 1  
  360.   
  361.             Case "italic"  
  362.   
  363.                 oWordApplic.Selection.Font.Italic = 1  
  364.   
  365.             Case "underlined"  
  366.   
  367.                 oWordApplic.Selection.Font.Subscript = 1  
  368.   
  369.         End Select  
  370.   
  371.     End Sub  
  372.   
  373.     '取消字体风格   
  374.   
  375.     Public Sub DissableStyle()Sub DissableStyle()  
  376.   
  377.         oWordApplic.Selection.Font.Bold = 0  
  378.   
  379.         oWordApplic.Selection.Font.Italic = 0  
  380.   
  381.         oWordApplic.Selection.Font.Subscript = 0  
  382.   
  383.     End Sub  
  384.   
  385.     '设置字体字号   
  386.   
  387.     Public Sub SetFontSize()Sub SetFontSize(ByVal nSize As Integer)  
  388.   
  389.         oWordApplic.Selection.Font.Size = nSize  
  390.   
  391.     End Sub  
  392.   
  393.     '跳过本页   
  394.   
  395.     Public Sub InsertPageBreak()Sub InsertPageBreak()  
  396.   
  397.         Dim pBreak As Integer  
  398.   
  399.         pBreak = CInt(Word.WdBreakType.wdPageBreak)  
  400.   
  401.         oWordApplic.Selection.InsertBreak(pBreak)  
  402.   
  403.     End Sub  
  404.   
  405.     '转到书签   
  406.   
  407.     Public Sub GotoBookMark()Sub GotoBookMark(ByVal strBookMark As String)  
  408.   
  409.         Dim missing = System.Reflection.Missing.Value  
  410.   
  411.         Dim BookMark = CInt(Word.WdGoToItem.wdGoToBookmark)  
  412.   
  413.         oWordApplic.Selection.GoTo(BookMark, missing, missing, strBookMark)  
  414.   
  415.     End Sub  
  416.   
  417.     '判断书签是否存在   
  418.   
  419.     Public Function BookMarkExist()Function BookMarkExist(ByVal strBookMark As StringAs Boolean  
  420.   
  421.         Dim Exist As Boolean  
  422.   
  423.         Exist = oDocument.Bookmarks.Exists(strBookMark)  
  424.   
  425.         Return Exist  
  426.   
  427.     End Function  
  428.   
  429.     '转到文档结尾   
  430.   
  431.     Public Sub GotoTheEnd()Sub GotoTheEnd()  
  432.   
  433.         Dim missing = System.Reflection.Missing.Value  
  434.   
  435.         Dim unit = Word.WdUnits.wdStory  
  436.   
  437.         oWordApplic.Selection.EndKey(unit, missing)  
  438.   
  439.     End Sub  
  440.   
  441.     '转到文档开头   
  442.   
  443.     Public Sub GotoTheBegining()Sub GotoTheBegining()  
  444.   
  445.         Dim missing = System.Reflection.Missing.Value  
  446.   
  447.         Dim unit = Word.WdUnits.wdStory  
  448.   
  449.         oWordApplic.Selection.HomeKey(unit, missing)  
  450.   
  451.     End Sub  
  452.   
  453.     '转到表格   
  454.   
  455.     Public Sub GotoTheTable()Sub GotoTheTable(ByVal ntable As Integer)  
  456.   
  457.         'Dim missing = System.Reflection.Missing.Value   
  458.   
  459.         'Dim what = Word.WdGoToItem.wdGoToTable   
  460.   
  461.         'Dim which = Word.WdGoToDirection.wdGoToFirst   
  462.   
  463.         'Dim count = ntable   
  464.   
  465.   
  466.   
  467.         'oWordApplic.Selection.GoTo(what, which, count, missing)   
  468.   
  469.         'oWordApplic.Selection.ClearFormatting()   
  470.   
  471.   
  472.   
  473.         'oWordApplic.Selection.Text = ""   
  474.   
  475.         oRange = oDocument.Tables(ntable).Cell(1, 1).Range  
  476.   
  477.         oRange.Select()  
  478.   
  479.     End Sub  
  480.   
  481.     '转到表格的某个单元格   
  482.   
  483.     Public Sub GotoTableCell()Sub GotoTableCell(ByVal ntable As IntegerByVal nRow As IntegerByVal nColumn As Integer)  
  484.   
  485.         oRange = oDocument.Tables(ntable).Cell(nRow, nColumn).Range  
  486.   
  487.         oRange.Select()  
  488.   
  489.     End Sub  
  490.   
  491.     '表格中转到右面的单元格   
  492.   
  493.     Public Sub GotoRightCell()Sub GotoRightCell()  
  494.   
  495.         Dim missing = System.Reflection.Missing.Value  
  496.   
  497.         Dim direction = Word.WdUnits.wdCell  
  498.   
  499.         oWordApplic.Selection.MoveRight(direction, missing, missing)  
  500.   
  501.     End Sub  
  502.   
  503.     '表格中转到左面的单元格   
  504.   
  505.     Public Sub GotoLeftCell()Sub GotoLeftCell()  
  506.   
  507.         Dim missing = System.Reflection.Missing.Value  
  508.   
  509.         Dim direction = Word.WdUnits.wdCell  
  510.   
  511.         oWordApplic.Selection.MoveLeft(direction, missing, missing)  
  512.   
  513.     End Sub  
  514.   
  515.     '表格中转到下面的单元格   
  516.   
  517.     Public Sub GotoDownCell()Sub GotoDownCell()  
  518.   
  519.         Dim missing = System.Reflection.Missing.Value  
  520.   
  521.         Dim direction = Word.WdUnits.wdCell  
  522.   
  523.         oWordApplic.Selection.MoveDown(direction, missing, missing)  
  524.   
  525.     End Sub  
  526.   
  527.     '表格中转到上面的单元格   
  528.   
  529.     Public Sub GotoUpCell()Sub GotoUpCell()  
  530.   
  531.         Dim missing = System.Reflection.Missing.Value  
  532.   
  533.         Dim direction = Word.WdUnits.wdCell  
  534.   
  535.         oWordApplic.Selection.MoveUp(direction, missing, missing)  
  536.   
  537.     End Sub  
  538.   
  539.     '插入图片   
  540.   
  541.     Public Sub InsertPic()Sub InsertPic(ByVal FileName As String)  
  542.   
  543.         Dim missing = System.Reflection.Missing.Value  
  544.   
  545.         oWordApplic.Selection.InlineShapes.AddPicture(FileName, FalseTrue, missing)  
  546.   
  547.     End Sub  
  548.   
  549.   
  550.   
  551. End Class