0

I have an Excel Macro that is run and generates a docket to stick on student laptops that are handed in for repair, the document also emails the help desk that creates a job in the system with the same details (or part of the details anyway).

The laptop that is used was upgraded to Windows 8.1 and upgraded from Outlook 2010 to Outlook 2013. The script used to work on the old system however since upgrading to the new system the subject no longer populates, even though the variable 'subject' when the mouse is hovered over it, shows the text that should be entered.

Script below:

    Sub Next_Loan()
'
' Next_Loan Macro
' Macro recorded 18/05/2011
'
' Keyboard Shortcut: Ctrl+n
'
Sheets("Sheet1").Select
    Range("D4").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-2]="""",RC[3],VLOOKUP(RC[-2],Sheet2!R[-3]:R[65532],2,FALSE))"
    Range("E4").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-3]="""",CONCATENATE(RC[3],""@eq.edu.au""),VLOOKUP(RC[-3],Sheet2!R[-3]:R[65532],3,FALSE))"
    Range("F4").Select




    ActiveCell.FormulaR1C1 = "=NOW()"
    Range("A4:F4").Select
    Range("F4").Activate
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Rows("4:4").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlDown
    Range("L4").Font.Color = RGB(211, 211, 211)


'    ActiveWindow.SmallScroll Down:=-9
    Sheets("Sheet3").Select
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "=Sheet1!R5C4"
    Range("D6").Select
    ActiveCell.FormulaR1C1 = "=Sheet1!R5C5"
    Range("D7").Select
    ActiveCell.FormulaR1C1 = "=Sheet1!R5C6"
    Range("D10").Select
    ActiveCell.FormulaR1C1 = "=Sheet1!R5C3"
    ActiveCell.Offset(-5, 0).Range("A1:B9").Select


    Sheets("Sheet3").Select
    Range("D4:D20").Select
    ActiveSheet.PageSetup.PrintArea = "$D$4:$D$20"
    ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,1,,,TRUE,,FALSE)"
    Sheets("Sheet1").Select
    Range("A4").Select

'
'Sub Mail_Selection_Range_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2010
    Dim rng As Range
    Dim subject As Range
    Dim OutApp As Object
    Dim OutMail As Object
Sheets("Sheet4").Select
Range("B2:B10").Select
    Set rng = Nothing
    Set subject = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a range if you want
    Set rng = Sheets("Sheet4").Range("B1:B10").SpecialCells(xlCellTypeVisible)
'    Set subject = Sheets("Sheet4").Range("B2").SpecialCells(xlCellTypeVisible)
    Set subject = Sheets("Sheet4").Range("B2")
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    If subject Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If


    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)

    On Error Resume Next
    With OutMail
        .To = "EmailGoesHere"
        .CC = ""
        .BCC = ""
        .subject = subject
        .HTMLBody = RangetoHTML(rng)
        .display
'        .Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    Sheets("Sheet1").Select
    Range("A4").Select

     'Clear contents of Sheet 1 I5 and L5 (Cell Phone Number and Student Password after printing ticket)
Range("I5").ClearContents
Range("L5").ClearContents
End Sub

Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Everything else appears to be working....I am lost for thoughts, and I have scoured the web for script to try....still nothing.

Any help appreciated

Further note:

I did notice that if the

Set subject = Sheets("Sheet4").Range("B2").SpecialCells(xlCellTypeVisible)

is used, it doesn't copy the text in Sheet4,B2 however if you remove the .SpecialCells(xlCellTypeVisible) then it copies the value into the variable...The former worked in the previous version of Excel.

Never the less both the above now do not copy the value into the subject field of the email.

If I change the 'subject' variable to a double quoted string, it inserts the string into the subject field, so for some reason, it doesn't like the variable, or is my syntax incorrect somehow?

Random206
  • 49
  • 4
  • 13
  • Maybe it is perfectly fine to try and set the "Subject" to a variable that is defined as a "Range", but I think you may want to change your code to retrieve the values from the cell(s) into something defined as a string, the set ".Subject" to that string. Also, IMO, using variable names that are spelled the same as properties / methods, etc. is asking for trouble. – Wayne G. Dunn Nov 11 '14 at 03:13
  • @WayneG.Dunn Thanks for the answer, this isn't actually my script it's a script from the previous guy before I was here, I am just building on and keeping it alive persay. Thanks for your input, I will give it a shot now. – Random206 Nov 11 '14 at 04:39
  • @WayneG.Dunn If you submit your answer as an actual answer to the question I will mark it as correct and best answer. – Random206 Nov 11 '14 at 05:09

1 Answers1

2

[EDIT: Added .Value to range] Your code is trying to set the mail property 'Subject', which is typed as a string, to a variable that is defined as a 'Range'. VBA will try and coerce one type to the other, but it is not always correct and your result can sometimes just be unpredictable. I would either change the datatype of variable 'subject' to String and grab the value from cell B2, or just change the line from:

.subject = subject

to

.subject = Worksheets("Sheet4").Range("B2").Value
Wayne G. Dunn
  • 4,282
  • 1
  • 12
  • 24