Got another one - Problem with Before Save Macro

klarowe

Active Member
Joined
Mar 28, 2011
Messages
389
I am trying to set up this workbook so that it will automatically compress all of the pictures in the workbook before saving. Since I have Excel 2003, I can't just simply do it as a macro and have to set it up using SendKeys.

In addition to that, it is already set up so that as soon as the original workbook is opened as read-only, it brings up an input box to enter the job number, and then automatically saves the workbook as "number teardown.xls" in a specified folder.

Now here is the issue. When I first open the workbook as read-only it saves it correctly, but then the Compress Pictures dialogue box pops up which I do not want (want it to basically stay hidden). Once the original is saved, it works correctly there-after. But on the initial save it keeps popping the box up and won't continue without making a selection. I've also tried applying a "wait" and it seems to just wait longer before making the box pop up.

Here is the Before Save Code:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.Run "SequentiallyNumberVisiblePagesOnly"
 
    Application.ScreenUpdating = False
    Dim Sheet As Worksheet
    Const TopLeft As String = "A1"
    For Each Sheet In ActiveWorkbook.Sheets
        Application.Goto Sheet.Range(TopLeft), Scroll:=True
    Next
        Sheets("Home").Activate
    Application.ScreenUpdating = True
 
    Dim octl As CommandBarControl
    With Selection
    Set octl = Application.CommandBars.FindControl(ID:=6382)
        Application.SendKeys "%e~"
        Application.SendKeys "%a~"
        Application.SendKeys "{ENTER}"
        octl.Execute
    End With
End Sub

And in-case you need it, here is the BeforeOpen code:
Code:
Private Sub Workbook_Open()
    Sheets("Home").ToggleButton1.Value = False
 
    Dim ws As Worksheet
    For Each ws In Sheets
    If ws.Visible Then ws.Select (False)
    Application.ScreenUpdating = False
    ActiveWindow.View = xlPageBreakPreview
    ActiveWindow.View = xlNormalView
    Sheets("Home").Select
    Next
    Application.ScreenUpdating = True
 
    If ActiveWorkbook.ReadOnly = True Then GoTo 1
    If ActiveWorkbook.ReadOnly = False Then GoTo 2
1:  Response = Application.InputBox("Enter Job Number", "Save-As", vbOKOnly)
    Sheets("Home").Range("C39") = Response
    If Response = False Then
    MsgBox "You have cancelled Save-As." & vbNewLine & "   Report was not saved!"
    Sheets("Home").Range("C39") = ""
    Exit Sub
    End If
    On Error GoTo Error
    ActiveWorkbook.SaveAs Filename:="S:\SERVICE\Shop Teardown Reports\" & Response & " teardown.xls"
2:  Exit Sub
Error: Resume 1
End Sub

Thanks in advance.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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