Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-04-2020, 08:03 AM
Ilmari Ilmari is offline Error 4605 while trying to copy and paste with same document Windows 10 Error 4605 while trying to copy and paste with same document Office 2016
Novice
Error 4605 while trying to copy and paste with same document
 
Join Date: May 2020
Posts: 2
Ilmari is on a distinguished road
Default Error 4605 while trying to copy and paste with same document

Hello,



I have several documents that contain data in one table (2 columns, row number varies) and I am trying to make a macro that loops through each row (starting from 3rd row) and copies the value of the 1st cell to the 2nd cell. Here's my current code:

Code:
Sub a_macro()
    
    Dim d As Document. t As Table, r As Long, c As Long, rw As Row
    Set d = ActiveDocument
    Set t = d.Tables(1)
    
    For r = 3 To t.Rows.Count
         Set rw = t.Rows(r)
        
        For c = 1 To rw.Cells.Count
            If c = 1 Then
                t.Cell(r, c).Range.Copy

            ElseIf c = 2 Then
                 t.Cell(r, c).Range.PasteAndFormat (wdFormatOriginalFormatting)
            End If
        Next c
    Next r
    
End Sub
But for some reason I'm getting error 4605 - "This command is not available." after copying and pasting 3 values. If I set the range to start from e.g. row 5, it succesfully copies and pastes the 3 next column 1 values. I've tried a few ways to loop the cells (hence the current is not the most elegant implementation) but the error keeps coming. The paste is what causes the error and when I run the code step-by-step with the debugger it runs without errors.

Any ideas how to fix this? I am using Word 2016.
Reply With Quote
  #2  
Old 05-04-2020, 03:18 PM
macropod's Avatar
macropod macropod is offline Error 4605 while trying to copy and paste with same document Windows 7 64bit Error 4605 while trying to copy and paste with same document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Aside from the '.' after 'd As Document', your code works for me. That said, it's verbose and inefficient. Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim r As Long, Rng As Range
With ActiveDocument.Tables(1)
  For r = 3 To .Rows.Count
    With .Rows(r)
      If .Cells.Count > 1 Then
        Set Rng = .Cells(1).Range
        Rng.End = Rng.End - 1
        .Cells(2).Range.FormattedText = Rng.FormattedText
      End If
    End With
  Next r
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-05-2020, 12:08 AM
Ilmari Ilmari is offline Error 4605 while trying to copy and paste with same document Windows 10 Error 4605 while trying to copy and paste with same document Office 2016
Novice
Error 4605 while trying to copy and paste with same document
 
Join Date: May 2020
Posts: 2
Ilmari is on a distinguished road
Default

Works perfectly, thank you! Thanks also for the optimizations, I'm quite new to VBA and sometimes struggling a bit with the syntax even if I know the code isn't very efficient.
Reply With Quote
Reply

Tags
4605, paste, table

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error 4605 while trying to copy and paste with same document Copy/Paste error stuart255 Office 5 05-06-2019 02:13 AM
Error 4605 while trying to copy and paste with same document run-time error 4605 command not available with Selection.paste Marzio Word VBA 14 01-30-2019 04:46 AM
Word Error Message Run time Error 4605 baes10 Word VBA 1 08-30-2018 02:37 PM
Error 4605 while trying to copy and paste with same document Error 4605 when looping through files in folder and deleting comments Peterson Word VBA 2 04-19-2018 08:45 AM
PasteAppendTable not available (Run-Time Error 4605) q_scribe Word VBA 1 08-12-2013 09:56 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:44 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft