delete form controls in copied workbook

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All, I have some code that copies a macro enabled workbook to an .xlsx format. The original file has some form controls on it, and I would like to include some code to delete the controls. The code to copy the workbook is below. What I am looking to do is to delete the form controls from the destination workbook (Destwb). How can I do it...?

VBA Code:
Dim Sourcewb As Workbook
    Dim Destwb As Workbook

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    
    Dim answer As Integer
    
    answer = MsgBox("File will be saved in .xlsx format in original file location." & vbCr & vbCr & _
    "Email will be prepared. Please attach any supplemental information", vbOKCancel + vbInformation, "File Save Location")
    
    If answer = vbCancel Then
        Exit Sub
    Else:
        Set Sourcewb = ActiveWorkbook
        Sheets.Copy 'Copy the ActiveSheet to a new workbook

        Set Destwb = ActiveWorkbook
        'Determine the Excel version and file extension/format
        With Destwb
            If Val(Application.Version) < 12 Then
                'You use Excel 97-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2016
                FileExtStr = ".xlsx": FileFormatNum = 51
            End If
        End With
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,
Once you have done your copy, you can test following macro:
VBA Code:
Sub Remove_All_Objects()
    On Error Resume Next
    ActiveSheet.DrawingObjects.Visible = True
    ActiveSheet.DrawingObjects.Delete
    On Error GoTo 0
End Sub
 
Upvote 0
Hi,
Once you have done your copy, you can test following macro:
VBA Code:
Sub Remove_All_Objects()
    On Error Resume Next
    ActiveSheet.DrawingObjects.Visible = True
    ActiveSheet.DrawingObjects.Delete
    On Error GoTo 0
End Sub
Hi there, thanks for your reply. It's not working - completely ignored by the code...
 
Upvote 0
What do you mean by " completely ignored by the code... " ???

Just tested macro , and it is working fine : deleting all Form Controls ...
 
Upvote 0
I presume it was "completely ignored by the code" is because of the "on error resume next" line... When I removed it I found "Error -2147024809: The specified value is out of range." The following modification to my code fixed the problem.
Excel Formula:
With Destwb
            .Sheets("Instructions").Select
            .ActiveSheet.Buttons("btn_ClearData").Delete
            .ActiveSheet.Buttons("btn_ManData").Delete
            .ActiveSheet.Buttons("btn_ImportImage").Delete
            .ActiveSheet.Buttons("btn_ImportData").Delete
            .ActiveSheet.Buttons("btn_Finalize").Delete
            ...
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top