不弹出覆盖文件的消息框

问题描述 投票:0回答:1

我编写了以下 VBA 代码来保存我的 Solidworks 文件。它还检查文件夹中是否已存在文件,如果存在 - 通过消息框询问是否需要覆盖这些文件。 这是代码:

Main sub()
PathInit = Part.GetPathName                         'Determine file location of the assembly
PathCut = Left(PathInit, InStrRev(PathInit, "\"))   'Remove text "Assembly.SLDASS" after the last slash
initName = PathCut + ArrayList(i) + ExtInit                                                                                                                                     'Name to open the original file
finalName = PathCut & FolderName & "\" & NumPart(initName) & "_" & mldpartcode & " " & ArrayListAdapted & " " & "[REV" & UserParam.getREV(UserParam, CStr(ArrayListNr(i))) & "]" & ExtNew  'New filename
finalNameCut = NumPart(initName) & "_" & mldpartcode & " " & ArrayListAdapted & " " & "[REV" & UserParam.getREV(UserParam, CStr(ArrayListNr(i))) & "]" & ExtNew

 For i = LBound(ArrayList) To UBound(ArrayList) 'Run loop x times depending on the amount of selected checkboxes in the userform
    'Save the file if it does not exist yet
    Dim FileNameOverwrite
    Dim IsToBeSaved
    IsToBeSaved = True
    
    If Not Dir(finalName, vbDirectory) = vbNullString Then
        FileNameOverwrite = MsgBox("Filename " & finalNameCut & " already exists. Do you want to overwrite?", vbQuestion + vbYesNoCancel, "File overwrite")
        UserParam.Hide
        If FileNameOverwrite = vbNo Then
            UserParam.Show
            IsToBeSaved = False
            'Exit Sub ' Stop the code execution, no more looping
        End If
        If FileNameOverwrite = vbCancel Then
            UserParam.Show
            Exit Sub
        End If
    End If
    
    If IsToBeSaved Then
        swModelToExport.Extension.SaveAs3 finalName, 0, 1, Nothing, Nothing, nErrors, nWarnings
    End If
    
    'Close all the files
    swApp.CloseDoc ArrayList(i) & ".SLDPRT"
     
    'Reopen assembly
    Set swModel = swApp.OpenDoc6(PathInit, 1, 0, "", nStatus, nWarnings)                                                    'Open the model
    Set swModelActivated = swApp.ActivateDoc3(PathInit, False, swRebuildOnActivation_e.swUserDecision, nErrors)             'Activate the model
    Set swModelToExport = swApp.ActiveDoc                                                                                   'Get the activated model
 Next
End Sub

除了覆盖多个文件之外,这效果非常好。 假设我已经保存了两个名为

"Saved1"
"Saved2"
的文件。 现在,我再次保存两个完全相同的文件,并希望代码通过以下弹出窗口询问我是否要覆盖每个文件:

enter image description here

如果我选择“是”选项,则代码可以正常工作,并且会弹出一个类似的消息框,但这次是

Saved2
。 但是,如果我为第一个文件(“Saved1”)选择“否”选项,那么它会返回到我的用户表单,而不是询问我是否要覆盖
Saved2
,即使我不想覆盖
 Saved1

有谁知道如何调整代码,即询问我是否要覆盖第二个文件,即使我不想覆盖第一个文件?

提前谢谢您

vba save messagebox overwrite solidworks
1个回答
0
投票

看起来这个(未经测试)可能更接近你想要的:

Sub Main()
    
    Dim FileNameOverwrite
    
    PathInit = Part.GetPathName                         'Determine file location of the assembly
    PathCut = Left(PathInit, InStrRev(PathInit, "\"))   'Remove text "Assembly.SLDASS" after the last slash

    UserParam.Hide
    
    For i = LBound(ArrayList) To UBound(ArrayList)
 
        initName = PathCut + ArrayList(i) + ExtInit                                                                                                                                     'Name to open the original file
        
        'filename
        finalNameCut = NumPart(initName) & "_" & mldpartcode & " " & _
                    ArrayListAdapted & " " & _
                    "[REV" & UserParam.getREV(UserParam, CStr(ArrayListNr(i))) & "]" & ExtNew
        
        finalName = PathCut & FolderName & "\" & finalNameCut  'full path
        
        'Save the file if it does not exist yet
        If Not Dir(finalName, vbNormal) = vbNullString Then
            
            FileNameOverwrite = MsgBox("Filename " & finalNameCut & _
                          " already exists. Do you want to overwrite?", _
                          vbQuestion + vbYesNoCancel, "File overwrite")
            
            Select Case FileNameOverwrite
                Case vbYes
                    swModelToExport.Extension.SaveAs3 finalName, 0, 1, _
                                  Nothing, Nothing, nErrors, nWarnings
                Case vbNo         'do nothing - just process next file
                Case vbCancel     'user cancelled any other saves
                    UserParam.Show
                    Exit Sub
            End Select
        End If
        swApp.CloseDoc ArrayList(i) & ".SLDPRT" 'Close this file
     Next i
     
    'Reopen assembly
    Set swModel = swApp.OpenDoc6(PathInit, 1, 0, "", nStatus, nWarnings)      'Open the model
    Set swModelActivated = swApp.ActivateDoc3(PathInit, False, _
                            swRebuildOnActivation_e.swUserDecision, nErrors) 'Activate the model
    Set swModelToExport = swApp.ActiveDoc                                    'Get the activated model

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.