TripleBlack
New Member
- Joined
- Oct 6, 2013
- Messages
- 11
Hello everyone, I'm a complete newbe trying to learn VBA. This is my first attempt so apologies for any obvious mistake in the code I'm posting. I'm also not an English native speaker so sorry for my not so perfect English.
I want a userform to pop up once the workbook is opened, here is the code I wrote in "this workbook"
Private Sub Workbook_Open()
If ActiveWorkbook.Name = "Trading Fax.xlsm" Then
Worksheets("Start").Visible = True
Worksheets("Start").Activate
With frmTransactionAssistant
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
.Show
End With
Else
End If
End Sub
I then created a command button that sends copy of the workbook (without saving it) using outlook, here is the code
Private Sub cmdSend_Click()
Range("T23").Value = Me.cboProofer
If Me.cboProofer.Value = "" Then
Me.cboProofer.SetFocus
MsgBox "Please select a person"
Exit Sub
End If
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook <> "Outlook" Then
MsgBox ("Please open Outlook")
Exit Sub
End If
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
Set wb1 = ActiveWorkbook
If Val(Application.Version) >= 12 Then
If wb1.FileFormat = 51 And wb1.HasVBProject = True Then
MsgBox "There is VBA code in this xlsx file. There will" & vbNewLine & _
"be no VBA code in the file you send. Save the" & vbNewLine & _
"file as a macro-enabled (. Xlsm) and then retry the macro.", vbInformation
Exit Sub
End If
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Make a copy of the file.
' If you want to change the file name then change only TempFileName variable.
TempFilePath = Environ$("temp") & "\"
TempFileName = Range("I10").Value & " " & Range("B10") & " " & Format(Now, "dd-mmm-yy")
FileExtStr = "." & LCase(Right(wb1.Name, _
Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
' Change the mail address and subject in the macro before you run this procedure.
With OutMail
.To = Range("U23")
.CC = ""
.BCC = ""
.Subject = "Please check the attached trading fax"
.Body = ""
.Attachments.Add wb2.FullName
' You can add other files by uncommenting the following line.
'.Attachments.Add ("C:\test.txt")
' In place of the following statement, you can use ".Display" to
' display the mail.
.Display
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys "%s"
End With
On Error GoTo 0
wb2.Close SaveChanges:=False
' Delete the file.
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Unload Me
End Sub
The problem I'm having is that the workbook Outlook sends is opened in protected view and when I click on "Enable editing" I get a Run Time Error 91: Object variable or With block variable not set. The problem disappears if in the Workbook open code I remove the "if" statement but I would like that code to run only on the initial workbook as it makes a userform pop up and this is not necessary for the person that gets the workbook via email.
One solution could be to disable protected view in the application settings but I would like, if possible, to solve the problem differently.
Thanks to everyone that will take his time to help me, of course any kind of suggestion on any other mistake will be very much appreciated.
I want a userform to pop up once the workbook is opened, here is the code I wrote in "this workbook"
Private Sub Workbook_Open()
If ActiveWorkbook.Name = "Trading Fax.xlsm" Then
Worksheets("Start").Visible = True
Worksheets("Start").Activate
With frmTransactionAssistant
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
.Show
End With
Else
End If
End Sub
I then created a command button that sends copy of the workbook (without saving it) using outlook, here is the code
Private Sub cmdSend_Click()
Range("T23").Value = Me.cboProofer
If Me.cboProofer.Value = "" Then
Me.cboProofer.SetFocus
MsgBox "Please select a person"
Exit Sub
End If
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook <> "Outlook" Then
MsgBox ("Please open Outlook")
Exit Sub
End If
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
Set wb1 = ActiveWorkbook
If Val(Application.Version) >= 12 Then
If wb1.FileFormat = 51 And wb1.HasVBProject = True Then
MsgBox "There is VBA code in this xlsx file. There will" & vbNewLine & _
"be no VBA code in the file you send. Save the" & vbNewLine & _
"file as a macro-enabled (. Xlsm) and then retry the macro.", vbInformation
Exit Sub
End If
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Make a copy of the file.
' If you want to change the file name then change only TempFileName variable.
TempFilePath = Environ$("temp") & "\"
TempFileName = Range("I10").Value & " " & Range("B10") & " " & Format(Now, "dd-mmm-yy")
FileExtStr = "." & LCase(Right(wb1.Name, _
Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
' Change the mail address and subject in the macro before you run this procedure.
With OutMail
.To = Range("U23")
.CC = ""
.BCC = ""
.Subject = "Please check the attached trading fax"
.Body = ""
.Attachments.Add wb2.FullName
' You can add other files by uncommenting the following line.
'.Attachments.Add ("C:\test.txt")
' In place of the following statement, you can use ".Display" to
' display the mail.
.Display
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys "%s"
End With
On Error GoTo 0
wb2.Close SaveChanges:=False
' Delete the file.
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Unload Me
End Sub
The problem I'm having is that the workbook Outlook sends is opened in protected view and when I click on "Enable editing" I get a Run Time Error 91: Object variable or With block variable not set. The problem disappears if in the Workbook open code I remove the "if" statement but I would like that code to run only on the initial workbook as it makes a userform pop up and this is not necessary for the person that gets the workbook via email.
One solution could be to disable protected view in the application settings but I would like, if possible, to solve the problem differently.
Thanks to everyone that will take his time to help me, of course any kind of suggestion on any other mistake will be very much appreciated.