- 帖子
- 9
- 主題
- 2
- 精華
- 0
- 積分
- 50
- 點名
- 0
- 作業系統
- win7
- 軟體版本
- sp1
- 閱讀權限
- 20
- 註冊時間
- 2015-9-21
- 最後登錄
- 2020-7-23
|
2#
發表於 2017-1-5 19:32
| 只看該作者
本帖最後由 pipi1968 於 2017-1-5 19:33 編輯
已解決了
上網查了好久,終於OK了
提供給需要的人參考- Function IsFileOpen(strFile As String) As Boolean
- Dim iFile As Integer
- Dim iErr As Integer
-
- On Error Resume Next
- iFile = FreeFile()
- Open strFile For Input Lock Read As #iFile '以鎖定方式開啟,開啟指定檔案後直接關閉檔案
- Close iFile
-
- iErr = Err '將錯誤號碼帶入iErr變數中,然後依照數字即可得知檔案狀態
- On Error GoTo 0
- Select Case iErr
- Case 0
- IsFileOpen = False
- Case 70
- IsFileOpen = True
- Case 53
- MsgBox "找不到檔案,將建立空白進度管制表!"
- IsFileOpen = False
- Case 76
- MsgBox "找不到路徑,請再確認!"
- IsFileOpen = False
- End Select
- End Function
- Sub CheckFile()
- Dim strPath As String
- Dim strFile As String
- Dim strWordFile As String
- Dim wordDoc As Object
- Set wordApp = CreateObject("Word.Application")
- Set fs = CreateObject("Scripting.FileSystemObject")
-
- strFile = "北區1.docx"
- strPath = "D:\My Documents\Temp\"
- strWordFile = strPath & strFile
-
-
- If IsFileOpen(strWordFile) Then
- Set wordDoc = GetObject(strWordFile)
- Application.Visible = True
- GoTo 101
- ElseIf fs.FileExists(strWordFile) Then
- Set wordDoc = wordApp.Documents.Open(strWordFile)
- wordApp.Visible = True
- wordDoc.Activate
- GoTo 101
- Else
- FileCopy strPath & "空白案件進度管控表.docx", strWordFile
- Set wordDoc = wordApp.Documents.Open(strWordFile)
- wordApp.Visible = True
- wordDoc.Activate
- GoTo 101
- 'MsgBox ("檔案不存在!")
- 'Exit Sub
- End If
-
- 101:
- With wordDoc.Tables(1)
- .Cell(6, 2) = Replace(.Cell(6, 2), Chr(13), "") & "、這是測試"
-
- '取得word的日(時)數,重新計算總工作時數
- If InStr(.Cell(6, 3), "日") = 0 And InStr(.Cell(6, 3), "小時") = 0 Then
- WorkHours = 0
- GoTo 102
- Else
- xString = .Cell(6, 3)
- xtemp = Split(xString, "日")
- hours = Val(xtemp(0)) * 8
- xtemp = Split(xtemp(1), "小時")
- WorkHours = hours + Val(xtemp(0))
- End If
- 102:
- '加新增時數:預計增加4.5小時
- WorkHours = WorkHours + 4.5
- '計算日數(每8小時為1日)
- days = Int(WorkHours / 8)
- '計算剩餘小時
- hours = WorkHours - days * 8
- '寫回word表格
- .Cell(6, 3) = days & "日" & hours & "小時"
- End With
- 'wordDoc.Close '關閉該Word文件檔
- 'wordApp.Quit '結束Word應用程式
- 'Set wordDoc = Nothing '釋放物件變數wordDoc
- 'Set wordApp = Nothing '釋放物件變數wordApp
- End Sub
複製代碼 |
|