代码之家  ›  专栏  ›  技术社区  ›  rosi97

VBA GetPrivateProfileString

  •  0
  • rosi97  · 技术社区  · 7 年前

    我想用 GetPrivateProfileString .
    问题是我的字符串包含一些不应该存在的字符

    Picture

    我认为这个问题来自 path = String(255,0) 但是如果我不写这个,我的Excel程序就会停止工作。

    有没有办法绕过这个问题,或者我的代码有什么问题?

    附属的

    Dim path as String,m2_path as string
    path = String(255, 0)
    m2_path = String(255, 0)
    
    nc = GetPrivateProfileString("PATH", "path1", "", path, 255, inipath)
    nc = GetPrivateProfileString("PATH", "path2", "", m2_path, 255, inipath)
    

    作用

    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
    

    [PATH]
    path1="G:\Arbeit\gen molding"
    ...
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   cup    7 年前

    dim path1 as string, path2 as string, path as string
    
    path = string(255, 0)
    nc = GetPrivateProfileString("PATH", "path1", "", path, 255, inipath)
    path1 = left(path, nc)
    nc = GetPrivateProfileString("PATH", "path2", "", m2_path, 255, inipath)
    path2 = left(path, nc)