Consulting

Results 1 to 7 of 7

Thread: VBA wont run unless through the editor

  1. #1
    VBAX Newbie
    Joined
    May 2016
    Posts
    2
    Location

    VBA wont run unless through the editor

    I have some VBA code which is supposed to take an excel worksheet, copy it and email the copy. This has been working fine for the last few months but recently the office machines were updated to windows 7 and it's stopped working.

    If I open the excel file and click the button on the worksheet I have it set to use then the code works (copies file, attaches to email) but stops at clicking send in outlook. But if though I run the code from the VBA editor it works perfectly...

    I'm lost...

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    We can't see anything wrong with the code.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by SamT View Post
    We can't see anything wrong with the code.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    It must have been inadvertently turned to
    code.invisible=true

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Welcome to the VBAX forum, and sorry about the delay in the welcome.

    Quote Originally Posted by SimonKD View Post
    I have some VBA code
    Please don't be offended by the comments above. The truth is, if you dont post the code to the forum, we can't review it.

    And to you other three......
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Newbie
    Joined
    May 2016
    Posts
    2
    Location
    Sorry about that...

    Sub Mail_workbook_Outlook_2()
    
    
    
    
    'If score is less than 80% then User cannot save.
    If Worksheets("Score Sheet").Range("K21").Value <= "63" Then
    
    
         MsgBox ("You have not passed this assement, please try again")
        
    ElseIf Worksheets("Score Sheet").Range("K21").Value > "64" Then
    
    
    'Working in Excel 2000-2013
    
    
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim Destwb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
    
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
    
        Set Sourcewb = ActiveWorkbook
    
    
        'Copy the ActiveSheet to a new workbook
        ActiveSheet.Copy
        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 = ".xlsx": FileFormatNum = -4143
            Else
                'You use Excel 2007-2013
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xlsx": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End With
    
        'Save the new workbook/Mail it/Delete it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = Worksheets("Score Sheet").Range("D3").Value & " " & Worksheets("Score Sheet").Range("D4").Value & " " & Format(Now, "dd-mmm-yy h-mm-ss")
    
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
    
        With Destwb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .to = "email address"
                .CC = ""
                .BCC = ""
                .Subject = TempFileName
                .Body = ""
                .Attachments.Add Destwb.FullName
                .display
                .send
                SendKeys "^{ENTER}"
            End With
            On Error GoTo 0
            .Close savechanges:=False
        End With
    
    
        'Delete the file you have send
        Kill TempFilePath & TempFileName & FileExtStr
    
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    
    
    
    
    'Ending Message !NEEDS TO BE UPDATED!
    MsgBox ("Thank you for attending this training session, your score has been recorded.")
        
    ThisWorkbook.Saved = True
    
    
    Application.Quit
        
           
    End If
    
    
    End Sub

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Try changing this line
                     'You use Excel 97-2003
                    FileExtStr = ".xlsx": FileFormatNum = -4143 '<---
    To
                    FileExtStr = ".xls": FileFormatNum = -4143
    You are also referring to Range("K51") values (numericals) as strings. I would remove the quotes around the numbers.

    I am unable to determine the purpose of checking and changing the file formats. The only file format you are really changing is xlsm's without macros.

    The Sourcewb variable is not used.
    The Workbook containing "Score Sheet" is not specified. Nit Picking, I know.
    You may have to use the FileSystem Object to use the Kill Function

    See also:
    https://msdn.microsoft.com/en-us/lib.../ff198017.aspx
    http://www.rondebruin.nl/mac/mac020.htm
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •