{"id":2064,"date":"2024-11-25T08:00:00","date_gmt":"2024-11-25T00:00:00","guid":{"rendered":"https:\/\/www.wyrcad.com\/?p=2064"},"modified":"2024-11-22T10:18:21","modified_gmt":"2024-11-22T02:18:21","slug":"notepad-%e5%87%bd%e6%95%b0%e7%b3%bb%e5%88%97%e4%b9%8b%e4%b8%80","status":"publish","type":"post","link":"https:\/\/www.wyrcad.com\/?p=2064","title":{"rendered":"NotePad \u51fd\u6570\u7cfb\u5217\u4e4b\u4e00"},"content":{"rendered":"\n<p>Public Function FindNotePadText(ByVal sTxtFile As String, ByVal sData As String) As Boolean<br>FindNotePadText = False<br>On Error Resume Next<br>Dim TxtFileNumber As Integer<br>Dim LineContent As String<br>TxtFileNumber = FreeFile<br>If Not FileExist(sTxtFile) Then Exit Function<br>Open sTxtFile For Input As TxtFileNumber<br>Do While Not EOF(TxtFileNumber) &#8216;\u628a\u6587\u672c\u8bfb\u81f3\u6570\u7ec4<br>Line Input #TxtFileNumber, LineContent<br>If InStr(1, LineContent, sData, vbTextCompare) > 0 Then<br>FindNotePadText = True<br>Exit Do<br>End If<br>Loop<br>Close TxtFileNumber<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Function<\/p>\n\n\n\n<p><br>Public Function GetNotePadRowNo(ByVal sTxtFile As String, ByVal sData As String, Optional ByVal bFirst As Boolean = False) As Integer<br>&#8216;sData \u51fa\u73b0\u5728 sTxtFile \u7684\u884c\u53f7\uff0c\u65e0 sData \u4e3a 0\uff0cbFirst = True \u53d6\u7b2c\u4e00\u6b21\uff0cbFirst = False \u53d6\u6700\u540e\u4e00\u6b21<br>On Error Resume Next<br>GetNotePadRowNo = 0<br>If Not <a href=\"https:\/\/www.wyrcad.com\/?p=1239\">FileExist<\/a>(sTxtFile) Then Exit Function<br>Dim TxtFileNumber As Integer<br>Dim LineContent As String<br>Dim LineArray() As Integer<br>Dim LineCount As Integer<br>Dim LineNumber As Integer<br>LineCount = 0<br>LineNumber = 0<br>TxtFileNumber = FreeFile<br>Open sTxtFile For Input As TxtFileNumber<br>Do While Not EOF(TxtFileNumber) &#8216;\u8bfb\u6587\u672c<br>Line Input #TxtFileNumber, LineContent<br>LineNumber = LineNumber + 1<br>If InStr(1, LineContent, sData, vbTextCompare) > 0 Then &#8216;\u51fa\u73b0<br>LineCount = LineCount + 1<br>ReDim Preserve LineArray(1 To LineCount)<br>LineArray(LineCount) = LineNumber<br>End If<br>Loop<br>Close TxtFileNumber<br>If <a href=\"https:\/\/www.wyrcad.com\/?p=798\">IsEmptyArray<\/a>(LineArray) Then Exit Function &#8216;0<br>If bFirst Then<br>GetNotePadRowNo = LineArray(LBound(LineArray))<br>Else<br>GetNotePadRowNo = LineArray(UBound(LineArray))<br>End If<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Function<\/p>\n\n\n\n<p><br>Public Sub ReplaceNotePadText(ByVal sTxtFile As String, ByVal sOld As String, ByVal sNew As String)<br>On Error Resume Next<br>Dim I As Integer, J As Integer<br>Dim Lines() As String<br>Dim LineContent As String<br>Dim iLineCount As Integer<br>Dim TxtFileNumber As Integer<br>TxtFileNumber = FreeFile<br>If Not FileExist(sTxtFile) Then Exit Sub<br>Open sTxtFile For Input As TxtFileNumber<br>Do While Not EOF(TxtFileNumber) &#8216;\u628a\u6587\u672c\u8bfb\u81f3\u6570\u7ec4<br>Line Input #TxtFileNumber, LineContent<br>LineContent = Replace(LineContent, sOld, sNew, 1, -1, vbTextCompare)<br>iLineCount = iLineCount + 1<br>ReDim Preserve Lines(1 To iLineCount)<br>Lines(iLineCount) = LineContent<br>Loop<br>Close TxtFileNumber<br>If iLineCount = 0 Then Exit Sub &#8216;LBound \u4f1a\u51fa\u9519<br>Open sTxtFile For Output As TxtFileNumber<br>For I = 1 To iLineCount<br>Print #TxtFileNumber, Lines(I)<br>Next I<br>Close TxtFileNumber<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p><br>Public Sub TurnNotePadCursor(ByVal sTxtFile As String, ByVal iRow As Integer, Optional ByVal iColumn As Integer = 1, Optional bOpen As Boolean = False) &#8216;\u628a\u6587\u672c\u6587\u4ef6\u5149\u6807\u79fb\u5230\u67d0\u884c\u67d0\u5217<br>On Error Resume Next<br>Dim I As Integer, J As Integer<br>Dim oShell As Object<br>Set oShell = CreateObject(&#8220;WScript.Shell&#8221;)<br>If bOpen Then<br>&#8216;\u65b0\u6253\u5f00\u6587\u4ef6<br>Dim lPid As Long<br>lPid = Shell(&#8220;notepad.exe &#8221; &amp; sTxtFile, vbNormalFocus)<br>AppActivate (lPid)<br>Else<br>&#8216;\u987b\u7d27\u8ddf OpenFile \u4e4b\u540e\uff0c\u4ee5\u4fdd\u8bc1 sTxtFile \u4e3a\u5f53\u524d\u7a97\u4f53<br>&#8216;Debug.Print sTxtFile<br>&#8216;Debug.Print GetShortName(sTxtFile)<br>For I = 1 To GetNotePadRowCount(sTxtFile, False)<br>DoEvents<br>oShell.SendKeys &#8220;{UP}&#8221; &#8216;\u56de\u5230\u9996\u884c<br>Next I<br>For J = 1 To GetNotePadColumnCount(sTxtFile, False)<br>DoEvents<br>oShell.SendKeys &#8220;{LEFT}&#8221; &#8216;\u56de\u5230\u9996\u5217<br>Next J<br>End If<br>For I = 1 To iRow &#8211; 1<br>oShell.SendKeys &#8220;{DOWN}&#8221;<br>Next I<br>For J = 1 To iColumn &#8211; 1<br>oShell.SendKeys &#8220;{RIGHT}&#8221;<br>Next J<br>ERR_NORUNNING:<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p><br>Public Function GetNotePadRowCount(ByVal sTxtFile As String, Optional ByVal bTrimRow As Variant = True) As Integer &#8216;\u83b7\u53d6 txt \u6587\u4ef6\u884c\u6570, bTrimRow \u4e3a\u771f, \u5219\u7a7a\u884c &#8220;&#8221; \u53ca\u7a7a\u683c\u884c &#8221; &#8221; \u4e0d\u8ba1<br>On Error Resume Next<br>GetNotePadRowCount = 0<br>Dim TxtFileNumber As String<br>TxtFileNumber = FreeFile<br>If bTrimRow Then<br>Dim LineContent As String<br>Open sTxtFile For Input As TxtFileNumber<br>Do While Not EOF(TxtFileNumber) &#8216;\u628a\u6587\u672c\u8bfb\u81f3\u6570\u7ec4<br>Line Input #TxtFileNumber, LineContent<br>LineContent = Trim(LineContent)<br>If Not LineContent = &#8220;&#8221; Then GetNotePadRowCount = GetNotePadRowCount + 1<br>Loop<br>Close TxtFileNumber<br>Else<br>Open sTxtFile For Binary As #TxtFileNumber<br>GetNotePadRowCount = UBound(Split(Input(LOF(1), #1), vbCrLf)) + 1<br>Close #TxtFileNumber<br>End If<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Function<\/p>\n\n\n\n<p><br>Public Function GetNotePadColumnCount(ByVal sTxtFile As String, Optional ByVal bTrimRow As Variant = True) As Integer &#8216;\u83b7\u53d6 txt \u6587\u4ef6\u5217\u6570(\u6700\u5927\u5217\u6570)), bTrimRow \u4e3a\u771f, \u5219\u6bcf\u884c\u4e24\u7aef\u7684 &#8221; &#8221; \u4e0d\u8ba1<br>On Error Resume Next<br>Dim I As Integer, J As Integer<br>GetNotePadColumnCount = 0<br>Dim LineContent As String<br>Dim TxtFileNumber As Integer<br>TxtFileNumber = FreeFile<br>If Not FileExist(sTxtFile) Then Exit Function<br>Open sTxtFile For Input As TxtFileNumber<br>Do While Not EOF(TxtFileNumber)<br>Line Input #TxtFileNumber, LineContent<br>If bTrimRow Then LineContent = Trim(LineContent)<br>If Len(LineContent) > GetNotePadColumnCount Then GetNotePadColumnCount = Len(LineContent)<br>Loop<br>Close TxtFileNumber<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Function<br>Public Function GetNotePadColumnCountAtRow(ByVal sTxtFile As String, ByVal iRow As Integer, Optional ByVal bTrimRow As Variant = True) As Integer &#8216;\u83b7\u53d6 txt \u6587\u4ef6\u67d0\u884c\u5217\u6570, bTrimRow \u4e3a\u771f, \u5219\u884c\u4e24\u7aef\u7684 &#8221; &#8221; \u4e0d\u8ba1<br>On Error Resume Next<br>Dim I As Integer, J As Integer<br>GetNotePadColumnCountAtRow = 0<br>Dim LineContent As String<br>Dim TxtFileNumber As Integer<br>Dim iCount As Integer<br>TxtFileNumber = FreeFile<br>If Not FileExist(sTxtFile) Then Exit Function<br>Open sTxtFile For Input As TxtFileNumber<br>Do While Not EOF(TxtFileNumber)<br>Line Input #TxtFileNumber, LineContent<br>iCount = iCount + 1<br>If bTrimRow Then LineContent = Trim(LineContent)<br>If iCount = iRow Then<br>GetNotePadColumnCountAtRow = Len(LineContent)<br>Exit Do<br>End If<br>Loop<br>Close TxtFileNumber<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Function<\/p>\n\n\n\n<p><br>Public Function GetNotePadRowText(ByVal sTxtFile As String, ByVal iLineNumber As Integer) As String &#8216;\u83b7\u53d6 txt \u6587\u4ef6\u67d0\u884c\u5185\u5bb9<br>On Error Resume Next<br>Dim I As Integer, J As Integer<br>GetNotePadRowText = &#8220;&#8221;<br>If iLineNumber > GetNotePadRowCount(sTxtFile, False) Then Exit Function<br>Dim oFSO As Object, oFile As Object, oStream As Object<br>Set oFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<br>Set oFile = oFSO.GetFile(sTxtFile)<br>Set oStream = oFile.OpenAsTextStream(1, 0)<br>&#8216; \u8bfb\u53d6\u6307\u5b9a\u884c<br>For I = 1 To iLineNumber<br>GetNotePadRowText = oStream.ReadLine<br>Next<br>oStream.Close<br>Set oStream = Nothing<br>Set oFile = Nothing<br>Set oFSO = Nothing<br>If Err.Number &lt;> 0 Then Err.Clear<br>On Error GoTo 0<br>End Function<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Public Function FindNotePadText(ByVal sTxtFile As Strin &hellip; <a href=\"https:\/\/www.wyrcad.com\/?p=2064\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cNotePad \u51fd\u6570\u7cfb\u5217\u4e4b\u4e00\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[24,17],"class_list":["post-2064","post","type-post","status-publish","format-standard","hentry","category-bianchengyuandi","tag-vb","tag-biancheng"],"_links":{"self":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/2064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2064"}],"version-history":[{"count":3,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/2064\/revisions"}],"predecessor-version":[{"id":2069,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/2064\/revisions\/2069"}],"wp:attachment":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}