VB的文本框控件TextBox打开Office的Word文档
来源:互联网 发布:中传mfa艺术硕士知乎 编辑:程序博客网 时间:2024/05/17 14:14
有网友问我,VB的文本框控件TextBox能否打开Office的Word文档。VB的TextBox是Ansi版本的,通过open语句打开载入到TextBox中,肯定是乱码,没啥说的。但是,我们可以通过间接的方式打开Office的Word文档,通过一系列的API调用,将文档转换到TextBox中,而且保留了Word的版式(除字体变化以外)。请看下面程序:
标准模块:
'标准模块Option ExplicitPublic Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As LongPublic Const WM_GETTEXT As Long = &HD&Public Const WM_SETTEXT As Long = &HC&Private Const EM_GETLINECOUNT As Long = &HBA&Private Const EM_GETLINE As Long = &HC4&Public Const WM_CLOSE As Long = &H10& '关闭窗口消息Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As LongPublic Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As LongPublic Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As BooleanPrivate Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long'Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)Public OfficeWordFileName As String 'Office Word File Name(DOC格式的)Public WordPadHwnd As Long '写字板窗口句柄Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean Dim WindowCaption As String, LengthCaption As Long ', WindowClassName As String * 256 LengthCaption = GetWindowTextLength(hWnd) WindowCaption = Space(LengthCaption) Call GetWindowText(hWnd, WindowCaption, LengthCaption + 1) If InStr(1, WindowCaption, OfficeWordFileName) > 0 Then EnumChildWindows hWnd, AddressOf EnumChildWindowsProc, ByVal 0& WordPadHwnd = hWnd '保存写字板句柄 Debug.Print WordPadHwnd EnumWindowsProc = False End If EnumWindowsProc = TrueEnd FunctionPublic Function EnumChildWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean Dim WindowClassName As String * 256 Dim txtLineCount As Long, i As Long, TextString As String, sText As String Call GetClassName(hWnd, WindowClassName, 256) If InStr(1, WindowClassName, "RICHEDIT50W") > 0 Then txtLineCount = SendMessage(hWnd, EM_GETLINECOUNT, 0, ByVal 0&) '取得Word文档的行数 If txtLineCount > 0 Then For i = 0 To txtLineCount - 1 Dim strTXT(1024) As Byte strTXT(1) = 1 SendMessage hWnd, EM_GETLINE, i, strTXT(0) '取得每一行内容 sText = StrConv(strTXT, vbUnicode) sText = Left$(sText, InStr(1, sText, Chr(0)) - 1) & vbCrLf TextString = TextString & sText '连接每一行 Next Form1.Text1.Text = TextString Else Form1.Text1.Text = "Word文件无内容" End If EnumChildWindowsProc = False End If EnumChildWindowsProc = TrueEnd Function
Form1窗体模块:
'窗体上放1个文本框控件Text1、1个命令按钮Command1、1个通用对话框控件CommonDialog1Option ExplicitPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Const SYNCHRONIZE As Long = &H100000Private Sub Command1_Click() Dim Fname() As String, PIDshell As Long, hProcess As Long ' 设置“CancelError”为 True CommonDialog1.CancelError = True On Error GoTo ErrHandler ' 设置标志 CommonDialog1.Flags = cdlOFNHideReadOnly ' 设置过滤器 CommonDialog1.Filter = "Office Word Files(*.doc)|*.doc|All Files(*.*)|*.*" ' 指定缺省的过滤器 CommonDialog1.FilterIndex = 1 ' 显示“打开”对话框 CommonDialog1.ShowOpen '取得选定文件的名字 OfficeWordFileName = CommonDialog1.FileName PIDshell = Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe " & OfficeWordFileName, vbHide) hProcess = OpenProcess(SYNCHRONIZE, 0, PIDshell) Debug.Print "PID:"; hProcess If hProcess <> 0 Then WaitForSingleObject hProcess, 1000& '等待1秒,让shell执行完毕 CloseHandle hProcess End If Fname = Split(OfficeWordFileName, "\") OfficeWordFileName = Fname(UBound(Fname)) 'Sleep (1000) '等待1秒,让shell执行完毕 EnumWindows AddressOf EnumWindowsProc, ByVal 0& Exit Sub ErrHandler: ' 用户按了“取消”按钮 Exit SubEnd SubPrivate Sub Form_Load() Command1.Caption = "打开 Office Word 文档"End SubPrivate Sub Form_Unload(Cancel As Integer) SendMessage WordPadHwnd, WM_CLOSE, 0&, ByVal 0& '关闭写字板程序End Sub
- VB的文本框控件TextBox打开Office的Word文档
- VB文本框textbox的那点事
- VB打开Word文档
- VB判断指定的WORD文档是否已被打开
- 关于在webbrowser控件里面打开word文档的问题
- 在VB.NET窗体中如何打开/查看Office文档(Word, Excel, PowerPoint)
- office2007新建 Microsoft Office Word 文档.docx,打开后弹出转换文件对话框的解决办法
- WPF打开操作Office文档的解决办法
- dropdownlist textbox 结合,下拉框和文本框结合的控件
- TextBox服务器控件怎么操作,文本框的多行换行
- aspose.words生成的word文档中插入表格,用Microsoft office的word打开,表格显示正常,用wps打开,表格挤压成粗线条
- vb.net下打开、创建Word文档
- vb.net 2005 打开WORD文档
- 打开包含图片或对象的 Microsoft Office Word 2007 文档时,图片或对象不显示。
- 文本框TextBox类的应用
- VB.net关于Textbox文本控件全选的小技巧
- C#的office word文档的操作(1)
- WORD文档不能打开的一些原因
- 域用户登录过程
- Linux使用笔记: 使用Samba访问windows的共享目录
- 【山东】关于2012年下半年计算机技术与软件专业技术资格(水平)考试有关问题的通知
- 邂逅在华灯初上
- [转载]Android 高级绘图 paint
- VB的文本框控件TextBox打开Office的Word文档
- 构建嵌入式linux根文件系统
- 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果
- Lucene评分算法--简单的说明
- 我的emacs配置
- ROI选择小工具开发
- 基于java的串口通讯(附带实例+说明文档+测试工具)
- Android模拟器adb命令介紹
- asp.net gridview导出到Excel中。