Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2019, 06:53 AM
sts023 sts023 is offline Inserting a Table using VBA Windows 7 64bit Inserting a Table using VBA Office 2010
Hopeless Idiot
Inserting a Table using VBA
 
Join Date: Apr 2019
Location: God's Own County
Posts: 26
sts023 is on a distinguished road
Default Inserting a Table using VBA

Hi guys...
I have a document on which there is a specific table.
I want to copy that table "as is" to another document, but WITHOUT using the clipboard.


I can save the table in a variable Dim'd as Word.Table, but I dont know how to insert that table into another document.
I can't find an "Activedocument.Tables.Insert" with a table as an operand; all I've found is the "ActiveDocument.Tables.Add", which doesn't seem to have the facility to have a table variable as an operand.
I know I could extract the dimensions, formats and contents and use those details to "Add" and then populate the new table, but that's one great lump of coding I'd like to avoid.
Surely if I can code
Code:
Dim tbl                     As Word.Table
  Set tbl = ActiveDocument.Tables(1)
then there must be something like
Code:
Dim tbl                     As Word.Table
  ActiveDocument.Tables.insert(tbl)
isn't there?
Can ayone help?
Reply With Quote
  #2  
Old 08-18-2019, 07:58 AM
gmaxey gmaxey is offline Inserting a Table using VBA Windows 10 Inserting a Table using VBA Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc1 As Document
Dim oDoc2 As Document
Dim oRng As Range, oRngInsert As Range
  Set oDoc1 = ActiveDocument 'The one with the table
  Set oDoc2 = Documents.Add 'Some other document
  
  Set oRng = oDoc1.Tables(1).Range
  Set oRngInsert = oDoc2.Range
  oRngInsert.Collapse wdCollapseEnd
  oRngInsert.FormattedText = oRng.FormattedText
lbl_Exit:
  Exit Sub
  
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 08-18-2019, 08:20 AM
sts023 sts023 is offline Inserting a Table using VBA Windows 7 64bit Inserting a Table using VBA Office 2010
Hopeless Idiot
Inserting a Table using VBA
 
Join Date: Apr 2019
Location: God's Own County
Posts: 26
sts023 is on a distinguished road
Default

It works (obviously, it's from Greg).

I would never have worked that out in a month of Sundays.

So the first "trick" is to save the table as a Range instead of a Table, and the second is to use the FormattedText attribute.

Now I know why I never worked for Microsoft - my mind isn't sufficiently agile (or should that be "warped"?).


Thanks again Greg!
Reply With Quote
  #4  
Old 08-18-2019, 08:44 AM
gmaxey gmaxey is offline Inserting a Table using VBA Windows 10 Inserting a Table using VBA Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Well you aren't actually saving the table as a range. The table already defines a range.


A character defines a range, a word defines a range, a paragraph defines a range and yes, a table does too. ;-)


Here is an abbreviated version that does the same thing with no declared range variables.

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc1 As Document
Dim oDoc2 As Document
  Set oDoc1 = ActiveDocument
  Set oDoc2 = Documents.Add 'Some other document
  oDoc1.Activate
  oDoc2.Range.FormattedText = oDoc1.Tables(1).Range.FormattedText
  oDoc2.Activate
lbl_Exit:
  Exit Sub
  
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 08-18-2019, 08:58 AM
sts023 sts023 is offline Inserting a Table using VBA Windows 7 64bit Inserting a Table using VBA Office 2010
Hopeless Idiot
Inserting a Table using VBA
 
Join Date: Apr 2019
Location: God's Own County
Posts: 26
sts023 is on a distinguished road
Default

OK, I know this is going a bit "off piste", but presumably, as the entire known (Word) universe is a Range, I suspect that the only reason to have a Dim of Table is to enable us to Set the attributes for each cell all at once: or can that be done in a Range as well?
If there's an Idiots Guide to "Fun Things To Do With Ranges" that you know of (I just bet there's one at The Anchorage!), could you reply with a link?
I promise I'll go away then (for a while, at least).
Ta!
Reply With Quote
  #6  
Old 08-18-2019, 09:27 AM
gmaxey gmaxey is offline Inserting a Table using VBA Windows 10 Inserting a Table using VBA Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

There is a bit here called Learn to Love ranges.


VBA Basics


Actual a table is an object (type table). Declaring a variable just makes coding easier. (I'm not formally trained so could be wrong).

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Object
Dim oTbl2 As Table
Dim lngIndex As Long
  For Each oTbl In ActiveDocument.Tables
    MsgBox TypeName(oTbl)
    oTbl.Range.Cells(1).Range.Text = "AAA"
    MsgBox oTbl.Rows.Count
  Next oTbl
  For Each oTbl2 In ActiveDocument.Tables
    oTbl2.Range.Cells(1).Range.Text = "BBB"
    MsgBox oTbl2.Rows.Count
  Next oTbl2
  For lngIndex = 1 To ActiveDocument.Tables.Count
    ActiveDocument.Tables(lngIndex).Range.Cells(1).Range.Text = "CCC"
    MsgBox ActiveDocument.Tables(lngIndex).Rows.Count
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 08-19-2019 at 04:19 AM.
Reply With Quote
  #7  
Old 08-19-2019, 03:58 AM
sts023 sts023 is offline Inserting a Table using VBA Windows 7 64bit Inserting a Table using VBA Office 2010
Hopeless Idiot
Inserting a Table using VBA
 
Join Date: Apr 2019
Location: God's Own County
Posts: 26
sts023 is on a distinguished road
Default

Thanks Greg - very informative.

As a practicing idiot, I make the following remark with a great deal of trepidation!

I think there's an oversite in your code.

Sorry!

I think
Code:
    oTbl.Range.Cells(1).Range.Text = "BBB"
should be
Code:
    oTbl.Range.Cells(1).Range.Text = "AAA"
and when executing the code in a document with three varyingly sized tables. the first two loops change Cells(1) to the new letters in each table, but the third loop only changes the Cells(1) in the first table.
Unfortunately I haven't yet grasped exactly what's going on, so if I'm right (which is by no means certain), I can't suggest a correction for the third loop.

Believe me, this is not a vanity comment.
I use sample code from Forums to help me to learn techniques and good practices, so if there IS a bug it would be helpful to future browsers to correct it, and if there ISN'T a bug, an explanation of why my my comments are wrong could prevent other people from making the same mistake.
Reply With Quote
  #8  
Old 08-19-2019, 04:21 AM
gmaxey gmaxey is offline Inserting a Table using VBA Windows 10 Inserting a Table using VBA Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

sts,


The code in the third loop did exactly what I coded it wrong to do. It put the text in the active documents first cell, not the the first cell of the indexed table. See revised code.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 08-19-2019, 04:39 AM
sts023 sts023 is offline Inserting a Table using VBA Windows 7 64bit Inserting a Table using VBA Office 2010
Hopeless Idiot
Inserting a Table using VBA
 
Join Date: Apr 2019
Location: God's Own County
Posts: 26
sts023 is on a distinguished road
Default

Thanks Greg. Brilliant AND humble (wish I was talking about me!)
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax for inserting blank line before inserting table and after a line or paragraph SamDsouza Word VBA 8 08-04-2019 11:10 PM
Inserting graphic in last table lodi123 Word VBA 2 07-07-2017 06:48 AM
Inserting a Table using VBA Inserting a table using ADD command snufse Word VBA 1 11-10-2015 03:28 PM
Inserting a Table using VBA Inserting table natasha_arriell Mail Merge 4 06-04-2014 12:13 AM
Inserting a table Katelyn Outlook 0 04-05-2012 09:39 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:48 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