Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-27
6
Medium Priority
?
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 33546972
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 2000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

721 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