VBA: strange error with ParentWindow.ClipboardData.SetData

74 Views Asked by At

I'm using VBA on SolidWorks. I found some code to set text data on the clipboard, replicated it without thinking too much about it. It works on a previous macro I did but on this current one, it does not with generated text. Please find below aminimal code replicating my issue:

Sub main()

Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim vComponents As Variant
Dim rootFolder As String
Dim fileType As Long
Dim fileError As Long
Dim fileWarning As Long
Dim i As Integer
Dim compName As String
Dim unorderedList As New Collection
Dim listA As New Collection
Dim output As String
Dim unorderedItem As Variant
Dim objCP As Object
Dim modelTitle As String
Dim FilePath As String
Dim response As String
 
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set objCP = CreateObject("HtmlFile")
On Error Resume Next

rootFolder = "C:\Workbench\"
modelTitle = swModel.GetTitle

listA.Add Left(modelTitle, Len(modelTitle) - 7)

swApp.CloseDoc modelTitle

For Each toBeProcessed In listA
        FilePath = rootFolder & toBeProcessed & ".SLDASM"
        fileType = swDocASSEMBLY
        Set swModel = swApp.OpenDoc6(FilePath, fileType, swOpenDocOptions_Silent, "", fileError, fileWarning)
        vComponents = swModel.GetComponents(True)
        For i = 0 To UBound(vComponents)
            compName = vComponents(i).GetPathName
        unorderedList.Add compName
Next i

output = ""

For Each unorderedItem In unorderedList
    output = output & unorderedItem
    output = output & vbNewLine
Next unorderedItem

response = MsgBox("Press Yes if you want to copy the list below:" & vbNewLine & output, 4, "Header")

If response = vbYes Then
    objCP.ParentWindow.ClipboardData.SetData "text", output
End If

End Sub

It throws the following error:

Run-time error '-2147024809 (80070057)': Invalid argument

I replaced in objCP.ParentWindow.ClipboardData.SetData "text", output output by "test", or even by a replication of the construction of the string ("" & "MS00000" & vbNewLine & "MS00000" & vbNewLine), and it worked just fine. I don't get where the problem is coming from. Can you help me with that?

0

There are 0 best solutions below