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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy an active form and paste into a Word document

API Functions

How to copy an active form and paste into a Word document

by  Mike Gagnon  Posted    (Edited  )
The following will copy the content of an active form into the clipborad and paste it into a Word document. It could also be used to copy and translate into a bitmap.
Code:
LOCAL oWord as word.application
#DEFINE CF_BITMAP   2	
#DEFINE SRCCOPY     13369376 
DO copyActiveWindow
PRIVATE hwnd, lnLeft, lnTop, lnRight, lnBottom, lnWidth, 
lnHeight
hwnd = GetFocus() 
STORE 0 TO lnLeft, lnTop, lnRight, lnBottom, lnWidth, 
lnHeight
= getRect (@lnLeft, @lnTop, @lnRight, @lnBottom, @lnWidth, 
@lnHeight)
hdc = GetWindowDC (hwnd)  
hVdc = CreateCompatibleDC (hdc)
hBitmap = CreateCompatibleBitmap (hdc, lnWidth, lnHeight)
= SelectObject (hVdc, hBitmap)  
= BitBlt (hVdc, 0,0, lnWidth,lnHeight, hdc, 0,0, SRCCOPY)
= OpenClipboard (hwnd)
= EmptyClipboard()
= SetClipboardData (CF_BITMAP, hBitmap)
= CloseClipboard()  
= DeleteObject (hBitmap)
= DeleteDC (hVdc)
= ReleaseDC (hwnd, hdc)
oWord=CREATEOBJECT("word.application")
oWord.Documents.Add()
loSelection=oword.Selection
loselection.PasteAndFormat(2)
oword.Visible =.t.
RETURN		

PROCEDURE  copyActiveWindow   
	DECLARE INTEGER GetWindowRect IN user32 INTEGER 
hwnd, STRING @ lpRect 
	DECLARE INTEGER SelectObject IN gdi32 INTEGER hdc, 
INTEGER hObject
	DECLARE INTEGER ReleaseDC IN user32 INTEGER hwnd, 
INTEGER hdc 
	DECLARE INTEGER CreateCompatibleDC IN gdi32 
INTEGER hdc
	DECLARE INTEGER DeleteObject IN gdi32 INTEGER 
hObject
	DECLARE INTEGER DeleteDC IN gdi32 INTEGER hdc
	DECLARE INTEGER CloseClipboard IN user32 
	DECLARE INTEGER GetFocus IN user32 
	DECLARE INTEGER EmptyClipboard  IN user32 
	DECLARE INTEGER GetWindowDC IN user32 INTEGER hwnd 
	DECLARE INTEGER OpenClipboard IN user32 INTEGER 
hwnd 
	DECLARE INTEGER SetClipboardData IN user32 INTEGER 
wFormat, INTEGER hMem

	DECLARE INTEGER CreateCompatibleBitmap IN gdi32;
		INTEGER hdc, INTEGER nWidth, INTEGER 
nHeight

	DECLARE INTEGER BitBlt IN gdi32;
		INTEGER hDestDC, INTEGER x, INTEGER y,;
		INTEGER nWidth, INTEGER nHeight, INTEGER 
hSrcDC,;
		INTEGER xSrc, INTEGER ySrc, INTEGER dwRop
RETURN		
PROCEDURE  getRect(lnLeft, lnTop, lnRight, lnBottom, 
lnWidth, lnHeight)
    LOCAL lpRect 
    lpRect = Repli(Chr(0), 16)  
    = GetWindowRect (hwnd, @lpRect)  
    lnLeft   = buf2dword(SUBSTR(lpRect,  1,4))  
    lnTop    = buf2dword(SUBSTR(lpRect,  5,4))  
    lnRight  = buf2dword(SUBSTR(lpRect,  9,4))  
    lnBottom = buf2dword(SUBSTR(lpRect, 13,4))  
	lnWidth  = lnRight - lnLeft
	lnHeight = lnBottom - lnTop
RETURN
FUNCTION  buf2dword (lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
	Asc(SUBSTR(lcBuffer, 2,1)) * 256 +;
	Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +;
	Asc(SUBSTR(lcBuffer, 4,1)) * 16777216

[sub]Parts of this code was borrowed http://www.news2news.com/vfp/[/sub]

Mike Gagnon
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top