Adding TextBox2 into subject error

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,229
Office Version
  1. 2010
Platform
  1. Windows
Hi Good morning, please can you help me i have a macro to send an email and for the subject i want to add the text in the textbox2 into the subject heading i have done this in the code below but get an error please can you help?
Code:
 With NewMail
        .Subject = "Late Outage request - " & ThisWorkbook.Sheets("Sheet1").Range & "Sheet1.TextBox2.Value & """
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi sorry this is the whole code below:
Code:
Sub SendHTML_And_Image_As_Body_UsingOutlook()

    Dim olApp As Object, NewMail As Object
    Dim ChartName As String
    Dim imgPath As String


    
    If ActiveWorkbook.MultiUserEditing Then
    ActiveWorkbook.ExclusiveAccess
End If

    Set olApp = CreateObject("Outlook.Application")
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    

    tmpImageName = VBA.Environ$("temp") & "\tempo.jpg"
    
 With Sheets("Sheet1")
    Set RangeToSend = .Range("B2:M16")
    End With
    
    RangeToSend.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    

    
    Set sht = Sheets.Add
    sht.Shapes.AddChart
    sht.Shapes.Item(1).Select
    Set objChart = ActiveChart

    With objChart
        .ChartArea.Height = RangeToSend.Height
        .ChartArea.Width = RangeToSend.Width
        .ChartArea.Fill.Visible = msoFalse
        .ChartArea.Border.LineStyle = xlLineStyleNone
        .Paste
        .Export Filename:=tmpImageName, FilterName:="JPG"
    End With

    sht.Delete

    Set NewMail = olApp.CreateItem(0)
    
    With NewMail
        .Subject = "Late Outage request - " & ThisWorkbook.Sheets("Sheet1").Range & "Sheet1.TextBox2.Value & """
        .To = "jobloggs@gmail.com"
        .CC = "jobloggs@gmail.com"
        .HTMLBody = ""
        .Display
        

    End With
    MsgBox "Email Sent successfully"

err:

    Set olApp = Nothing
    Set NewMail = Nothing
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
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"

    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

    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

    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=")

    TempWB.Close savechanges:=False

    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Last edited:
Upvote 0
Hello,

You could test following

Code:
[COLOR=#333333].Subject = "Late Outage request - " & ThisWorkbook.Sheets("Sheet1").Range & Sheet1.TextBox2.Value[/COLOR]

Hope this will help
 
Upvote 0
Hi thank you for your help, i have tried the code but i get Run Time error 450 wrong number of arguments or invalid property assignment. hope you can help please
 
Upvote 0
Hello again,

Could you explain what is the the TextBox2 .... ?

Is it located on the Sheet1 ?

Is it a Forms Control or an ActiveX control ?
 
Upvote 0
Hi it is an activex control, and what it is is a box for people to type an area in and the area then goes into the subject. Hi yes it is located in sheet1 :) hope you can help :)
 
Upvote 0
Hello,

You could try following :

Code:
[COLOR=#333333]
.Subject = "Late Outage request - " & [/COLOR][COLOR=#303336][FONT=inherit]Sheet1.[/FONT][/COLOR][COLOR=#303336][FONT=inherit]TextBox2[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Value

[/FONT][/COLOR]

Hope this will help
 
Upvote 0
hi thanks, i tried this but it doesnt copy the text in the textbox plus it doesnt copy my range from the code below.
Code:
Sub SendHTML_And_Image_As_Body_UsingOutlook()

    Dim olApp As Object, NewMail As Object
    Dim ChartName As String
    Dim imgPath As String


    
    If ActiveWorkbook.MultiUserEditing Then
    ActiveWorkbook.ExclusiveAccess
End If

    Set olApp = CreateObject("Outlook.Application")
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    

    tmpImageName = VBA.Environ$("temp") & "\tempo.jpg"
    
 With Sheets("Sheet1")
    Set RangeToSend = .Range("B2:M16")
    End With
    
    RangeToSend.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    

    
    Set sht = Sheets.Add
    sht.Shapes.AddChart
    sht.Shapes.Item(1).Select
    Set objChart = ActiveChart

    With objChart
        .ChartArea.Height = RangeToSend.Height
        .ChartArea.Width = RangeToSend.Width
        .ChartArea.Fill.Visible = msoFalse
        .ChartArea.Border.LineStyle = xlLineStyleNone
        .Paste
        .Export Filename:=tmpImageName, FilterName:="JPG"
    End With

    sht.Delete
 
Upvote 0
Could you kindly test the following :

Code:
Sub TestMsgBox()
  MsgBox Sheet1.TextBoxes("TextBox 2").Text
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,280
Members
449,149
Latest member
mwdbActuary

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