VBA-阅读文本文件,将其分成数组并将其打印回VBA Word

发布于 2025-02-03 17:41:19 字数 1237 浏览 1 评论 0原文

您好,我想将包含数据字符串的TXT文件划分为逐行分组。但是,我无法正确输入数据,或者我对函数getArlLength有问题。我是VBA的新手,无法弄清楚这个问题。宏以运行时错误'13'停止:键入不匹配并突出显示代码的这一部分:

GetArrLength = UBound(arr) - LBound(arr) + 1

希望这不是一个大问题。 感谢您的任何想法。

Sub apokus()

'PURPOSE: Send All Data From Text File To A String Variable


Dim TextFile As Integer
Dim filePath As String
Dim FileContent As String
Dim strAll As String
Dim arrString() As String

'File Path of Text File
  filePath = InputBox("Path to your MD file.", "Path to MD", "actual path to the file")


'Determine the next file number available for use by the FileOpen function
  TextFile = FreeFile
  
'Open the text file
  Open filePath For Input As TextFile
  
'Store file content inside a variable
  FileContent = Input(LOF(TextFile), TextFile)
  Close TextFile
  
arrString = Strings.Split(FileContent, vbCr)
Selection.TypeText Text:=GetArrLength(1)
Dim i As Long
For i = 1 To GetArrLength(arrString)
   Selection.TypeText Text:=GetArrLength(i) + vbNewLine
Next i

End Sub
Public Function GetArrLength(arr As Variant) As Long
   If IsEmpty(arr) Then
      GetArrLength = 0
   Else
      GetArrLength = UBound(arr) - LBound(arr) + 1
   End If
End Function

Hello I would like to take my txt file containing string of data, split it into array by line. However, I am not able to either input data to array correctly or I have problem with the function GetArrLength. I am pretty new to VBA and can't figure the problem out. The macro stops with Run-time error '13': type mismatch and highlights this section of the code:

GetArrLength = UBound(arr) - LBound(arr) + 1

Hopefully it's not a big issue.
Thanks for any ideas.

Sub apokus()

'PURPOSE: Send All Data From Text File To A String Variable


Dim TextFile As Integer
Dim filePath As String
Dim FileContent As String
Dim strAll As String
Dim arrString() As String

'File Path of Text File
  filePath = InputBox("Path to your MD file.", "Path to MD", "actual path to the file")


'Determine the next file number available for use by the FileOpen function
  TextFile = FreeFile
  
'Open the text file
  Open filePath For Input As TextFile
  
'Store file content inside a variable
  FileContent = Input(LOF(TextFile), TextFile)
  Close TextFile
  
arrString = Strings.Split(FileContent, vbCr)
Selection.TypeText Text:=GetArrLength(1)
Dim i As Long
For i = 1 To GetArrLength(arrString)
   Selection.TypeText Text:=GetArrLength(i) + vbNewLine
Next i

End Sub
Public Function GetArrLength(arr As Variant) As Long
   If IsEmpty(arr) Then
      GetArrLength = 0
   Else
      GetArrLength = UBound(arr) - LBound(arr) + 1
   End If
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2025-02-10 17:41:19

您的代码应如下:

arrString = Strings.Split(FileContent, vbCr)
Selection.TypeText Text:=GetArrLength(arrString)
Dim i As Long
For i = 0 To UBound(arrString)
   Selection.TypeText Text:=arrString(i) + vbNewLine
Next i

但是,我看不到您的代码的重点。您将包含许多段落的文本文件删除,删除运输返回,然后将文本插入添加马车返回的单词中。

Your code should be as follows:

arrString = Strings.Split(FileContent, vbCr)
Selection.TypeText Text:=GetArrLength(arrString)
Dim i As Long
For i = 0 To UBound(arrString)
   Selection.TypeText Text:=arrString(i) + vbNewLine
Next i

However, I can't see the point of your code. You take a text file that contains a number of paragraphs, remove the carriage returns, then insert the text into Word adding carriage returns back in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文