Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations JAE on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Files as Read Only

Status
Not open for further replies.

jzecha

Aerospace
Joined
Jan 20, 2016
Messages
236
Location
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?
 
How do I do that in with VBA code?
 
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.

Part and Inventory Search

Sponsor

Back
Top