I want to paste some text in clipboard into Ms Word. I wrote this following code

a:=findWIndow(pchar('OpusApp'),nil);
 b:=FindWindowEx(a,0,pchar('_WwF'),nil);
 c:=FindWindowEx(b,0,pchar('_WwB'),nil);
 d:=FindWindowEx(c,0,pchar('_WwG'),nil);
 SendMessage(d,wm_paste,0,0);

and it didn't work.

But i wrote the code below to paste into Wordpad,and it works fine

a:=findwindow(pchar('WordPadClass'),nil);
 b:=FindWindowEx(a,0,pchar('RICHEDIT50W'),nil);
 SendMessage(b,wm_paste,0,0);

Am I made some mistake in Ms Words' window structure? The structure above is what i got using Winspector.I'm currently using Ms Word 2007.

Does anyone have solution?

Thanks

Dani AI

Generated

Good call, — and sensible nudge from . Poking Word's top-level window with WM_PASTE is fragile: Word's editor is not a simple standard child control, the internal class names vary across versions, and cross-process messages often don't reach the actual editable object. Using Word's object model (OLE/COM) is the correct, future-proof approach.

Practical checklist to make your automation robust: initialize COM on the thread as a single-threaded apartment; obtain the Word application object from the Running Object Table (or create one if needed); confirm there is an active document and an active Selection or Range; perform the paste via the object model (or insert text directly into a Range to avoid the clipboard); and finally uninitialize COM. Prefer PasteSpecial when you need a specific format, or use Range.InsertAfter/InsertBefore if you can bypass the clipboard entirely.

Troubleshooting notes and pitfalls: GetActiveObject can fail if Word is running elevated or under another account, so handle that case and fall back to creating a new instance or match the foreground process by PID/name (WINWORD.EXE) before sending simulated input. Protected View, modal dialogs or document protection can block programmatic paste operations. Automating Office from services or unattended server processes is unsupported by Microsoft and will behave poorly. If automation is impossible, use a foreground-sent Ctrl+V (SendInput) only after ensuring the correct window is focused and saving/restoring the clipboard.

Pascal/Delphi tips: use the ComObj/unit for late binding or import the Word type library for early binding; call the COM init/uninit on the same thread; wrap COM calls in try/except and check Documents.Count before acting. These practices make the solution reliable across Word 2007 and later and avoid brittle window-class hacks.

Recommended Answers

All 2 Replies

I don't use Word, so I can't help... but what you are doing is unlikely to work well anyway because the window class names can change between versions of Word... But I think you should be looking for a RichEdit class...

I think you ought to look at OLE automation.
Here's a page all about word

Search down near the bottom in "How to automate word" for "paste".

The other option would be to send the proper keyboard sequences to Word to access the Edit|Paste menu item.

Sorry I can't be of more help.

I've found a solution in Word VBA reference:

V := GetActiveOleObject('Word.Application');
V.ActiveDocument.ActiveWindow.Selection.Paste;

All i have to do is to check whether the foreground window is Ms Word or not, using GetClassName and GetForegroundWindow.

Problem solve. After all, thanks to you guys...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.