Solved

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

Posted on 2010-08-27
6
1,096 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 500 total points
ID: 33546972
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

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.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

627 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