如何使用 Word 更改 INI 文件中的部分文本 VBA

How to change section text in INI file using Word VBA

我必须使用 Word 更改 ini 文件中的部分 VBA。

我知道如何更改键的值,但是谁能告诉我如何更改一个部分(第一部分=唯一部分)的值? 在下面的示例中,我想在 NewName 中更改 OldName。

[办公室]
Office1=旧名称

[原名]
键 1=值
键2=值 等等

谢谢,
凯姆

您可以使用此代码替换任何文本文件中的文本

Public Sub Test()

    ReplaceInTXT "C:\folder\file.ini", "[OldName]", "[NewName]"

End Sub

Public Sub ReplaceInTXT(sFileName As String, sFind As String, sReplace As String)

    Dim Content As String
    Dim hFile As Long

    hFile = FreeFile
    Open sFileName For Input As #hFile
    Content = Input$(LOF(hFile), hFile)
    Close #hFile

    Content = Replace(Content, sFind, sReplace)

    Open sFileName For Output As #1
    Print #1, Content
    Close #1

End Sub

请务必先备份。我不想弄乱文件