標題:
[發問]
excel VBA 如何在word檔開啟狀態下順利寫入word的表格
[打印本頁]
作者:
pipi1968
時間:
2017-1-2 00:40
標題:
excel VBA 如何在word檔開啟狀態下順利寫入word的表格
我想利用excel VBA 作一個表單輸入資料後,要資料寫入word的表格,
我的作法是1.先檢查word檔是否開啟,如果已開啟,去執行101程序
2.如果未開啟,則開啟word檔後去執行101程序
3.如果是新専案,則從資料夾複製空白専案管為新専案進度表後,去執行101程序
目前2. 3.都可以順利執行
但是1.會出現錯誤如下圖,也就是當word檔開啟狀態下,無法正常執行
[attach]26276[/attach]
麻煩各位先進指導一下,如何克服這個問題
謝謝
程式碼如下:
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
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
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
複製代碼
word檔附件
[attach]26277[/attach]
作者:
pipi1968
時間:
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
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)