Results 1 to 5 of 5

Thread: Macro reads settings from txt, ini file

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52

    Macro reads settings from txt, ini file

    Hi All,
    I have a Excel Macro which has some a form with some checkboxes and Option buttons.

    What i am planning to do is ... design a standalone VB application where I define these

    checkboxes, write these values to a .ini or .txt file and then make the excel macro read

    these values from this file.

    Is it possible to do this.... can someone help me with the logic.. a sample code would be

    greatly appreciated.
    thanks in advance,
    Lonely
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Here's an exaple for using an ini file.

    VB Code:
    1. Option Explicit
    2.  
    3. Const cstrPathIni = "C:\Junk\MyExcelSettings.ini"
    4.  
    5. Private Declare Function WritePrivateProfileString _
    6.     Lib "kernel32" Alias "WritePrivateProfileStringA" _
    7.    (ByVal lpApplicationName As String, _
    8.     ByVal lpKeyName As Any, _
    9.     ByVal lpString As Any, _
    10.     ByVal lpFileName As String) As Long
    11.  
    12. Private Declare Function GetPrivateProfileString _
    13.     Lib "kernel32" Alias "GetPrivateProfileStringA" _
    14.    (ByVal lpApplicationName As String, _
    15.     ByVal lpKeyName As Any, _
    16.     ByVal lpDefault As String, _
    17.     ByVal lpReturnedString As String, _
    18.     ByVal nSize As Long, _
    19.     ByVal lpFileName As String) As Long
    20.  
    21. '**************************************************************************
    22. 'Procedure: ProfileSettingSave
    23. 'Purpose:   Set user profile setting in initialization file.
    24. 'Inputs:    v_strKey:       Key for value in ini file.
    25. '           v_strSetting:   Value to assign to key.
    26. 'Requires:  WritePrivateProfileString |   API
    27. 'Notes:     Name for section is "ExcelData".
    28. '           Path for ini file is cstrPathIni.
    29. '**************************************************************************
    30. Public Sub ProfileSettingSave(ByVal v_strKey As String, ByVal v_strSetting As String)
    31.  
    32.     WritePrivateProfileString "ExcelData", v_strKey, v_strSetting, cstrPathIni
    33.  
    34. End Sub
    35.  
    36. '**************************************************************************
    37. 'Procedure: ProfileSettingGet
    38. 'Purpose:   Get user profile setting from initialization file.
    39. 'Inputs:    v_strKey:       Key for value in ini file.
    40. '           v_strDefault:   Default value for setting (optional).
    41. 'Returns:   Setting if found, Default if not found.
    42. 'Requires:  GetPrivateProfileString |   API
    43. 'Notes:     Name for section is "ExcelData".
    44. '           Path for ini file is cstrPathIni.
    45. '**************************************************************************
    46. Public Function ProfileSettingGet(ByVal v_strKey As String, _
    47.                          Optional ByVal v_strDefault As String = vbNullString) _
    48.                                   As String
    49.  
    50.     Dim strSetting  As String * 255  ' String buffer to accept setting value.
    51.     ProfileSettingGet = Left$(strSetting, _
    52.         GetPrivateProfileString("ExcelData", v_strKey, _
    53.         v_strDefault, strSetting, 255, cstrPathIni))
    54.  
    55. End Function
    56.  
    57. Sub InYourVBApp()
    58.  
    59.     ProfileSettingSave "Check1Name", "First Check Box"
    60.     ProfileSettingSave "Check1Value", 1
    61.  
    62. End Sub
    63.  
    64. Sub InExcel()
    65.  
    66.     Sheets(1).CheckBox1.Caption = ProfileSettingGet("Check1Name", "CheckBox1")
    67.     Sheets(1).CheckBox1.Value = ProfileSettingGet("Check1Value", 0)
    68.  
    69. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52

    thanks for the example

    Hi WorkHorse,
    Thank you so much for the help that you have given me.... I tried using the code for the purpose... and was successful in saving the settings in a ini file. But when i try to load the settings in the Excel macro, it gives mean error.

    Please can you help me in troubleshooting the code. Have attached the whole source code here as a VB Project.
    Thanks again,
    Lonely
    Attached Files Attached Files
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    You just need the ProfileSettingSave & WritePrivateProfileString functions in VB to save the dat. Then in the Excel macro you put the ProfileSettingGet & GetPrivateProfileString to read the data.

    In VB app to write to file:

    VB Code:
    1. Option Explicit
    2.  
    3. Const cstrPathIni = "C:\Junk\MyExcelSettings.ini"
    4.  
    5. Private Declare Function WritePrivateProfileString _
    6.     Lib "kernel32" Alias "WritePrivateProfileStringA" _
    7.    (ByVal lpApplicationName As String, _
    8.     ByVal lpKeyName As Any, _
    9.     ByVal lpString As Any, _
    10.     ByVal lpFileName As String) As Long
    11.  
    12. '**************************************************************************
    13. 'Procedure: ProfileSettingSave
    14. 'Purpose:   Set user profile setting in initialization file.
    15. 'Inputs:    v_strKey:       Key for value in ini file.
    16. '           v_strSetting:   Value to assign to key.
    17. 'Requires:  WritePrivateProfileString |   API
    18. 'Notes:     Name for section is "ExcelData".
    19. '           Path for ini file is cstrPathIni.
    20. '**************************************************************************
    21. Public Sub ProfileSettingSave(ByVal v_strKey As String, ByVal v_strSetting As String)
    22.  
    23.     WritePrivateProfileString "ExcelData", v_strKey, v_strSetting, cstrPathIni
    24.  
    25. End Sub
    26.  
    27. Sub InYourVBApp()
    28.  
    29.     ProfileSettingSave "Check1Name", "First Check Box"
    30.     ProfileSettingSave "Check1Value", 1
    31.  
    32. End Sub

    In Excel VBA module:

    VB Code:
    1. Const cstrPathIni = "C:\Junk\MyExcelSettings.ini"
    2.  
    3. Private Declare Function GetPrivateProfileString _
    4.     Lib "kernel32" Alias "GetPrivateProfileStringA" _
    5.    (ByVal lpApplicationName As String, _
    6.     ByVal lpKeyName As Any, _
    7.     ByVal lpDefault As String, _
    8.     ByVal lpReturnedString As String, _
    9.     ByVal nSize As Long, _
    10.     ByVal lpFileName As String) As Long
    11.  
    12. '**************************************************************************
    13. 'Procedure: ProfileSettingGet
    14. 'Purpose:   Get user profile setting from initialization file.
    15. 'Inputs:    v_strKey:       Key for value in ini file.
    16. '           v_strDefault:   Default value for setting (optional).
    17. 'Returns:   Setting if found, Default if not found.
    18. 'Requires:  GetPrivateProfileString |   API
    19. 'Notes:     Name for section is "ExcelData".
    20. '           Path for ini file is cstrPathIni.
    21. '**************************************************************************
    22. Public Function ProfileSettingGet(ByVal v_strKey As String, _
    23.                          Optional ByVal v_strDefault As String = vbNullString) _
    24.                                   As String
    25.  
    26.     Dim strSetting  As String * 255  ' String buffer to accept setting value.
    27.     ProfileSettingGet = Left$(strSetting, _
    28.         GetPrivateProfileString("ExcelData", v_strKey, _
    29.         v_strDefault, strSetting, 255, cstrPathIni))
    30.  
    31. End Function
    32.  
    33. Sub InExcel()
    34.  
    35.     Sheets(1).CheckBox1.Caption = ProfileSettingGet("Check1Name", "CheckBox1")
    36.     Sheets(1).CheckBox1.Value = ProfileSettingGet("Check1Value", 0)
    37.  
    38. End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52

    thanks workhorse

    Hi Workhorse,
    Sorry for the delay in replying......... Thanks a million for answering my query... It worked wonderfully....
    Lonely
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

Posting Permissions

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



Click Here to Expand Forum to Full Width