返回列表 上一主題 發帖

[發問] 違反共用原則

[發問] 違反共用原則

請問論壇的大大們
"違反共用原則"是甚麼意思


加入底下的語法就會出現"違反共用原則"
Private Sub Workbook_Open()
Application.OnTime TimeValue("17:00:00"), "full_calc"
End Sub

語法的來源
https://learn.microsoft.com/zh-tw/office/vba/api/excel.application.ontime

回復 1# cowww

Function IsFileOpen(filePath As String) As Boolean
    Dim fileNum As Integer
    fileNum = FreeFile()

    On Error Resume Next
    Open filePath For Binary Access Read Write Lock Read Write As fileNum
    If Err.Number <> 0 Then
        IsFileOpen = True
    End If
    Close fileNum
    On Error GoTo 0
End Function

Dim targetFilePath As String
    targetFilePath = "\\shl-group.com\dept\MFMG\對外單位開放資料\會議室模具追蹤資訊\急件專案狀態追蹤_v2_1.xlsm"
   
If IsFileOpen(targetFilePath) Then
    ' 檔案已被開啟,執行另存新檔的動作
    Dim currentDate As String
    currentDate = Format(Date, "yyyymmdd") ' 取得當天日期的字串表示,例如:20230522
   
    Dim newFileName As String
    newFileName = "\\shl-group.com\dept\MFMG\對外單位開放資料\會議室模具追蹤資訊\急件專案狀態追蹤_v2_1_" & currentDate & ".xlsm"
   
    ThisWorkbook.SaveAs filename:=newFileName, WriteResPassword:="6112", ReadOnlyRecommended:=True

Else
    ThisWorkbook.SaveAs filename:=targetFilePath, WriteResPassword:="6112", ReadOnlyRecommended:=True

End If
============================================================================================
Private Sub Workbook_Open()

'指定07:45開始執行"full_calc"
    Application.OnTime TimeValue("17:00:00"), "full_calc"
   
End Sub

測試發現上述兩段語法好像會造成"違反共用原則"的異常出現

請問這個問題有辦法解決嗎??

TOP

回復 3# singo1232001

非常感謝singo1232001大大的解惑

還是會出現"違反共用原則"的錯誤訊息

TOP

回復 5# singo1232001

我也是覺得寫在open底下比較好
但是我在公司電腦的權限只有使用者
沒辦法做Windows工作排程器

所以才會想到用ontime

TOP

回復 7# singo1232001


非常感謝singo1232001大大的解惑
下面的寫法並沒有出現"違反共用原則"
Sub 專案_按鈕22_Click()
Call full_calc
End Sub

TOP

回復 8# goner

非常感謝goner大大的解惑

我是另存一個檔案做修改
也很確定檔案並沒有開起共用

TOP

非常感謝singo1232001大大的解惑
非常感謝goner大大的解惑

將語法改成這樣就沒有出現"違反共用原則"的錯誤訊息了
Function IsFileOpen(filePath As String) As Boolean
    Dim fso As Object
    Dim file As Object
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set file = fso.OpenTextFile(filePath, 1)
    If Err.Number = 0 Then
        IsFileOpen = False
        file.Close
    Else
        IsFileOpen = True
    End If
    On Error GoTo 0
    Set file = Nothing
    Set fso = Nothing
End Function
======================================
Private Sub Workbook_Open()
Application.OnTime TimeValue("17:00:00"), "full_calc"
End Sub

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題