从形状中读取文本时,值超出范围错误

时间:2017-01-10 17:42:49

标签: excel vba excel-vba

我需要输出一个文本文件,其中包含指定的形状名称和文本内部的文本。

我得到的错误是

  

运行时错误' -2147024809(80070057)'指定的值超出范围。

Sub test()
Dim objFSO As Object, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\[user]\Documents\text.txt", 2)
objFile.Write "" 'This clears the text file

Dim obj As Shape

For Each obj In ActiveSheet.Shapes
    sWrite = obj.Name & "; Content: " & obj.TextFrame2.TextRange.Characters.text
    Debug.Print sWrite
    objFile.WriteLine sWrite
Next obj
objFile.Close
End Sub

1 个答案:

答案 0 :(得分:0)

代码在我的电脑上正常工作

Sub test()
Dim objFSO As Object
Dim objFile As Object' here is your problem

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\[user]\Documents\Proyect folder\otros\test & intentions\test.txt", 2)
objFile.Write "" 'This clears the text file

Dim obj As Shape

For Each obj In ActiveSheet.Shapes
  sWrite = obj.Name & "; Content: " &    obj.TextFrame2.TextRange.Characters.Text
  Debug.Print sWrite
  objFile.WriteLine sWrite
Next obj
objFile.Close
End Sub