title: Excel VBA won't insert variable into Outlook email subject field
body: <p>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).</p>
  
  <p>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.</p>
  
  <p>Script below:</p>
  
  <pre><code>    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" &amp; _
                 vbNewLine &amp; "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" &amp; _
                 vbNewLine &amp; "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") &amp; "/" &amp; Format(Now, "dd-mm-yy h-mm-ss") &amp; ".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
  </code></pre>
  
  <p>Everything else appears to be working....I am lost for thoughts, and I have scoured the web for script to try....still nothing.</p>
  
  <p>Any help appreciated</p>
  
  <p>Further note:</p>
  
  <p>I did notice that if the </p>
  
  <pre><code>Set subject = Sheets("Sheet4").Range("B2").SpecialCells(xlCellTypeVisible)
  </code></pre>
  
  <p>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.</p>
  
  <p>Never the less both the above now do not copy the value into the subject field of the email.</p>
  
  <p>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?</p>