Results 1 to 3 of 3

Code to copy rows that meet criteria from Microsoft Excel to Microsoft Word not working

Threaded View

  1. #1
    Registered User
    Join Date
    05-21-2018
    Location
    Singapore
    MS-Off Ver
    Microsoft Office 2013
    Posts
    2

    Exclamation Code to copy rows that meet criteria from Microsoft Excel to Microsoft Word not working

    Hi, I am new to programming and this is the first VBA project I am working on. I have combined two sources I have found online to achieve my purpose but the code does not seem to be working. Please advise me on what I should change.

    Sub CopyYes()
        Dim c As Range
        Dim j As Integer
        Dim Source As Worksheet
        Dim WordApp As Word.Application
        Dim target As Word.Document
        
    'Optimize Code
      Application.ScreenUpdating = False
      Application.EnableEvents = False
    
    'Create an Instance of MS Word
      On Error Resume Next
    
          Set WordApp = GetObject(class:="Word.Application")
        
        'Clear the error between errors
          Err.Clear
    
        'If MS Word is not already open then open MS Word
          If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
        
        'Handle if the Word Application is not found
          If Err.Number = 429 Then
            MsgBox "Microsoft Word could not be found, aborting."
            GoTo EndRoutine
          End If
    
      On Error GoTo 0
      
    'Make MS Word Visible and Active
      WordApp.Visible = True
      WordApp.Activate
      
    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets(2)
    'Create a New Document
    Set target = WordApp.Documents.Add
    
        j = 1     ' Start copying to row 1 in target sheet
        For Each c In Source.Range("E1:E1000")   ' Do 1000 rows
            If c = "2" Then
               Source.Rows(c.Row).Copy
               target.Paragraph(j).Range.PasteExcelTable_
               j = j + 1
            End If
        Next c
    
    EndRoutine:
    'Optimize Code
      Application.ScreenUpdating = True
      Application.EnableEvents = True
    
    'Clear The Clipboard
      Application.CutCopyMode = False
    
        
    End Sub
    Last edited by yodeys; 05-21-2018 at 11:17 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 12-22-2014, 02:24 PM
  2. Copy filtered rows to Microsoft Word
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-20-2010, 05:53 PM
  3. » Copy worksheet information to Word using VBA in Microsoft Excel cont...
    By mscott123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2009, 09:18 AM
  4. Copy & Paste Microsoft word to Microsoft Excel
    By dbacks3449 in forum Excel General
    Replies: 1
    Last Post: 05-02-2007, 06:49 PM
  5. Replies: 1
    Last Post: 08-13-2006, 06:05 PM
  6. Replies: 3
    Last Post: 12-07-2005, 12:00 PM
  7. How do I import Microsoft Word files into Microsoft Excel?
    By Excel in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 01-08-2005, 09:06 PM

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