Solved

How do I add Attribute (Title, Subject, Author) to text file using VBA

Posted on 2010-08-27
6
1,016 Views
Last Modified: 2012-05-10
I am creating a CSV file from some data in a spreadsheet using VBA. I have strict requirements about the layout and content of the file. However I need the file to hold just a bit of extra information.

In Windows Explorer I can open properties page of the CSV file and change Title, Subject and Author. If I could add an account number on the Subject line, that would be really helpful. How can I do that from VBA? I assume I will need a Windows API call. Could you help me with that?


Sub InsertAccountNum(Acct As String)

    Dim FileName As String

    FileName = "C:\Temp\Data.csv"

    

    'insert account number as subject attribute 



End Sub

Open in new window

0
Comment
Question by:TadSter
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:calacuccia
ID: 33544800
Haven't been through all the details, and did not see anything about a Subject Line ... but this might interest you if unknown

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/A_3509.html
0
 
LVL 2

Author Comment

by:TadSter
ID: 33544946
Thanks, but it doesn't address my issue. I need to change the attributes of the file.
0
 
LVL 17

Accepted Solution

by:
calacuccia earned 500 total points
ID: 33546972
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:calacuccia
ID: 33547025
The DSOFile library from MIscrosoft allows to do what you want but only for Office files.

http://support.microsoft.com/kb/q224351/

Sample code

Sub pp()
Dim FileName As String
Dim DSO As DSOFile.OleDocumentProperties
Set DSO = New DSOFile.OleDocumentProperties
'FileName = "book2.xls"
FileName = "picture 096.jpg"
DSO.Open sfilename:=FileName
Debug.Print DSO.SummaryProperties.ApplicationName
Debug.Print DSO.SummaryProperties.Author
Debug.Print DSO.SummaryProperties.Comments
Debug.Print DSO.SummaryProperties.Subject
DSO.SummaryProperties.Comments = "my new test!!!!"   '***
Debug.Print DSO.SummaryProperties.Comments
DSO.Save
DSO.Close
End Sub

It won't work for non-office files though...
0
 
LVL 17

Assisted Solution

by:calacuccia
calacuccia earned 500 total points
ID: 33547112
You can GET (but not SET) the file attributes through this code (need to add a reference to the 'Microsoft Shell Controls and Automation" Library


Option Explicit

Type FileAttributes
    Name As String
    Size As String
    FileType As String
    DateModified As Date
    DateCreated As Date
    DateAccessed As Date
    Attributes As String
    Status As String
    Owner As String
    Author As String
    Title As String
    Subject As String
    Category As String
    Comments As String
    Keywords As String
End Type

Sub test()
Dim mFile As String
mFile = "C:\Documents and Settings\Administrator\My Documents\Personal\Question.txt"
GetFileAttributes (mFile)
End Sub

Public Function GetFileAttributes(strFilePath As String) As FileAttributes
' Shell32 objects
Dim objShell As Shell32.Shell
Dim objFolder As Shell32.Folder
Dim objFolderItem As Shell32.FolderItem

' Other objects
Dim strPath As String
Dim strFileName As String
Dim i As Integer
   
    ' If the file does not exist then quit out
    If Dir(strFilePath) = "" Then Exit Function
   
    ' Parse the file name out from the folder path
    strFileName = strFilePath
    i = 1
    Do Until i = 0
        i = InStr(1, strFileName, "\", vbBinaryCompare)
        strFileName = Mid(strFileName, i + 1)
    Loop
    strPath = Left(strFilePath, Len(strFilePath) - Len(strFileName) - 1)
   
    ' Set up the shell32 Shell object
    Set objShell = New Shell
   
    ' Set the shell32 folder object
    Set objFolder = objShell.Namespace(strPath)
   
    ' If we can find the folder then ...
    If (Not objFolder Is Nothing) Then
       
        ' Set the shell32 file object
        Set objFolderItem = objFolder.ParseName(strFileName)
       
        ' If we can find the file then get the file attributes
        If (Not objFolderItem Is Nothing) Then
           
            GetFileAttributes.Name = objFolder.GetDetailsOf(objFolderItem, 0)
            GetFileAttributes.Size = objFolder.GetDetailsOf(objFolderItem, 1)
            GetFileAttributes.FileType = objFolder.GetDetailsOf(objFolderItem, 2)
            GetFileAttributes.DateModified = CDate(objFolder.GetDetailsOf(objFolderItem, 3))
            GetFileAttributes.DateCreated = CDate(objFolder.GetDetailsOf(objFolderItem, 4))
            GetFileAttributes.DateAccessed = CDate(objFolder.GetDetailsOf(objFolderItem, 5))
            GetFileAttributes.Attributes = objFolder.GetDetailsOf(objFolderItem, 6)
            GetFileAttributes.Status = objFolder.GetDetailsOf(objFolderItem, 7)
            GetFileAttributes.Owner = objFolder.GetDetailsOf(objFolderItem, 8)
            GetFileAttributes.Author = objFolder.GetDetailsOf(objFolderItem, 9)
            GetFileAttributes.Title = objFolder.GetDetailsOf(objFolderItem, 10)
            GetFileAttributes.Subject = objFolder.GetDetailsOf(objFolderItem, 11)
            GetFileAttributes.Category = objFolder.GetDetailsOf(objFolderItem, 12)
            GetFileAttributes.Comments = objFolder.GetDetailsOf(objFolderItem, 14)
            GetFileAttributes.Keywords = objFolder.GetDetailsOf(objFolderItem, 40)

        End If
       
        Set objFolderItem = Nothing
       
    End If
   
    Set objFolder = Nothing
    Set objShell = Nothing

End Function
0
 
LVL 2

Author Closing Comment

by:TadSter
ID: 33558922
Wow! Ouch! That's big. Thanks for pointing me in the right direction. Thanks for the code, it's great.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now