你可以試試下面
Sub save_tracking()
Dim XLapp As Excel.Application
Dim xlWB As Excel.Workbook
Set XLapp = New Excel.Application
'turn off extra bits
Screen_ = XLapp.ScreenUpdating
XLapp.ScreenUpdating = False
Event_ = XLapp.EnableEvents
XLapp.EnableEvents = False
Alerts_ = XLapp.DisplayAlerts
XLapp.DisplayAlerts = False
'get username
un = Environ("username")
'open tracking workbook
Set xlWB = XLapp.Workbooks.Open("C:\Test Tacking.xlsx", False, False)
'save information
With xlWB.Sheets(1)
If .Range("A2").Value = "" Then
'no values yet
.Range("A2").Value = un
.Range("B2").Value = XLapp.Name
.Range("C2").Value = Now()
ElseIf .Range("A3").Value = "" Then
'2nd
.Range("A3").Value = un
.Range("B3").Value = XLapp.Name
.Range("C3").Value = Now()
Else
'>2 values
.Range("A2").End(xlDown).Offset(1, 0).Value = un
.Range("B2").End(xlDown).Offset(1, 0).Value = XLapp.Name
.Range("C2").End(xlDown).Offset(1, 0).Value = Now()
End If
End With
'restore settings to previous
XLapp.ScreenUpdating = Screen_
XLapp.EnableEvents = Event_
XLapp.DisplayAlerts = Alerts_
'save/close workbook
xlWB.Close True
XLapp.Quit
Set XLapp = Nothing
End Sub
Function Environ(Expression)
On Error GoTo Err_Environ
Environ = VBA.Environ(Expression)
Exit_Environ:
Exit Function
Err_Environ:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_Environ
End Function
變化xlWB到您的跟蹤表存儲在文件名。在追蹤表A1/B1/C1中預留了一個標題,信息將存儲在第一張表中。
編輯:修改爲從其他辦公室程序運行。您需要對「Microsoft Excel 12.0 Object Library」的引用,版本可能不同,但應該沒問題。工具 - >參考。讓你參考
感謝這一點,我認爲這是非常接近我後。我在Dim xlwb上看到'用戶定義類型未定義'錯誤,但是作爲Workbook行 - 任何想法會導致什麼? – monkeyb33f
你在Excel中運行這個嗎?或另一個程序? – 99moorem
從Word文檔裏面 – monkeyb33f