Solved Write to a file (1 Viewer)

InFlight

User
Local time
Tomorrow, 04:13
Joined
Jun 11, 2015
Messages
130
I am trying to do a test file
It works when i leave some code out but if i add
& " - " & vFolder.GetDetailsOf(vFolderItem, i) & " -") in line 105
i get a this error on line 114
1635331599196.png


Code:
Sub vName()
    Call fProperties("H:\My Music\Music Finished\Bob Lind", "Bob Lind - 09 - Elusive Butterfly.mp3")
End Sub

Function fProperties(vPath As String, vFile As String)
          'vPath is the path of the folder where the MP3 file is located
          'vFile is the name of the mp3 file with the extension
          Dim vShell As New Shell32.Shell
          Dim vFolder As Shell32.Folder
          Dim vFolderItem As Shell32.FolderItem
        
          Dim aPropName() As String
          Dim s As String
          Dim i As Integer
        
100       Set vShell = CreateObject("Shell.Application")
101       Set vFolder = vShell.Namespace(vPath)
102       Set vFolderItem = vFolder.Items.Item(vFile)
        
103       For i = 0 To 296
104           ReDim Preserve aPropName(i)
105           aPropName(i) = Format(i, "000 : ") & vFolder.GetDetailsOf(Null, i) & " - " & vFolder.GetDetailsOf(vFolderItem, i) & " -"
106           If Len(vFolder.GetDetailsOf(Null, i)) = 0 Then
107               ReDim Preserve aPropName(i - 1)
                  ' Exit For
108           Else
109               Debug.Print aPropName(i)

110           End If
111       Next
        
          ' Create ouput file
          Dim Fso, MyFile
112       Set Fso = CreateObject("Scripting.FileSystemObject")
113       Set MyFile = Fso.CreateTextFile(vPath & "\Song List Details - " & vFile & ".txt", True)
114       MyFile.Write Join(aPropName, Chr(13))
        
115       MyFile.Close
116       Set Fso = Nothing
117       Set MyFile = Nothing
118       fProperties = aPropName

119       Set vShell = Nothing
120       Set vFolder = Nothing
121       Set vFolderItem = Nothing
End Function
 
Last edited:

Ranman256

Well-known member
Local time
Today, 11:13
Joined
Apr 9, 2015
Messages
4,339
vFile = "c:\temp\MyTextFile.txt"

open vFile for output as #1
print #1, "this is my text"
close 1
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:13
Joined
May 7, 2009
Messages
19,175
here is a modified code:
Code:
Sub vName()
    Call fProperties("C:\Users\arnel puzon\Desktop\downloads\Simon & Garfunkel - The Essential (2003) [FLAC] vtwin88cube\MP3\", "03.-The Sound Of Silence.mp3")
End Sub

Function fProperties(vPath As String, vFile As String)
          'vPath vFolderresents the path of the folder where the MP3 file is located
          'vFile vFolderresents the name of the mp3 file with the extension    Dim vShell As New Shell32.Shell
          Dim vShell As New Shell
          Dim vFolder As Shell32.Folder
          Dim vFolderItem As Shell32.FolderItem
          
          Dim aPropName() As String
          Dim s As String
          Dim i As Integer
          
'100       Set vShell = CreateObject("Shell.Application")
101       Set vFolder = vShell.Namespace(vPath)
102       Set vFolderItem = vFolder.Items.Item(vFile)
          
103       For i = 0 To 296
104           ReDim Preserve aPropName(i)
105           aPropName(i) = Format(i, "000 : ") & vFolder.GetDetailsOf(Null, i) & " - " & vFolder.GetDetailsOf(vFolderItem, i) & " -"
106           If Len(vFolder.GetDetailsOf(Null, i)) = 0 Then
107               ReDim Preserve aPropName(i - 1)
                  ' Exit For
108           Else
109               Debug.Print aPropName(i)

110           End If
111       Next
          Debug.Print UBound(aPropName)
          ' Create ouput file
          Dim Fso, MyFile
112       Set Fso = CreateObject("Scripting.FileSystemObject")
113       Set MyFile = Fso.CreateTextFile(vPath & "\Song List Details - " & vFile & ".txt", True)
115       MyFile.Close
          'arnelgp
          Call WriteToText(vPath & "\Song List Details - " & vFile & ".txt", Join(aPropName, Chr(13)))
116       Set Fso = Nothing
117       Set MyFile = Nothing
118       fProperties = aPropName

119       Set vShell = Nothing
120       Set vFolder = Nothing
121       Set vFolderItem = Nothing
End Function

'arnelgp
Public Sub WriteToText(ByVal spath As String, ByVal the_content As String)
Open spath For Output As #1
Print #1, the_content
Close #1
End Sub
 

InFlight

User
Local time
Tomorrow, 04:13
Joined
Jun 11, 2015
Messages
130
here is a modified code:
Code:
Sub vName()
    Call fProperties("C:\Users\arnel puzon\Desktop\downloads\Simon & Garfunkel - The Essential (2003) [FLAC] vtwin88cube\MP3\", "03.-The Sound Of Silence.mp3")
End Sub

Function fProperties(vPath As String, vFile As String)
          'vPath vFolderresents the path of the folder where the MP3 file is located
          'vFile vFolderresents the name of the mp3 file with the extension    Dim vShell As New Shell32.Shell
          Dim vShell As New Shell
          Dim vFolder As Shell32.Folder
          Dim vFolderItem As Shell32.FolderItem
         
          Dim aPropName() As String
          Dim s As String
          Dim i As Integer
         
'100       Set vShell = CreateObject("Shell.Application")
101       Set vFolder = vShell.Namespace(vPath)
102       Set vFolderItem = vFolder.Items.Item(vFile)
         
103       For i = 0 To 296
104           ReDim Preserve aPropName(i)
105           aPropName(i) = Format(i, "000 : ") & vFolder.GetDetailsOf(Null, i) & " - " & vFolder.GetDetailsOf(vFolderItem, i) & " -"
106           If Len(vFolder.GetDetailsOf(Null, i)) = 0 Then
107               ReDim Preserve aPropName(i - 1)
                  ' Exit For
108           Else
109               Debug.Print aPropName(i)

110           End If
111       Next
          Debug.Print UBound(aPropName)
          ' Create ouput file
          Dim Fso, MyFile
112       Set Fso = CreateObject("Scripting.FileSystemObject")
113       Set MyFile = Fso.CreateTextFile(vPath & "\Song List Details - " & vFile & ".txt", True)
115       MyFile.Close
          'arnelgp
          Call WriteToText(vPath & "\Song List Details - " & vFile & ".txt", Join(aPropName, Chr(13)))
116       Set Fso = Nothing
117       Set MyFile = Nothing
118       fProperties = aPropName

119       Set vShell = Nothing
120       Set vFolder = Nothing
121       Set vFolderItem = Nothing
End Function

'arnelgp
Public Sub WriteToText(ByVal spath As String, ByVal the_content As String)
Open spath For Output As #1
Print #1, the_content
Close #1
End Sub
Thanks. I will use this.
Do you know why it is getting the error
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 15:13
Joined
Sep 12, 2006
Messages
15,614
I don't know what shell32 does, but maybe the "null" argument isn't valid.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:13
Joined
May 7, 2009
Messages
19,175
here is another version:
Code:
Sub vName()
    Call fProperties("H:\My Music\Music Finished\Bob Lind", "Bob Lind - 09 - Elusive Butterfly.mp3")
End Sub

Function fProperties(vPath As String, vFile As String)
          'vPath is the path of the folder where the MP3 file is located
          'vFile is the name of the mp3 file with the extension
          Dim vShell As New Shell32.Shell
          Dim vFolder As Shell32.Folder
          Dim vFolderItem As Shell32.FolderItem
        
          Dim aPropName() As String
          Dim s As String
          Dim i As Integer
        
100       Set vShell = CreateObject("Shell.Application")
101       Set vFolder = vShell.Namespace(vPath)
102       Set vFolderItem = vFolder.Items.Item(vFile)
        
103       For i = 0 To 296
104           ReDim Preserve aPropName(i)
105           aPropName(i) = Format(i, "000 : ") & vFolder.GetDetailsOf(Null, i) & " - " & vFolder.GetDetailsOf(vFolderItem, i) & " -"
106           If Len(vFolder.GetDetailsOf(Null, i)) = 0 Then
107               ReDim Preserve aPropName(i - 1)
                  ' Exit For
108           Else
109               Debug.Print aPropName(i)

110           End If
111       Next
        
          ' Create ouput file
          Dim Fso, MyFile
112       Set Fso = CreateObject("Scripting.FileSystemObject")
113       Set MyFile = Fso.CreateTextFile(vPath & "\Song List Details - " & vFile & ".txt", 2, True)
114       MyFile.Write Join(aPropName, Chr(13))
        
115       MyFile.Close
116       Set Fso = Nothing
117       Set MyFile = Nothing
118       fProperties = aPropName

119       Set vShell = Nothing
120       Set vFolder = Nothing
121       Set vFolderItem = Nothing
End Function
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 15:13
Joined
Sep 12, 2006
Messages
15,614
vFile = "c:\temp\MyTextFile.txt"

open vFile for output as #1
print #1, "this is my text"
close 1

to be honest, this is what I do as well. A slight modification to get an unused file reference with freefile, so you don't have to keep track of any concurrent open files. Note that you can open the file with other modes. Not just output, which overwrites any existing file. You can use extend to add to it. There's no need to use anything other than vba.

Code:
dim fno as long

fno = freefile
open vFile for output as #fno
  print #fno, "this is my text"
close fno
 

Users who are viewing this thread

Top Bottom