本文VB.Net源码, 针对富文本控件,复制、粘贴、删除、剪切、撤销、插入图片,导出Word文档 ,导入WORD 或RTF 文档等功能做了实例,简单易用,仅供参考。
引用:
Imports System.IO
Imports Microsoft.Office.Interop
说明:Text25 是富文本控件 RichtextBox 关联右键内容菜单,添加菜单按钮,写入回应的按钮事件。
'一、设置选中文本的字体、字号、等风格
Private Sub CmdFontsize_Click(sender As Object, e As EventArgs) Handles CmdFontsize.Click
' 设置选定文本字体风格
Me.Text25.HideSelection = False
Dim fontDialog As FontDialog = New FontDialog()
If fontDialog.ShowDialog() = DialogResult.OK Then
Me.Text25.SelectionFont = fontDialog.Font
End If
End Sub
'二、设置选中文本的前颜色
Private Sub CmdWordColor_Click(sender As Object, e As EventArgs) Handles CmdWordColor.Click
' 设置选定文本的前景色
Me.Text25.HideSelection = False
Dim colorDialog As ColorDialog = New ColorDialog()
If colorDialog.ShowDialog() = DialogResult.OK Then
Me.Text25.SelectionColor = colorDialog.Color
End If
End Sub
'三、复制选中文本内容
Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuCopy.Click
Me.Text25.Copy()
End Sub
'四、光标位置粘贴
Private Sub ToolStripMenuItem4_Click(sender As Object, e As EventArgs) Handles ToolStripMenuPaste.Click
Me.Text25.Paste()
End Sub
'五、删除选中内容
Private Sub ToolStripMenuDel_Click(sender As Object, e As EventArgs) Handles ToolStripMenuDel.Click
Text25.SelectedText = ""
End Sub
'六、剪切按钮
Private Sub ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ToolStripMenuCut.Click
Me.Text25.Cut()
End Sub
'七、全选按钮
Private Sub ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles ToolStripMenuSelectALL.Click
Me.Text25.SelectAll()
End Sub
'八、撤销按钮
Private Sub ToolStripMenuUndo_Click(sender As Object, e As EventArgs) Handles ToolStripMenuUndo.Click
Me.Text25.Undo()
End Sub
'九、光标位置插入图片
Private Sub ToolStripMenuInsMap_Click(sender As Object, e As EventArgs) Handles ToolStripMenuInsMap.Click
Dim path As String = System.IO.Directory.GetCurrentDirectory()
With OpenFileDialog1 '打开文件对话框控件
Dim bmp As Bitmap
.CheckFileExists = True
.FileName = ""
.CheckPathExists = True
.InitialDirectory = .FileName.Substring(0, .FileName.LastIndexOf("\") + 1)
.Multiselect = False
.Filter = "所有文件(*.*)|*.*|Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg|Png Files(*.png)|*.Png|Tif Files(*.tif)|*.tif|Jpeg Files(*.jpeg)|*.jpeg"
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
bmp = New Bitmap(.FileName)
Clipboard.SetData(DataFormats.Bitmap, bmp)
Me.Text25.Paste()
Clipboard.Clear()
End If
End With
End Sub
'十、居左按钮
Private Sub CmdWordALeft_Click(sender As Object, e As EventArgs) Handles CmdWordALeft.Click
Me.Text25.SelectionAlignment = HorizontalAlignment.Left
End Sub
Private Sub CmdARight_Click(sender As Object, e As EventArgs) Handles CmdARight.Click '十一、居右按钮
Me.Text25.SelectionAlignment = HorizontalAlignment.Right
End Sub
'十二、居中按钮
Private Sub CmdACenter_Click(sender As Object, e As EventArgs) Handles CmdACenter.Click
Me.Text25.SelectionAlignment = HorizontalAlignment.Center
End Sub
'十三、导出Word文档
Private Sub CmdSaveWord_Click(sender As Object, e As EventArgs) Handles CmdSaveWord.Click '导出Word文档
Try
Dim StrSysID As String = Me.Text1.Text
If StrSysID = "" Then Exit Sub
Dim SaveDialog As New SaveFileDialog
SaveDialog.Filter = "所有文件(*.*)|*.*|Rtf Files(*.rtf)|*.rtf|DOCX Files(*.DOCX)|*.DOCX|DOC Files(*.DOC)|*.DOC"
SaveDialog.CheckFileExists = False
SaveDialog.FileName = "征迁协议" + StrSysID
SaveDialog.CheckPathExists = True
SaveDialog.InitialDirectory = SaveDialog.FileName.Substring(0, SaveDialog.FileName.LastIndexOf("\") + 1)
If SaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
With SaveDialog
Dim PaWord As String = .FileName + ".doc"
Dim Parft As String = .FileName + ".rtf"
Text25.SaveFile(Parft)
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
wordApp = New Word.Application
' 使Word可见
wordApp.Visible = True
wordDoc = wordApp.Documents.Open(Parft)
wordDoc.SaveAs(PaWord, Word.WdSaveFormat.wdFormatDocument)
End With
End If
Catch ex As Exception
MsgBox("导出Word出错!" + ex.ToString, vbOKOnly + 64, "系统提示")
End Try
End Sub
'十四、导入Word文档或Rtf文档
Private Sub CmdImportWord_Click(sender As Object, e As EventArgs) Handles CmdImportWord.Click
Try
With OpenFileDialog1 '打开文件对话框控件
.CheckFileExists = True
.FileName = ""
.CheckPathExists = True
.InitialDirectory = .FileName.Substring(0, .FileName.LastIndexOf("\") + 1)
.Multiselect = False
.Filter = "所有文件(*.*)|*.*|Rtf Files(*.rtf)|*.rtf|DOCX Files(*.DOCX)|*.DOCX|DOC Files(*.DOC)|*.DOC"
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim StrFileExtName As String = Path.GetExtension(.FileName)'文件扩展名
If StrFileExtName.ToUpper = ".RTF" Then
Dim wordApp As New Word.Application
Dim wordFilePath As String = .FileName
'加载到RichTextBox
Text25.LoadFile(wordFilePath)
Else
If StrFileExtName.ToUpper = ".DOC" Or StrFileExtName.ToUpper = ".DOCX" Then
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
wordApp = New Word.Application
wordApp.Visible = False’隐藏WORDAPP
wordDoc = wordApp.Documents.Open(.FileName)
Dim Parft As String = Application.StartupPath + "\Tmp.rtf"
wordDoc.SaveAs(Parft, Word.WdSaveFormat.wdFormatRTF)
wordDoc.Close()
wordApp.Quit()
'加载到RichTextBox
Text25.LoadFile(Parft)
File.Delete(Parft)’删除临时文件
Else
MsgBox("请选择DOCX文件!", vbOKOnly + 64, "系统提示")
End If
End If
End If
End With
Catch ex As Exception
MsgBox("导出Word出错!" + ex.ToString, vbOKOnly + 64, "系统提示")
End Try
End Sub
复制过去做简单修改,即可。欢迎交流,共同研究。

4912

被折叠的 条评论
为什么被折叠?



