Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Open Files as Read Only

Status
Not open for further replies.

jzecha

Aerospace
Jan 20, 2016
235
0
0
US
I have a VBA Macro that opens files and then saves them to a different directory.

Is there a way I can open the files as Read Only so that when Catia activates associated links, it doesn't save the file in the initially loaded location before moving it to the new location?
 
Replies continue below

Recommended for you

Hi jzecha.

Try this.
Code:
'vba read only open sample

Option Explicit

Private Const READONLY = 1&

Sub CATMain()

    Dim path As String
    path = "C:\temp\Part1.CATPart"
    
    If Not isExist(path) Then
        Dim msg As String
        msg = "File not found" & vbCrLf & path
        MsgBox msg
        
        Exit Sub
    End If
    
    Dim backupAttr As Long
    backupAttr = getAttributes(path)
    
    If Not (backupAttr And READONLY = READONLY) Then
        Call setAttributes(path, READONLY)
    End If
    
    Call CATIA.Documents.Open(path)
    
    If Not (backupAttr And READONLY = READONLY) Then
        Call setAttributes(path, backupAttr)
    End If
    
End Sub

Private Sub setAttributes( _
    ByVal path As String, _
    ByVal Value As Long)

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    fso.GetFile(path).Attributes = Value
    
End Sub

Private Function getAttributes( _
    ByVal path As String) _
    As Long

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    getAttributes = fso.GetFile(path).Attributes

End Function

Private Function isExist( _
    ByVal path As String) _
    As Boolean

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    isExist = fso.FileExists(path)

End Function
 
Status
Not open for further replies.
Back
Top