Error opening workbook sent via email

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.
 
Tried again today and unfortunately I'm having the same result. Here is the code I have in the original workbook:

Code:
[/FONT][/COLOR]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)
    With wb2.VBProject.VBComponents("ThisWorkbook").CodeModule
    .DeleteLines 1, .CountOfLines
    End With
    wb2.Save


    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[COLOR=#574123][FONT=Courier New]  [/FONT][/COLOR][COLOR=#574123][FONT=system]

when I run the cmdSend_Click procedure from my home pc (Win 8 office 2010) the workbook that I get via email doesn't have any code in ThisWorkbook and therefore I don't get any error. If I do exactly the same from my work pc (Win 7 Office 2010) the code in ThisWorkbook does not get removed and I therefore get the error when opening the workbook Outlook sent.

This is driving me crazy, it is already sooo difficult for me given that I'm a total newbe to VBA and getting different results from the same code is really confusing...thanks everyone might be able to help.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Pleeease some help...I have been working for so long on this and now I get stuck on this issue which I just cannot understand how can even happen.

Thank you so much in advance:)
 
Upvote 0
I have no idea what the problem is. Sorry.

One guess would be the dropbox version doesn't have the latest updated code. Is the right version of your file loaded on DropBox? The one that includes the wb2.Save line?

From your home computer, did you test the file from dropbox (not a locally saved version)?
 
Upvote 0
Found the problem, actually embarrassing, the "Trust access to the VBA project object model" option wasn't flagged on my work computer and apparently that was the reason why the code was not getting deleted properly...
Thanks for the support!
 
Upvote 0

Forum statistics

Threads
1,216,468
Messages
6,130,800
Members
449,595
Latest member
jhester2010

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