两种方式,
一:通过写死具体单元格的行列获取
二:通过标签获取,标签获取实际不如写死单元格方便,因为要获取的位置较多,设置很麻烦
Dim FmObj As New OpenFileDialog
FmObj.Filter = "Word文件|*.doc;*.docx|Html文件|*.html"
FmObj.Multiselect = False
If FmObj.ShowDialog(Me) <> DialogResult.OK Then
Exit Sub
End If
Dim FilePath As String = FmObj.FileName
Dim FileExt As String = System.IO.Path.GetExtension(FilePath).ToLower
Dim SysWorkPath As String = Application.StartupPath & "\..\" '系统路径
Dim SysTmpPath As String = SysWorkPath & "..\Tmp\" '临时目录路径
Try
If FileExt = ".doc" OrElse FileExt = ".docx" Then
Dim wordDoc As Interop.Word.Document
Dim wordApp As New Microsoft.Office.Interop.Word.Application
wordDoc = wordApp.Documents.Open(FilePath, [ReadOnly]:=0)
'table直接获取方式,可以配合XML设置每个单元格的位置
Dim tbs As Microsoft.Office.Interop.Word.Tables = wordDoc.Content.Tables
Dim tb As Microsoft.Office.Interop.Word.Table
For Each tb In tbs
'此处我是对取出的文本进行了二次处理,去除了ChrW(7)和vbCr两个字符
Me.TextBox_InquireNo.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(2, 3).Range.Text)
Me.TextBox_Description.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(3, 2).Range.Text)
Me.XySe_IssueDate.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(4, 2).Range.Text)
Me.XySe_RequestedReturnDate.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(4, 4).Range.Text)
Me.ComboBox_AcType.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(6, 2).Range.Text)
Me.ComboBox_EngType.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(6, 4).Range.Text)
Me.ComboBox_Release.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(7, 2).Range.Text)
Me.TextBox_Event.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(7, 4).Range.Text)
Me.XySe_CusReqDate.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(8, 2).Range.Text)
Me.ComboBox_AcReg.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(8, 4).Range.Text)
Me.XySe_DueDate.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(9, 2).Range.Text)
Me.ComboBox_Location.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(9, 4).Range.Text)
Me.TextBox_NonRoutineMhCap.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(10, 2).Range.Text)
Me.TextBox_MaterialCapAndCharge.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(11, 2).Range.Text)
Me.TextBox_AccessFromCustomer.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(12, 3).Range.Text)
Me.XySe_EstimateTime.Text = Quo_Common.repliceDocExcelErrorChar(tb.Cell(13, 3).Range.Text)
Next
'书签获取方式容易因为无法设置单元格无法继续
Dim wordBookMarks As Interop.Word.Bookmarks = wordDoc.Bookmarks
For Each wordBookMark As Interop.Word.Bookmark In wordBookMarks
wordBookMark.Select()
MsgBox(wordBookMark.Name)
Dim ss1 As String = wordBookMark.Range.Text
If ss1 <> "" Then
MsgBox(ss1)
End If
Next
wordDoc.Close()
wordApp.Quit()
End If
Catch ex As Exception
End Try
来源:oschina
链接:https://my.oschina.net/u/3471785/blog/4309383