VBA Command Button to send sheet contents to Word File and Outlook Message

rimshot609

Board Regular
Joined
Jun 4, 2015
Messages
79
I have a macro that exports the contents from a worksheet to a text file. It works great but I need to tweak a little.

I would like to save it as a Word document with font, size, color included. I tried changing the .txt to .doc but that doesn't work. It creates a corrupt file. I would also like it to open the file to send as an Outlook message as well. I don't want it to send the message but just open in a message window with the to and subject fields already filled in as well and to have the to and from fields referenced from a worksheet. Thanks!

Code:
Private Sub CommandButton1_Click()
    Dim myArea As Range, myAreas As Areas
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(ThisWorkbook.Path & "\Files\" & Sheets("Title").Range("A1") & ".txt", True)
        With Sheets("Contents")
            With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
                On Error Resume Next
                Set myAreas = .SpecialCells(12).Areas
                On Error GoTo 0
            End With
        End With
        For Each myArea In myAreas
            .writeline Join(Application.Transpose(myArea), vbCrLf)
        Next myArea
    End With
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,214,822
Messages
6,121,772
Members
449,049
Latest member
greyangel23

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