从Excel复制到Word文档时出现剪贴板错误

1szpjjfi  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(295)

我正尝试将Excel单元格(单元格A1到A66)中的文本复制到Word文档中。此操作的目标是将其复制并粘贴为TEXT。如果直接从Excel复制,则将其粘贴为TABLE。

Private Sub Bouton1_Click()
    
    Dim objWord As New Word.Application
    With objWord
        .Documents.Add
        Application.Wait (Now + TimeValue("0:00:01") / 2)
        Worksheets("Description2").Cells(1, 1).Copy
        Application.Wait (Now + TimeValue("0:00:01") / 2)
        .Selection.PasteSpecial xlPasteValues
        .Visible = True
    End With
    
    Dim i As Integer
    For i = 2 To 66
        If Worksheets("Description2").Cells(i, 1) = Worksheets("Description2").Cells(i + 1, 1) Then Exit For
        With objWord
            Application.Wait (Now + TimeValue("0:00:01") / 2)
            Worksheets("Description2").Cells(i, 1).Copy
            Application.Wait (Now + TimeValue("0:00:01") / 2)
            .Selection.PasteSpecial xlPasteValues
            .Visible = True
        End With
    Next i
    
    objWord.Application.Activate
    objWord.Application.WindowState = wdWindowStateMaximize
    
End Sub

这段代码在70%的情况下都能正常工作,如果不能正常工作,我会收到以下错误(或者是一个变体,但总是与剪贴板有关):
运行时错误“4605”:此方法或属性不可用
因为剪贴板为空或无效。
此外,有时会打开随机OneDrive窗口。
我已经添加了Application.Wait行,试图减慢复制/粘贴速度,但效果不太好。
如何使我的代码更可靠?

1tuwyuhd

1tuwyuhd1#

如果要粘贴为文本,可能需要:

Sub CopyAsTextToWord()
    Dim wordApp As New Word.Application

    With wordApp
        .Visible = True
        .Documents.Add

        Worksheets("Description2").Range("A1:A66").Copy
        .Selection.PasteSpecial DataType:=wdPasteText
    End With
End Sub

另一方面,如果你想一次粘贴一个单元格(这是你的原始代码,不确定),也许有一个稍微不同的方法,避免剪贴板。读取范围到一个数组,迭代通过它,然后使用Selection.TypeText“粘贴”每个元素顺序。也许可以使更强大。

Sub TransferAsText()
    Dim wordApp As New Word.Application

    With wordApp
        .Visible = True
        .Documents.Add

        Dim arr()
        arr = Worksheets("Description2").Range("A1:A66").Value

        Dim i As Long
        For i = LBound(arr, 1) To UBound(arr, 1)
            .Selection.TypeText Text:=CStr(arr(i, 1))
        Next i
    End With
End Sub
xzv2uavs

xzv2uavs2#

编写单独的函数并从Excel中捕获所有数据

Function GetDataFromExcel()
    
    CreateObject ("Excel.Application")
    Dim xlApp As Excel.Application, xObjFD As FileDialog
    Set xlApp = New Excel.Application
    xlApp.Visible = True
    Set xObjFD = Application.FileDialog(msoFileDialogFilePicker)
    xObjFD.Title = "Select the excel file location " & FileType
    
    With xObjFD
        .AllowMultiSelect = False
        .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm; *.xlsb", 1
        .Show
        
        ' Selection is not null
        If .SelectedItems.Count > 0 Then
            xFilePath = .SelectedItems.Item(1)
        Else
            Exit Function
        End If
        
        Dim xlWorkBook As Object, valueCollected As String, _
        rowCount As Integer : rowCount = 1
        Set xlWorkBook = xlApp.Workbooks.Open(xFilePath, True, False)
        xlWorkBook.Activate
        Set ArrayValues = New ArrayList
        
loopToCollectData:
        On Error GoTo err
        valueCollected = xlApp.ActiveWorkbook.Sheets("Description2").Range("A" & rowCount).Value
        If valueCollected <> "" Then
            ArrayValues.Add valueCollected
            If rowCount < 66 then
                rowCount = rowCount + 1
                GoTo loopToCollectData
            End If
        End If
    End With
    
    xlWorkBook.Close
    xlApp.Visible = False
    Exit Function

err:
    xlWorkBook.Close
    xlApp.Visible = False
    MsgBox "Please select the relevant input file!"
    End

End Function

一旦数据被收集,那么它就独立于应用程序,可以在word应用程序内部使用。
数组也可用于通过使用范围来收集数据。

Function GetDataFromExcel()   
     '     Some Code ===========     
     dataArrayCollected = Application.Transpose(Range(Cells(1, 1), Cells(66, 1)))
      '     Some more Code ===========   
      '     No more code ===========   😃 😁
End Function

相关问题