Check if file is open/inuse when saving as PDF using iLogic

Check if file is open/inuse when saving as PDF using iLogic

fsdolphin
Collaborator Collaborator
2,603 Views
4 Replies
Message 1 of 5

Check if file is open/inuse when saving as PDF using iLogic

fsdolphin
Collaborator
Collaborator

Hi,
 
I'm using a slightly modified version of the code below to save my drawings as PDF and everything is working fine except when the file is opened I get an error so, I would like to be able to check if the file is opened to let the user know.
 
How can I check if the file I'm saving as PDF is opened?
 

ERROR

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

CODE:

Public Sub PublishPDF()
    ' Get the PDF translator Add-In.
    Dim PDFAddIn As TranslatorAddIn
    Set PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
 
    'Set a reference to the active document (the document to be published).
    Dim oDocument As Document
    Set oDocument = ThisApplication.ActiveDocument
 
    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = kFileBrowseIOMechanism
 
    ' Create a NameValueMap object
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
 
    ' Create a DataMedium object
    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
 
    ' Check whether the translator has 'SaveCopyAs' options
    If PDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
        ' Options for drawings... '
        oOptions.Value("All_Color_AS_Black") = 0
        oOptions.Value("Sheet_Range") = kPrintAllSheets
 
        'oOptions.Value("Remove_Line_Weights") = 0
        'oOptions.Value("Vector_Resolution") = 400
        'oOptions.Value("Custom_Begin_Sheet") = 2
        'oOptions.Value("Custom_End_Sheet") = 4
    End If
 
    'Set the destination file name
    oDataMedium.FileName = "c:\temp\test.pdf"
 
    'Publish document.
    Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
End Sub

 

Accepted solutions (1)
2,604 Views
4 Replies
Replies (4)
Message 2 of 5

rjay75
Collaborator
Collaborator
Accepted solution

Here's a function that will check if a file is in use.

 

Function IsFileInUse(filePath As String) As Boolean
	IsFileInUse = False
	If System.IO.File.Exists(filePath) Then
		Dim fileInfo As System.IO.FileInfo
		Dim stream As System.IO.FileStream
		
		fileInfo = New System.IO.FileInfo(filePath)
		Try
			stream = fileInfo.Open(System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)
		Catch
			IsFileInUse = True
		Finally
			If stream IsNot Nothing Then
				stream.Close()
			End If
		End Try
	End If
End Function
Message 3 of 5

fsdolphin
Collaborator
Collaborator

It worked, thanks a lot.

 

 

 

Public Sub Main()
 IsFileInUse("c:\some\folder\file.pdf")

End Sub


Function IsFileInUse(filePath As String) As Boolean

    IsFileInUse = False
    
    If System.IO.File.Exists(filePath) Then
        Dim fileInfo As System.IO.FileInfo
        Dim stream As System.IO.FileStream
        
        fileInfo = New System.IO.FileInfo(filePath)
        Try
            stream = fileInfo.Open(System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)
        Catch
            IsFileInUse = True
            MessageBox.Show("File is opened")
        Finally
            If stream IsNot Nothing Then
                MessageBox.Show("Closing")
                stream.Close()
            End If
        End Try
    End If
End Function 

 

 

0 Likes
Message 4 of 5

Anonymous
Not applicable

I have tried this logic on a .txt file

 

But I couldn't get the logic to close the file. It knows if read only permissions are set on the file but doesnt close the text file..

 

Any thoughts?

0 Likes
Message 5 of 5

Anonymous
Not applicable
Ah my bad! I follow the logic! It does work!
0 Likes