从文本文件

时间:2015-05-12 05:14:58

标签: excel vba

您好任何人都可以尽快帮助您从文本文件中读取特定值

这是我的输入文件,

的config.txt

<DatabaseConnectionString>
"Provider=SQL;Password=pa;Persist Security Info=True;User ID=s;Initial Catalog=L;Data Source=C.com"
</DatabaseConnectionString>

我只需要获取VBA中连接字符串标记内的文本

1 个答案:

答案 0 :(得分:0)

以下是开始,现在您需要在代码中输入您的文件或其他内容以填充您的初始字符串DbC

Sub Minu_Manjaly()

Dim DbC As String

DbC = CStr("<DatabaseConnectionString>" & Chr(34) & _
        "Provider=SQL;Password=pa;Persist Security Info=True;User ID=s;Initial Catalog=L;Data Source=C.com" & _
        Chr(34) & "</DatabaseConnectionString>")

MsgBox GetDB_Connect(DbC)

End Sub

这是功能:

Public Function GetDB_Connect(ByVal DB_Connect_Str As String) As String
    Dim CsT As String, _
        StartS As Long

    StartS = InStr(1, DB_Connect_Str, "<DatabaseConnectionString>")

    CsT = Mid(DB_Connect_Str, StartS + Len("<DatabaseConnectionString>"), _
            InStr(StartS, DB_Connect_Str, "</DatabaseConnectionString>") - StartS - Len("</DatabaseConnectionString>") + 1)

    GetDB_Connect = CsT
End Function