Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FileSystemObject (VB6)

    I've been asked to modify my application (Be-Grudgedly) to insert a comment with an image file.
    I've looked into this and feel the best way to go is with "FileSystemObject"

    I've modified a click event with the image to firstly check if a text file exists with the same name as the image:
    <pre>Private Function imgclick(i As Integer)
    Dim fso As New FileSystemObject
    Dim intIndex As Integer
    Dim strFile As String
    Dim j As Integer
    For j = 1 To intNumberOfImages
    If j = i Then
    Me.PreviewImg.Picture = Me.Controls("img" & j).Picture

    Else
    End If
    Next j
    '===================
    If fso.FileExists("L:mmpdfimage" & arrFileNames(i) & ".txt") Then
    'MsgBox "File Exists"
    Exit Function
    Else
    fso.CreateTextFile "L:MMPDFImage" & arrFileNames(i) & ".txt"
    End If
    '================
    End Function</pre>


    What I'm struggling with is the textStream part to open the textfile and edit it.
    I would rather a static text box on the form which will show the text contents from the txt file.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileSystemObject (VB6)

    Dave,

    See Scripting Runtime Library - TextStream Object in the MSDN library for information and example code. The code you would use in VB6 corresponds to the VBScript examples given.

    An alternative would be to maintain a single text file with the comments, or even a small Access database (the users don't need to have Access for this, your VB6 application can read and write Access databases.)

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    Hans
    I thought of using a database, but I'm not sure how to implement it in the system.
    I have a look at the MSDN site and see what I can achieve
    .

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileSystemObject (VB6)

    Yet another possibility: the good old ini file. You could create a text file with contents like this:

    [Images]
    L:mmpdfimage17318-W918MBB_Img_0657.jpg=This is a nice dent
    L:mmpdfimage17318-W918MBB_Img_0658.jpg=Check this out!

    Say that you name it Settings.ini and store it in C:MyApp. Put the following declarations and code in a standard module:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Public Declare Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

    Public Declare Function WritePrivateProfileString Lib "kernel32" _
    Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpString As Any, _
    ByVal lpFileName As String) As Long

    Public Const strIniFile As String = "C:MyAppSettings.ini"

    Public Function GetComment(FileName As String) As String
    Dim strRet As String
    Dim lngLen As Long
    strRet = String(255, 0)
    lngLen = GetPrivateProfileString("Images", FileName, "", strRet, 255, strIniFile)
    GetComment = Left(strRet, lngLen)
    End Function

    Public Sub SetComment(FileName As String, Comment As String)
    WritePrivateProfileString "Images", FileName, Comment, strIniFile
    End Sub

    <img src=/w3timages/blueline.gif width=33% height=2>

    Examples of use:

    GetComment("L:mmpdfimage17318-W918MBB_Img_0657.jpg") will return "This is a nice dent".

    Set a new comment like this:
    SetComment "L:mmpdfimage17318-W918MBB_Img_0699.jpg", "Part will have to be ordered"

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    Hans
    This looks quite complicated.
    I can't see how this would allow a user to attach a comment to an image of their choice and save it for future reference.
    Does it use a inputBox or something ?

    I still like the idea of the txt notepad doc, and was hoping to embed the doc in some kind of control on the form, or even like a sticky note.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileSystemObject (VB6)

    Dave,

    The code between the two blue lines is just technical stuff. You could use an input box to let the user enter a comment, but why not use a text box on the form itself, with a command button to store the comment.

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    Hans
    Each pic has a textbox anyway showing the path and file name.
    To be honest, I dont really need to show the path information at all, if I could use these texts instead and use the original code as the first post above, this would be great.
    It would also control the amount of clutter on screen also.

    Is it possible to insert the comment ib the FileSystemObject ? and show the texts in these controls ?

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileSystemObject (VB6)

    The link to MSDN I posted in my first reply mentions the ReadLine and WriteLine method of the TextStream object. You can use these to retrieve and store text in a text file.

    You probably populate the text boxes in the FillImages procedure. You could use ReadLine to get the comment from the text file corresponding to the image, then put it in the text box instead of the path.

    Similarly, you can use WriteLine to store the comment from the text box into the text file.

  9. #9
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    Thanks Hans
    I'm sure I'm getting closer.
    I'll check MSDN, I'm certain now this is the way to go.

  10. #10
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    Getting there now.
    With this version, a couple of things need to happen.
    1/ Remove the extension .jpg (the file saves as IMG_124.jpg.txt)

    2/ Append to the end of the text instead of over writing

    I've looked at the .WriteBlankLine & .WriteLine method but they aren't very clear.


    <pre>Private Function imgDBLclick(i As Integer)
    Dim s As FileSystemObject
    Dim sl As TextStream
    Dim j As Integer
    For j = 1 To intNumberOfImages
    If j = i Then

    Set s = New FileSystemObject
    s.CreateTextFile ("L:MMPDFImage" & arrFileNames(i) & ".txt")
    s.OpenTextFile "L:MMPDFImage" & arrFileNames(i) & ".txt", ForWriting, True
    Set sl = s.OpenTextFile("L:MMPDFImage" & arrFileNames(i) & ".txt", ForWriting, True)
    sl.WriteLine Me.Text1
    sl.WriteBlankLines 1

    sl.Close
    s.OpenTextFile "L:MMPDFImage" & arrFileNames(i) & ".txt", ForReading, False
    Set sl = s.OpenTextFile("L:MMPDFImage" & arrFileNames(i) & ".txt", ForReading, True)
    MsgBox sl.ReadAll


    Else
    End If
    Next j
    End Function</pre>


  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FileSystemObject (VB6)

    1) is easy: replace

    "L:MMPDFImage" & arrFileNames(i) & ".txt"

    by

    "L:MMPDFImage" & Left(arrFileNames(i), Len(arrFileNames(i) - 4) & ".txt"

    Since you use this several times, it would be more efficient to assign this to a string variable.

    2) Do you really want to keep on adding comments to the same image?

  12. #12
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    "2) Do you really want to keep on adding comments to the same image?"

    Not particularly but some one will, then they'll complain the original comments have gone !

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: FileSystemObject (VB6)

    Does this have to be done for free, or can you license a third party tool? JPG files have room in the header for comments, so it seems natural to just stuff them in there, but you would need some kind of JPG-format-aware control to do that. Of course, if you cannot change the files for legal reasons (preservation of evidence), then this would not be a good approach...

    If you review the FileSystemObject documentation in more detail, you will find that it has a method to return the file name without its extension. HansV's code might be equally (or even more) reliable, but the FSO method probably is more self-documenting for the next person who has to edit this code. (I forget its name... self-documenting <> memorable. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15> )

  14. #14
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: FileSystemObject (VB6)

    Here's a sample of reading and writing with the FileSystemObject. I don't make any claim of efficiency or brilliance for this script. But it did work while we used it.

  15. #15
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: FileSystemObject (VB6)

    Just a thought, but as yo uare using VB you should have access to the RichText control. You could use this in place of a text box and just load the relevant text file into the control. You can edit the RichTextt control text and save it when appropriate.

    Andrew C

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •