PDA

View Full Version : Force macro and save



filipebessa9
05-21-2015, 09:00 AM
Hi everyone,

I'm a bit new to the forum so forgive me if I have posted in the wrong place.

I'm currently doing a timesheet module for work and am struggling with a bit of code.

I have found here a bit of code which is perfect for what I want and need done but unfortunately it's interfering with other part of my code.

I am using the following code to make people enable the macro:

Option Explicit

Const WelcomePage = "Macros"

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
Call CustomSave
Case Is = vbNo
'Do not save
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If

'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
.Close savechanges:=False
Else
Application.EnableEvents = True
End If
End With
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True

'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub

Private Sub Workbook_Open()
'Unhide all worksheets
Application.ScreenUpdating = False
Call ShowAllSheets
Application.ScreenUpdating = True
End Sub

Private Sub CustomSave(Optional SaveAs As Boolean)
Dim ws As Worksheet, aWs As Worksheet, newFname As String
'Turn off screen flashing
Application.ScreenUpdating = False

'Record active worksheet
Set aWs = ActiveSheet

'Hide all sheets
Call HideAllSheets

'Save workbook directly or prompt for saveas filename
If SaveAs = True Then
newFname = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls")
If Not newFname = "False" Then ThisWorkbook.SaveAs newFname
Else
ThisWorkbook.Save
End If

'Restore file to where user was
Call ShowAllSheets
aWs.Activate

'Restore screen updates
Application.ScreenUpdating = True
End Sub

Private Sub HideAllSheets()
'Hide all worksheets except the macro welcome page
Dim ws As Worksheet

Worksheets(WelcomePage).Visible = xlSheetVisible

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden
Next ws

Worksheets(WelcomePage).Activate
End Sub

Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
Next ws

Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub

------


When using this, unfortunately, it's then stopping my worksheet from being saved with the name I want it to have.

When not using the veryhidden code it does it by using the following code:
Public Sub Send_Email()
On Error Resume Next
Application.DisplayAlerts = False
MkDir ("C:\Timesheets\")
ActiveWorkbook.SaveAs "C:\Timesheets\" & Range("A3").Value & " " & Replace(Range("B3"), "/", "-") & ".xlsm"

Dim linsOutlookApp As Object
Dim linsMailItem As Object
Dim lstrFileName As String
Dim lstrOldFileName As String
Dim MailAddress As String
Dim CCMailAddress As String
Dim MailSubject As String
Dim EmailComments As Variant

Sheets("Time Entry").Activate
ActiveWorkbook.Save


If MsgBox("Press OK to send email", vbOKCancel, "Email Confirmation") = vbOK Then


Set linsOutlookApp = CreateObject("Outlook.Application")
linsOutlookApp.Session.Logon
Set linsMailItem = linsOutlookApp.CreateItem(0)

linsMailItem.Attachments.Add ActiveWorkbook.FullName

linsMailItem.Recipients.Add "myemail"

linsMailItem.Subject = "Timesheet for " & Range("A3").Value & " for w/c " & (Range("B3").Value)
linsMailItem.Body = "This file was sent automatically from Excel. " & _
linsMailItem.DeleteAfterSubmit = False
linsMailItem.Send

Else: End
End If
End Sub



------


Could someone please help me tie these two up?

I need the veryhidden code to let my macro save the spreadsheet so it can then be sent with the right name and date.

Thank you for all your help! :)

Regards,

Andre Bessa

filipebessa9
05-22-2015, 05:40 AM
Anyone to give a quick hand on this, please?

jonh
05-22-2015, 06:18 AM
Hi there.

Unfortunately you've posted an Excel question in the Access forum which is generally pretty quiet anyway.

I know a little bit about Excel, and would normally try to help, but,

1) I'm not really sure what you are asking about. You aren't very specific about what the problem is, e.g. what do you want it to do, what does it do instead, what errors are generated, etc?

and 2) The code you've posted isn't easy to read because you didn't use the code tags (# button on the toolbar jobbo) and (it's Excel (and Email which I have even less interest in) so) I can't be bothered to try and decipher it.

This isn't sarcasm or anything... just...y'know, help us to help you...

I guess this is the problem code?


ActiveWorkbook.SaveAs "C:\Timesheets\" & Range("A3").Value & " " & Replace(Range("B3"), "/", "-") & ".xlsm"

filipebessa9
05-22-2015, 07:00 AM
Hi John,

Thank you for responding to my email.

No, that works when I don't use the veryhidden code.

I will try and word it better and respost in the right place.

My apologies for this.

Regards

Andre Bessa