Consulting

Results 1 to 5 of 5

Thread: Solved: Automation of excel in word - making file hidden

  1. #1
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location

    Solved: Automation of excel in word - making file hidden

    MY only question is when saving the file is how do I set the files attributes (I need to make it hidden).

    this is the code I use to create the excel spreadsheet.

    [vba]
    'sdir is a directory created in code.
    'filepathforxl and excelfilename are constants

    Public Const FIELDNAME_1 As String = "Correspondence"
    Public Const FIELDNAME_2 As String = "Title"
    Public Const FIELDNAME_3 As String = "First Name"
    Public Const FIELDNAME_4 As String = "Surname"
    Public Const FIELDNAME_5 As String = "Company Name"
    Public Const FIELDNAME_6 As String = "Address"
    Public Const FIELDNAME_7 As String = "Town"
    Public Const FIELDNAME_8 As String = "Country"
    Public Const FIELDNAME_9 As String = "Postcode"
    Public Const FIELDNAME_10 As String = "DX Number"
    Public Const FIELDNAME_11 As String = "DX Exchange"
    Public Const FIELDNAME_12 As String = "Fax Number"
    Public Const FIELDNAME_13 As String = "E-Mail"
    Public Const FIELDNAME_14 As String = "Reference"
    Public Const FIELDNAME_15 As String = "Heading"

    Private Sub createExcelFileForHeading(sDir As String)
    Dim ObjExcel As Excel.Application
    Dim Wkb As Excel.Workbook
    Dim WS As Excel.Worksheet

    Set ObjExcel = New Excel.Application
    Set Wkb = ObjExcel.Workbooks.Add
    Set WS = Wkb.Sheets("Sheet1")
    WS.Range("A1").Value = FIELDNAME_1
    WS.Range("B1").Value = FIELDNAME_2
    WS.Range("C1").Value = FIELDNAME_3
    WS.Range("D1").Value = FIELDNAME_4
    WS.Range("E1").Value = FIELDNAME_5
    WS.Range("F1").Value = FIELDNAME_6
    WS.Range("G1").Value = FIELDNAME_7
    WS.Range("H1").Value = FIELDNAME_8
    WS.Range("I1").Value = FIELDNAME_9
    WS.Range("J1").Value = FIELDNAME_10
    WS.Range("K1").Value = FIELDNAME_11
    WS.Range("L1").Value = FIELDNAME_12
    WS.Range("M1").Value = FIELDNAME_13
    WS.Range("N1").Value = FIELDNAME_14
    WS.Range("O1").Value = FIELDNAME_15
    Wkb.SaveAs FileName:=FilePathForXL + sDir + ExcelFilename
    Wkb.Close Savechanges:=True
    Set ObjExcel = Nothing
    Set Wkb = Nothing
    Set WS = Nothing
    End Sub[/vba]
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi sadam,

    The code below will set the hidden bit. I am afraid it will only change the file if it is in the current directory, I didn't investigate further.
    [VBA]
    Sub SetHiddenBit(filespec)
    Dim fs As FileSystemObject
    Dim f As File
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(fs.GetFileName(filespec))
    f.Attributes = 2
    Set f = Nothing
    Set fs = Nothing
    End Sub
    Sub SendFile()
    Dim FilNm As String
    FilNm = "usat1.dwg"
    SetHiddenBit FilNm
    End Sub
    [/VBA]

  3. #3
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Ok I investigated further this one works across paths harddrives etc. If you have the rights you can do it
    [VBA]
    Sub SetHiddenBit(filespec As String, PathToFile As String)
    Dim fs As FileSystemObject
    Dim f As File
    Dim p As Folder
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set p = fs.GetFolder(PathToFile)
    Set f = p.Files(filespec)
    f.Attributes = 2
    Set f = Nothing
    Set p = Nothing
    Set fs = Nothing
    End Sub
    Sub SendFile()
    Dim FilNm As String
    FilNm = "usat1.dwg"
    SetHiddenBit FilNm, "c:\acad\"
    End Sub
    [/VBA]

  4. #4
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    Thanks Tommy - works an absolute treat and integrates in to my code so easily.

    This one is solved
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  5. #5
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Glad I could help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •