Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2014, 06:30 AM
Sektor Sektor is offline Copy table to another location in the same document Windows 8 Copy table to another location in the same document Office 2013
Novice
Copy table to another location in the same document
 
Join Date: Mar 2014
Location: Russia
Posts: 6
Sektor is on a distinguished road
Default Copy table to another location in the same document

I have one table in document and want to copy it into another location (more precisely, bookmark location). For now I have the following code:



Code:
Dim doc As Document
Dim t As Table

Set doc = ThisDocument
Set t = doc.Tables(1)
t.Select
Selection.Copy
doc.Bookmarks("bm46").Select
Selection.Paste
Is there another, more elegant, way to copy table? Thanks beforehand!
Reply With Quote
  #2  
Old 03-31-2014, 03:10 PM
macropod's Avatar
macropod macropod is offline Copy table to another location in the same document Windows 7 32bit Copy table to another location in the same document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

If the idea is to replicate the table, you could just bookmark and cross-reference the bookmark.

As for your code, you don't need to select anything and you don't need either copy or paste and it doesn't provide for subsequent updates of the table (a new one will be inserted instead). Instead, try:
Code:
Sub TableDuplicate()
Dim BmkNm As String, BmkRng As Range
BmkNm = "bm46"
With ActiveDocument
  On Error Resume Next
  Set BmkRng = .Bookmarks(BmkNm).Range
  BmkRng.Tables(1).Delete
  BmkRng.FormattedText = .Tables(1).Range.FormattedText
  .Bookmarks.Add BmkNm, BmkRng
End With
Set BmkRng = Nothing
End Sub
Although the code is more complicated than yours, that's because of the overheads required to update, rather than just add to, the bookmarked content. Were that not a consideration, the code could be reduced to one line:
Code:
ActiveDocument.Bookmarks("bm46").Range.FormattedText = ActiveDocument.Tables(1).Range.FormattedText
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-31-2014, 09:28 PM
Sektor Sektor is offline Copy table to another location in the same document Windows 8 Copy table to another location in the same document Office 2013
Novice
Copy table to another location in the same document
 
Join Date: Mar 2014
Location: Russia
Posts: 6
Sektor is on a distinguished road
Default

macropod
Thanks a lot! Works perfectly! Word's object model is difficult, so I would never guess to write it the way you did. Once again - thanks!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy table to another location in the same document How to have the location (path) of the Word document on the title bar? Jamal NUMAN Word 24 09-06-2017 12:18 PM
Copy table to another location in the same document Open document file location issue help required jborchel Word 8 10-21-2013 02:58 PM
Displaying the filepath/document location in ribbon MrPahoehoe PowerPoint 1 03-01-2013 06:41 AM
Copy table to another location in the same document VBA to copy bookmark to a new location kent Word VBA 5 06-14-2012 02:18 PM
Copy table to another location in the same document table location, table title pav Word VBA 1 05-24-2011 09:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:16 AM.


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