learn how to export excel to word

 learn how to export excel to word


Here is the code:


Sub SendRangeToWord()

    Dim rng As Range

    Dim wordApp As Object

    Dim wordDoc As Object

    

    ' Check if a range is selected

    If TypeName(Selection) <> "Range" Then

        MsgBox "Please select a range.", vbExclamation

        Exit Sub

    End If

    

    ' Set the range to the selected range

    Set rng = Selection

    

    ' Create a new instance of Word

    On Error Resume Next

    Set wordApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then

        Set wordApp = CreateObject("Word.Application")

    End If

    On Error GoTo 0

    

    ' Check if Word is running

    If wordApp Is Nothing Then

        MsgBox "Microsoft Word is not available.", vbExclamation

        Exit Sub

    End If

    

    ' Make Word visible and activate it

    wordApp.Visible = True

    wordApp.Activate

    

    ' Create a new Word document

    Set wordDoc = wordApp.Documents.Add

    

    ' Copy the range

    rng.Copy

    

    ' Paste the range into Word

    wordDoc.Range.Paste

    

    ' Clear the clipboard

    Application.CutCopyMode = False

    

    ' Inform the user that the range has been sent to Word

    MsgBox "The selected range has been sent to Microsoft Word.", vbInformation

End Sub