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!

Creating objects on the clipboard

Status
Not open for further replies.

MigrantFirmWorker

Programmer
Apr 9, 2003
54
US
My goal is to create a Word table from Excel and place it on the clipboard as gracefully as possible.

My first approach was to create the table without actually placing it in a document but I ended up having to build the table in an open document, then cut it to the clipboard.

Is there a way to create a table object without opening a Word document?
If I must create a temporary doc to build the table, how can I close it without having the Save dialog open?

Thanx.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 



hi,

What's the business case for using Excel to create a Word table and then copy to Clipboard?

Then what?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I do a lot of structural analysis reports, with lots of tables of data from large workbooks. Copying the tables as embedded objects yields large file sizes and usually corrupts the Word document. Copying the tables as pictures works well for presentation but is not editable. Word tables have proven the most effective but they are a pain to format.
I've written the code to duplicate the selected cells from Excel into a Word table that can be pasted as required. It works quite well with the exception of the issues I'm trying to resolve.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 



How about creating a MACRO???

Repetative, complex... THAT is what this tool is meant to do!!!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


use your macro recorder.

Post back with your recorded code to customize.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I am using a macro. I just want to avoid having to deal with the dummy Word document that I am currently creating.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 


Well you need to create a Word Application Object and a Work Document Object before you can create anything contained within those objects.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Why not bypass the whole clipboard and build the table dynamically from the excel table? In this way you can control the process. Have code in the word template to "Insert Table from Excel Range" into the selected location. This would

1)Provide you the ability to select any excel range
2)Read the number of rows and columns in the range
3)create a table in Word at correct location with correct rows and columns
4)loop the range and get correct format per excel column and apply to word table
5)Fill the word table cell by cell
6)Automate all formatting so all tables are formatted correctly, and take out the leg work.

I build a lot of word templates that read database information and populate word documents. With this approach you can format the tables to look exactly the way you want.

You said "effective but they are a pain to format". Not sure what you mean because word tables are very easy to format. Maybe you are referring to the code.
 


Stated in a very direct and specific manner!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanx MajP.
I'm currently using your approach except that I am initiating from the Excel side. The goal was to be able to set up my table in Excel, click my macro button, and have a pastable Word table on the clipboard, independently of the intended destination.
I have been building the table in a dummy document to avoid any chance of the code screwing up an existing document (I can undo a paste operation. I can't undo a macro run).
Is there any way to close the dummy doc without the SaveAs dialog hijacking my session?

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 

"Is there any way to close the dummy doc without the SaveAs dialog hijacking my session?"

You can try:
[tt]
objWord.DisplayAlerts = False[/tt]

That should by-pass the Save of SaveAs dialog.

Have fun.

---- Andy
 
Sorry I misread your post. I thought you were copying from excel, but you are building the table. You cannot build a disconnected table object, but I would think if you keep the temp document invisible it would appear as if you did.

Although you cannot undo the creation of the table. I would think you would be safe to create directly in the master. Not sure if you gaining much safety with the intermediate step of building and pasting. You would probably want to add a Save first prior to creating the table. But everything would be contained in the new table so you could just delete it.
 
Thanx Andy. Unfortunately it didn't suppress the SaveAs dialog.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 
I found it. Telling Word that the document is already saved renders the SaveAs operation irrelevant and suppresses the dialog.

tempDoc.Saved = True

Thanx to everyone for your suggestions.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 


Code:
    Dim oWd As Word.Application, oDoc As Word.Document
    
    Set oWd = New Word.Application
    
    Set oDoc = oWd.Documents.Add
    
    oWd.Visible = True
    
    oDoc.Range.Text = "hello"
    [b]
    oDoc.Close False
    [/b]
    Set oDoc = Nothing
    oWd.Quit
    Set oWd = Nothing


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Even better! Thanx.

Chris

-------------------------------------------------------------
"Don't be deceived. We're all temporary employees.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top