有网友问我,VB的文本框控件TextBox能否打开Office的Word文档。VB的TextBox是Ansi版本的,通过open语句打开载入到TextBox中,肯定是乱码,没啥说的。但是,我们可以通过间接的方式打开Office的Word文档,通过一系列的API调用,将文档转换到TextBox中,而且保留了Word的版式(除字体变化以外)。请看下面程序:
标准模块:
'标准模块 Option Explicit Public 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 Long Public 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 Long Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long,ByVal lpEnumFunc As Long,ByVal lParam As Long) As Long Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long,ByVal lParam As Long) As Boolean Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long,ByVal lpString As String,ByVal cch As Long) As Long Private 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,OfficeWordFileName) > 0 Then EnumChildWindows hWnd,AddressOf EnumChildWindowsProc,ByVal 0& WordPadHwnd = hWnd '保存写字板句柄 Debug.Print WordPadHwnd EnumWindowsProc = False End If EnumWindowsProc = True End Function Public 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,"RICHEDIT50W") > 0 Then txtLineCount = SendMessage(hWnd,EM_GETLINECOUNT,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 = True End Function
Form1窗体模块:
'窗体上放1个文本框控件Text1、1个命令按钮Command1、1个通用对话框控件CommonDialog1 Option Explicit Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long,ByVal bInheritHandle As Long,ByVal dwProcessId As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long,ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Const SYNCHRONIZE As Long = &H100000 Private 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,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 Sub End Sub Private Sub Form_Load() Command1.Caption = "打开 Office Word 文档" End Sub Private Sub Form_Unload(Cancel As Integer) SendMessage WordPadHwnd,WM_CLOSE,0&,ByVal 0& '关闭写字板程序 End Sub