我试图复制Excel中的一系列单元格到一个空白的记事本,我得到一个错误

lnlaulya  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(93)

我尝试了下面的代码,并不断得到一个
调试错误(运行时错误'429' ActiveX组件无法创建对象)

Set ojbNotepad = CreateObject(Notepad.Application")
Sub CopyRangeToNotepad()
    Dim objNotepad As Object
    Dim strText As String
    Dim rng As Range
    Dim i As Integer, j As Integer
    
    ' Set the range that you want to copy
    Set rng = Range("A1:B5")
    
    ' Copy the range to clipboard
    rng.COPY
    
    ' Open Notepad
    **Set objNotepad = CreateObject("Notepad.Application")**
    objNotepad.Visible = True
    
    ' Paste the range into Notepad
    objNotepad.Documents.Add
    objNotepad.Selection.Paste
    
    ' Release the object
    Set objNotepad = Nothing
End Sub

知道它为什么一直这样吗
我尝试了上面的方法,得到一个调试错误

xt0899hw

xt0899hw1#

尝试

Sub CopyToNotepad()
    With Application
        Range("A1:B5").Copy
        Shell "Notepad.exe", 3
        SendKeys "^v"
        VBA.AppActivate .Caption 'set focus on Excel Application
        .CutCopyMode = False
    End With
End Sub

相关问题