Results 1 to 6 of 6

Add Text to String, Output to Wordpad/Word

Threaded View

Jimbo42 Add Text to String, Output to... 11-16-2011, 10:01 PM
Leith Ross Re: Add Text to String,... 11-16-2011, 11:31 PM
Jimbo42 Re: Add Text to String,... 11-17-2011, 01:22 AM
Jimbo42 Re: Add Text to String,... 11-17-2011, 02:14 PM
Leith Ross Re: Add Text to String,... 11-17-2011, 02:16 PM
snb Re: Add Text to String,... 11-17-2011, 05:03 PM
  1. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Add Text to String, Output to Wordpad/Word

    Hello Jimbo42,

    I have added the macro below to the "Print to Word" button in the attached workbook.

    
    ' Thread:  http://www.excelforum.com/excel-programming/801746-add-text-to-string-output-to-wordpad-word.html
    ' Poster:  Jimbo42
    ' Written: November 16, 2011
    ' Author:  Leith Ross
    
    Sub CopyToWordFile()
    
        Dim Cell As Range
        Dim Rng As Range
        Dim RngEnd As Range
        Dim Text As String
        Dim wdApp As Object
        Dim wdDoc As Object
        Dim wdRng As Object
        Dim Wks As Worksheet
        
            Set Wks = ActiveSheet
            
            Set Rng = Wks.Range("A2")
            
                Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
                If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
                
                For Each Cell In Rng
                    If VBA.StrComp(Cell, "NR", vbTextCompare) = 0 Then
                       Text = Text & Cell.Offset(0, 1) & ", "
                    End If
                Next Cell
                
            If Text <> "" Then
            
               Text = "The following branches have recovered: " & Left(Text, Len(Text) - 2)
               
               Set wdApp = CreateObject("Word.Application")
               Set wdDoc = wdApp.Documents.Add
               Set wdRng = wdDoc.Content
               
               wdApp.Visible = True
               wdRng.InsertAfter Text
               
               On Error Resume Next
               wdDoc.Close -2
               
            End If
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1