No announcement yet.

Word8 document to clipboard

  • Filter
  • Time
  • Show
Clear All
new posts

    Word8 document to clipboard

    Compiler PBDLL60
    Does anyone have an example of how to copy a Word-document (Word 97) to the
    clipboard from within an application? Using the %CF_TEXT format does not
    work. The Word-format is obviously more complicated. Does Windows support a
    default clipboard-format for Word-documents or do you need to register it,
    using "RegisterClipBoardFormat"? And if the latter is the case, how?

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    *** Opinions expressed here are not necessarily untrue ***

    Need to see some code to be able to give a good answer, but maybe
    you could try with: lCopy& = SendMessage(hWnd, %WM_COPY, 0, 0)

    This works fine with the Richedit control's RTF format, so hopefully
    it also works will work with MS Word's different formats..


      There's more complexity involved with these kind of formats.

      To my knowledge, it's possible that Word clipboard's a 'link' instead of it's data.
      When you paste it, the app wich holds the data 'renders' the data into the new area.
      Search for rendering.
      This means if you have large BMP's for example, it's more clever to copy a simple handle and if you need the data, give a message to the app wich holds the data.

      This is because if you DO have large stuff for the clipboard, it never might be pasted but still would consume a lot of mem.


        Win98SE: I opened Word97 document, selected a text and copied it to clipboard by Ctrl-C. Then tried two variants: in VB ClipBoard.GetText; in PB - sending Ctrl-V to TextBox. All works like it should be:

        #Compile Exe
        #Register None
        #Include "WIN32API.INC"
        Global hDlg&
        CallBack Function But_CallBack()
        Control Set Focus hDlg&, 201
        scan1& = MapVirtualKey(%VK_CONTROL, 0)
        scan2& = MapVirtualKey(86, 0)
        KeyBd_Event %VK_CONTROL, scan1&, 0, 0: ApiSleep 20
        KeyBd_Event 86, scan2&, 0, 0: ApiSleep 20
        KeyBd_Event 86, scan2&, %KEYEVENTF_KEYUP, 0: ApiSleep 20
        KeyBd_Event %VK_CONTROL, scan1&, %KEYEVENTF_KEYUP, 0: ApiSleep 20
        End Function

        Function PbMain () As Long
        Dialog New 0, "Hello",,, 540, 250, %WS_SYSMENU Or %WS_CAPTION Or %WS_OVERLAPPEDWINDOW, 0 To hDlg&
        Control Add Button, hDlg&, 101, "Paste", 50, 10, 100, 15 Call But_Callback
        Control Add TextBox , hDlg&, 201&, "" , 10, 30, 520, 200, %ES_AUTOVSCROLL Or %ES_WANTRETURN Or %ES_MULTILINE, %WS_EX_WINDOWEDGE Or %WS_EX_CLIENTEDGE
        Dialog Show Modal hDlg&
        End Function

        [This message has been edited by Semen Matusovski (edited January 09, 2000).]


          Hi Folks,

          Thanks to everyone who provided a reply, although it did not help much. Maybe I did'nt make myself clear enough for good understanding? Therefore, let me tell the 'whole' story.

          In the newsroom we are using a Word97-based word processor. Sometimes our correspondents provide us with articles in plain ASCII. In order to be able to process those texts without a lot of redundant editor's work, I've made a conversion table (within an application) that translates accented characters in accordance with the codeset used by the word processor, including begin en end commands for bold and/or italic. After saving the converted article (with a proper filename extension) and then opening it in the word processor, it looks exactly the way we want.

          So far, so good. But things would become a lot easier if we were able to copy the file on the fly to the Windows Clipboard. Then the user simply can do a 'paste' (Ctrl/v) in the word processor. The problem is that the pasted text does not appear in the correct format, most likely because the clipboard format for Word documents is a bit more complicated than %CF_TEXT alone (as E.B. Knoppert already states here).

          The following maybe of interest. When I copy a piece of formatted text to the clipboard using the editor itself (select all + Ctrl/C) and then view the clipboard, Clipbrd.exe shows up with an interesting Display-menu. Many formats have been grayed here, except Text, OEM Text and (surprise, surprise(!) Picture. By default the viewer selects text and initially the content of the clipboard looks as bad as described above, but as soon as the Picture option has been selected, the text in the clipboard 'receives' the correct formats. Is this a known phenomenon for more experienced coders, perhaps?

          Finally, how does the app. copy text to the clipboard? Here is the code (found in the PowerBASIC Source Code forum). Note: the calling module is modified for this example.
          #COMPILE EXE
          #INCLUDE "WIN32API.INC"
          FUNCTION WriteToClipBoard(BYVAL content AS STRING) AS LONG
            LOCAL where AS LONG, hData AS LONG, bytes AS LONG
            content = content + CHR$(0)
            bytes = LEN(content)
            IF bytes < 2 THEN EXIT FUNCTION
            hData = GlobalAlloc(&H2002, bytes)                 ' get memory of clipboard
            where = GlobalLock(hData)                          ' lock it
            POKE$ where, content                               ' paste text into memory
            GlobalUnlock hData                                 ' unlock memory
            IF ISFALSE OpenClipboard(0) THEN                   ' if clipboard isn't available
              GlobalFree hData                                 ' free up memory
              FUNCTION = 0
              EXIT FUNCTION
            END IF
            EmptyClipboard                                     ' empty whatever's in there now
            SetClipBoardData %CF_TEXT, hData                   ' %CF_TEXT = wrong format for Word docs
            CloseClipboard                                     ' release clipboard
            FUNCTION = -1                                      ' success
          END FUNCTION
            OPEN "C:\AUTOEXEC.BAT" FOR BINARY AS #1            ' use a formatted word doc instead
            SEEK #1, 0
            GET$ #1, LOF(1), content$
            dummyRetVal& = WriteToClipBoard(content$)
            CLOSE #1
            ' error trap left out here 
          END FUNCTION

          Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)

          *** Opinions expressed here are not necessarily untrue ***


            Egbert - could be it's easy to create macro ? VBA is very powerful.