首先引用Microsoft Word Library: Public appYours As Word.Application Public docYours As Word.DocumentSub Yours() Set appYours = New Word.Application Set docYours = appYours.Documents.Open("Your.dos") appYours.Visible = True End Sub调用Yours就可以打开Word了。通过appYours 和docYours 操控
'这样,用shell语句就可以:Option ExplicitConst SYNCHRONIZE = &H100000 Const INFINITE = &HFFFFFFFF Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click() Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变数pId = Shell("start d:\ms\123.doc") ' Shell 传回 Process IdpHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process HandleMsgBox pHnd End Sub
上面的pId = Shell("start d:\ms\123.doc")出错了啊,是不是错了
Option Explicit Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click() Dim id As Long, Handle As Long ShellExecute Me.hwnd, "open", "c:\123.doc", "", "", 0 Handle = FindWindow("OpusApp", "123 - Microsoft Word") ' 获得窗口句柄 End Sub
获得文件句柄要用: Const OFS_MAXPATHNAME = 128 Const OF_CREATE = &H1000 Const OF_READ = &H0 Const OF_WRITE = &H1 Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End Type Private Declare Function CopyLZFile Lib "lz32" (ByVal n1 As Long, ByVal n2 As Long) As Long Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Sub Form_Load() Dim sFile As String, hDecomp As Long, hResult As Long Dim OF As OFSTRUCT, sSaveTo As String 'Ask for a file to decompress sFile = InputBox("Please, enter a file to decompress.") 'Ask for a file to decompress it to sSaveTo = InputBox("Please, enter a filename to decompress it to.") 'Open the two files hDecomp = OpenFile(sFile, OF, OF_READ) hResult = OpenFile(sSaveTo, OF, OF_WRITE Or OF_CREATE) 'decompress the file CopyLZFile hDecomp, hResult 'Close the two files CloseHandle hDecomp CloseHandle hResult Unload Me End Sub
下面是一个简单的例子: Const OFS_MAXPATHNAME = 128 Const OF_CREATE = &H1000 Const OF_READ = &H0 Const OF_WRITE = &H1 Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End Type Private Declare Function CopyLZFile Lib "lz32" (ByVal n1 As Long, ByVal n2 As Long) As Long Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click() Dim sFile As String, hDecomp As Long Dim OF As OFSTRUCT sFile = "d:\1.doc" '打开文件,获得文件句柄 hDecomp = OpenFile(sFile, OF, OF_READ) '用完之后,要用CloseHandle关闭 CloseHandle hDecomp End Sub
Public appYours As Word.Application
Public docYours As Word.DocumentSub Yours()
Set appYours = New Word.Application
Set docYours = appYours.Documents.Open("Your.dos")
appYours.Visible = True
End Sub调用Yours就可以打开Word了。通过appYours 和docYours 操控
Const INFINITE = &HFFFFFFFF
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click()
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变数pId = Shell("start d:\ms\123.doc") ' Shell 传回 Process IdpHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process HandleMsgBox pHnd
End Sub
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Sub Command1_Click()
Dim id As Long, Handle As Long
ShellExecute Me.hwnd, "open", "c:\123.doc", "", "", 0
Handle = FindWindow("OpusApp", "123 - Microsoft Word") ' 获得窗口句柄
End Sub
??
兄弟,什么系统下,另外,要确定目录和文件存在,如不存在,可以新建目录和文件
你的代码获得的是窗口句柄,不是文件句柄。
你的代码是获得进程句柄吧,现在就差文件句柄了,呵呵。我也不太清楚应该怎样获得文件句柄,关注!!!
Const OFS_MAXPATHNAME = 128
Const OF_CREATE = &H1000
Const OF_READ = &H0
Const OF_WRITE = &H1
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function CopyLZFile Lib "lz32" (ByVal n1 As Long, ByVal n2 As Long) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Form_Load()
Dim sFile As String, hDecomp As Long, hResult As Long
Dim OF As OFSTRUCT, sSaveTo As String
'Ask for a file to decompress
sFile = InputBox("Please, enter a file to decompress.")
'Ask for a file to decompress it to
sSaveTo = InputBox("Please, enter a filename to decompress it to.")
'Open the two files
hDecomp = OpenFile(sFile, OF, OF_READ)
hResult = OpenFile(sSaveTo, OF, OF_WRITE Or OF_CREATE)
'decompress the file
CopyLZFile hDecomp, hResult
'Close the two files
CloseHandle hDecomp
CloseHandle hResult
Unload Me
End Sub
Const OFS_MAXPATHNAME = 128
Const OF_CREATE = &H1000
Const OF_READ = &H0
Const OF_WRITE = &H1
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function CopyLZFile Lib "lz32" (ByVal n1 As Long, ByVal n2 As Long) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click()
Dim sFile As String, hDecomp As Long
Dim OF As OFSTRUCT
sFile = "d:\1.doc"
'打开文件,获得文件句柄
hDecomp = OpenFile(sFile, OF, OF_READ)
'用完之后,要用CloseHandle关闭
CloseHandle hDecomp
End Sub