7,762
社区成员
发帖
与我相关
我的任务
分享
'试试下面代码:
Option Explicit
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Dim hIE As Long
Private Sub Form_Load()
Shell ("Explorer c:\Book1.xls"), vbMaximizedFocus
Timer1.Enabled = True
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
hIE = FindWindow(vbNullString, "Book1.xls")
If hIE <> 0 Then SetParent hIE, Me.hWnd: Timer1.Enabled = False
End Sub
Private Sub Command1_Click() '''打开WORD
Dim wdapp As Object
Dim wddoc As Object
Dim wdtab As Object
Set wdapp = CreateObject("Word.Application")
Set wddoc = wdapp.Documents.Open("c:\1.doc") '打开WORD
wddoc.Save '保存
wddoc.Close '关闭文档
wdapp.Quit '关闭WORD
Set wddoc = Nothing
Set wdapp = Nothing
End Sub
'------------打开EXCEL
Private Sub Command1_Click()
Set ex = CreateObject("Excel.Application")
Set wb = ex.Workbooks.Add
'Set wb = ex.Workbooks.Open("c:\1.xls") '打开你的EXCEL文件
Set sh = wb.Sheets(1) '指定工作表sheets1
sh.Cells(1, 1) = "A" '写入A到第一行第一列 返之写出
sh.Cells(1, 2) = "B"
sh.Cells(2, 1) = "C"
sh.Cells(50, 2) = "D"
wb.Close SaveChanges:=True '关闭文件后,直接保存不提问
ex.Quit
Set ex = Nothing
Set wb = Nothing
Set sh = Nothing
MsgBox "数据已写入"
'ex.Visible = True
End Sub