Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying Word Table without using Clipboard

Status
Not open for further replies.

ShaneBrennan

Programmer
May 19, 1999
198
GB
Hi Everyone - hope you all had a very jolly New Year.

I am application that uses the clipboard to copy and paste tables in Word. Which works fine - but once you use any other application and do Ctrl-C the clipboard gets overwritten and the application crashes.

Basically its a catalog system, where I ahve a template with a large A4 table, which I copy, fill in the details. If there is another record to write, I paste the table to get another blank form, fill it in, and so on.

Using the .copy and .paste is easy!


I've managed to come up with the following code where I can highlight a range and past it to another range - fully formatted. But with 1 problem - it's copying a fully completed table instead a blank one! Because it's copying what's in that range at that moment in time (I've cut the code down a little :

Private Sub Form_Load()
Dim filename As String
Dim SaveFilename As String
Dim TheSourceRange As Word.Range, TheDestinationRange As Word.Range

Set oApp = CreateObject("Word.Application")
With oApp
filename = "c:\temp\Catalogue Template.doc"
SaveFilename = "c:\Temp\Finished Report.doc"

.ScreenUpdating = True
.Visible = True
.Documents.open filename:=Chr(34) & filename & Chr(34), _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:=""

CheckExistingFile SaveFilename
.activedocument.SaveAs filename:=SaveFilename
With .Selection
.HomeKey unit:=wdStory

Find oApp, "<Table1>"
.tables(1).Select
Set TheSourceRange = oApp.Selection.Range.Duplicate
: : : :
: : : :
: : : :
: : : :
: : : :

Set TheDestinationRange = oApp.Selection.Range
TheDestinationRange.FormattedText = TheSourceRange.FormattedText

End With
End With

Set oApp = Nothing

End Sub

If possible what I need is to record what's in the table to a memory variable, then use this to update the "TheDestinationRange"

Thank you for any help you can offer

Shane Brennan


 
Hi Shane,

Is there a reason you don't simply create the new tables programmatically?

As an alternative, and assuming this is all within the one document, you could start out by having your original table bookmarked. Then, whenever you want another table, simply insert a cross-reference to the bookmark, then unlink it. Voila! a duplicated table with no copy/paste. From there, it's a simple matter to clear the table's contents:
Code:
Dim oCel As Cell
With ActiveDocument.Range.Tables
  For Each oCel In .Item(.Count).Range.Cells
    oCel.Range.Text = vbNullString
  Next
End With

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul

I'll give this a try, I should have thought about clearing the text afterwards. I've been determined to have a blank table.

Thank you again for your help.



Shane Brennan


 
I would certainly go for the creation of a new table programmatically.

Or, use an AutoText entry.

I mean, if I understand it correctly. You want an empty table, right?

< 60 50 working days until retirement
 
Hi Fumei and MacroPod

Thank you very much for your help, I've now managed to do this in a test environment, I'm going to try it on the live system shortly.

The tables are too complicated to create dynamically, as the template can be used by different clients who want their own layouts - therefore the system needs to be generic - sorry I forgot to mention that.

The table is like a form with labels and merge codes, so blanking the fields would be difficult as there is no clear indication of what is a lable or completed text once the table had been completed and was ready to copy. However after several sleepless nights (just kidding) I've thought of a possible solution - very simple - copy the table first whilst blank then for subsequent tables, use the copy - it's blank has the correct formatting and mergecodes still in place!

Basically the solution is:

1) Find the start of the table. Then mark the whole table as the source range.
2) Move the cursor to the end of the document - and set this as the destination range
3) Copy the table to the end of the document, and make this the new Source Range
4) Put the cursor back into the origional table, fill it in.
5) Put the cursor in the correct position below the one I;ve filled in
6) Set this as the new destination range and copy the blank table in.
7) |Continue through steps 4 to 6 until there is no more data to fill in
8) Delete the table at the end of the document!

Simple!

Here is an outline of the code:


Private Sub Form_Load()
Dim filename As String
Dim SaveFilename As String
Dim TheSourceRange As Word.Range, TheDestinationRange As Word.Range
Dim oCel As Cell

Dim LookFor As String

Set oApp = CreateObject("Word.Application")
With oApp
filename = "c:\temp\Catalogue Template.doc"
SaveFilename = "c:\Temp\Finished Report.doc"

.ScreenUpdating = True
.Visible = True
.Documents.Open filename:=Chr(34) & filename & Chr(34), _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:=""

CheckExistingFile SaveFilename
.ActiveDocument.SaveAs filename:=SaveFilename
With .Selection
' Create a Temp New Copy of the Table
.HomeKey Unit:=wdStory
LookFor = "<Table1>"
Find oApp, LookFor
.Tables(1).Select
Set TheSourceRange = oApp.Selection.Range.Duplicate

.EndKey Unit:=wdStory

Set TheDestinationRange = oApp.Selection.Range
TheDestinationRange.FormattedText = TheSourceRange.FormattedText
' Reset this new table as the new source
.Tables(1).Select
Set TheSourceRange = oApp.Selection.Range.Duplicate

' go back to the source table
.HomeKey Unit:=wdStory
Find oApp, LookFor

' Fill in the table
: : : :
: : : :
: : : :
: : : :
: : : :

'Put the cursor in place for the next table
Set TheDestinationRange = oApp.Selection.Range
TheDestinationRange.FormattedText = TheSourceRange.FormattedText

'Now we've finished with the temp table - delete it.
Find oApp, LookFor
.Tables(1).Select
.Tables(1).Delete
.HomeKey Unit:=wdStory

End With
End With
Set oApp = Nothing

End Sub


Again thank you for your help.

Shane Brennan


 
I am having a hard time following that.

Is the "new" table blank...or not?

< 60 50 working days until retirement
 
Hi Fumei

The table will be exactly the same as the origional table. The origional table, in my template, is multiple rows with mergecodes for photos which cover merged cells both vertically and horizontally.

Basically the template is a glorified mail merge, but instead of MSWord doing the merge itself, I have VB6 to do it, because there is information from various sources that need to go into seperate tables/sections of the document.

it's easy to copy and paste the table using .copy and .paste, but this is running on a server account which is used by the admin to copy/paste document, text, etc to and from the server - so any use of the clipboard would damage both systems.

I hope this make sense.




Shane Brennan


 
Hi Shane,

What you're now descibing sounds like a Directory/Catalogue merge. Provided your data source indicates which other files data should be imported from, you could probably do it without vba.

The other possibility, using vba, is to simply copy the table as often as needed from whatever template you're using as the document's base.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Macropod

The export is quite complicated and for some of the logic - it does need to be VBA unfortunately - especially as some of the logic requires colour coding of certain cells and text based on values.. it gets complicated, would be an easy Job if it wasn't for client requirements :)

The 2nd option is a good one - thank you for the tip, should also speed up pagination with duplicating the table the correct number of times.

Thanks again for your help, it's very appreciated.

Shane Brennan


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top