Sendkeys只从文本文件中写第一个字符

时间:2017-07-16 02:14:26

标签: vba fso

我正在尝试从文本文件中编写所有内容,但它只编写第一个字符。

这是我的代码:

Sub Main
    Dim TextFile As Integer
    Dim FilePath As String
    Dim FileContent As String

    'File Path of Text File
      FilePath = "C:\Users\Username\Desktop\Clipboard.txt"

    '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)

    'Report Out Text File Contents
      SendKeys FileContent

    'Close Text File
      Close TextFile

End Sub

我也试过这个,但同样只写第一个字符:

Sub Main
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Dim objTextStream

    Const strFileName = "C:\Users\Username\Desktop\Clipboard.txt"
    Const fsoForReading = 1

    If objFSO.FileExists("C:\Users\Username\Desktop\Clipboard.txt") Then
        'The file exists, so open it and output its contents
        Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
        SendKeys objTextStream.ReadAll
        objTextStream.close
        Set objTextStream = Nothing
    Else
        'The file did not exist
        SendKeys "was not found."
    End If

    'Clean up
    Set objFSO = Nothing
End Sub

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

看起来我需要更多的库才能工作。

我添加了Microsoft WMI脚本库,OLE自动化,VBA 6和Visual Basic运行时异常。我不确定是哪一个修好了。