Solved

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

Posted on 2010-08-27
6
1,039 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
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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 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