I am using OLE automation to edit a MS Word document that includes tables.
I can duplicate a table by:

# code
import win32clipboard, traceback, sys, win32com.client
app = win32com.client.Dispatch("word.application")
app.visible = 1
d = app.documents.Open('c:\\table.doc')
d.tables(1).range.Copy()
d.Range(0,0).Paste() # pastes table
# endcode

Now I want to save the clipboard contents and restore it later, so I tried:
# code
# get all formats and corresponing data
win32clipboard.OpenClipboard()
format = 1
formats = []
data = []
while 1:
format = win32clipboard.EnumClipboardFormats(format)
if not format:break
try:
datum = win32clipboard.GetClipboardData(format)
formats.append(format)
data.append(datum)
except:
print format, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)
# an error happens getting format 3: "6, 'GetClipboardData:GetMetafileBitsEx(NULL)', 'The handle is invalid.'
# 3 = CF_METAFILEPICT
win32clipboard.EmptyClipboard()
win32clipboard.CloseClipboard()


# write the data back to the clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
for i in range(len(data)):
try: win32clipboard.SetClipboardData(formats[i], data[i])
except: print formats[i], traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)
# an error happens setting format 14: 6, 'SetClipboardData', 'The handle is invalid.')
# 14 = CF_ENHMETAFILE
win32clipboard.CloseClipboard()
d.Range(0,0).Paste() # ONLY Pastes text!
# endcode


Do you know what's going on with formats 3 and 14. It would appear that one or both are required in order for the table to paste correctly.

Bob Gailer
mailto:[EMAIL PROTECTED]
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.454 / Virus Database: 253 - Release Date: 2/10/2003

Reply via email to