Hide Table of Contents

Delete OLE Objects from Drawing Sheet and Template Example (VBA)

This example shows how to delete any OLE objects from the current drawing sheet and template.




' Preconditions:

'        (1) Drawing is open.

'        (2) At least one OLE oject exists in the drawing sheet or template.


' Postconditions: All OLE objects existing in drawing sheet

'        and template are deleted.



Option Explicit

Sub DeleteAllOleItemsInSheet _

( _

    swApp As SldWorks.SldWorks, _

    swModel As SldWorks.ModelDoc2, _

    swSheet As SldWorks.Sheet _


    Dim nOleCnt                     As Long

    Dim i                           As Long

    Dim nSize                       As Long

    Dim nAspect                     As Long

    Dim skArray()                   As Variant

    Dim bRet                        As Boolean

    nOleCnt = swSheet.GetOLEObjectCount: If 0 = nOleCnt Then Exit Sub

    ReDim skArray(nOleCnt - 1)


    ' Get location of each OLE item

    For i = 0 To nOleCnt - 1

        skArray(i) = swSheet.GetOLEObjectSettings(i, nSize, nAspect): Debug.Assert Not IsEmpty(skArray(i))

    Next i


    ' Delete each OLE items

    For i = 0 To nOleCnt - 1

        ' Do not assert because OLE object might be in sheet and OLE object

        ' might be in template and cannot be selected

        bRet = swModel.SelectByID("", "OLEITEM", _

                    0.5 * (skArray(i)(0) + skArray(i)(3)), _

                    0.5 * (skArray(i)(1) + skArray(i)(4)), _

                    0.5 * (skArray(i)(2) + skArray(i)(5)))


        ' Do not assert because nothing might be selected

        bRet = swModel.DeleteSelection(False)

    Next i

End Sub

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swDraw                      As SldWorks.DrawingDoc

    Dim swSheet                     As Object

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swDraw = swModel

    Set swSheet = swDraw.GetCurrentSheet



    DeleteAllOleItemsInSheet swApp, swModel, swSheet


    ' Switch to template and delete OLE objects


    Set swSheet = swDraw.GetCurrentSheet

    DeleteAllOleItemsInSheet swApp, swModel, swSheet





    ' Switch back to sheet and make sure all OLE objects are deleted

    Set swSheet = swDraw.GetCurrentSheet

    Debug.Assert 0 = swSheet.GetOLEObjectCount

End Sub


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Delete OLE Objects from Drawing Sheet and Template Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2010 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.