Excel 2007 Macro, VBAProject can put a configuration file?

First off, I'm new to Excel Macro. I read some articles and did some work these days. But now I so doubt that the .txt or .xml file will be placed in VBAProject? If so, how is the place and how to get the file path or just read the file? If not, is there a transit way to do this work? Just think of the Macro, not the AddIn project. Thank you very much!

+4
source share
1 answer

You can save txt with the ini extension and save your settings in it as key pairs. Using the code below, you can get these settings by providing a key and getting a value.

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Public Function GetINIString(ByVal sApp As String, ByVal sKey As String, ByVal filepath As String) As String Dim sBuf As String * 256 Dim lBuf As Long lBuf = GetPrivateProfileString(sApp, sKey, "", sBuf, Len(sBuf), filepath) GetINIString = Left$(sBuf, lBuf) End Function Sub sample() Dim Path As String Path = ThisWorkbook.Path & "\" & "Path.ini" Link = GetINIString("Path", "Link", Path) End Sub 

Save the .ini file in the same folder as the workbook, or you can modify the Path variable accordingly.

enter image description here

You can also refer to link for more details.

+4
source

Source: https://habr.com/ru/post/1480620/


All Articles