Results 1 to 10 of 10
  1. #1
    Lounger
    Join Date
    Jan 2003
    Location
    Brisbane, Queensland, Australia
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Office API controlling Clipboard (Microsoft Office 2003)

    While my program accesses the clipboard, what the user put on it is inevitably destroyed. I noticed that Office allows the user to access its quite a few clipboards. If I can use one of them, this situation can be avoided. However, I dispointedly found no corresponding API. It is weird that the multi-clipboard feature has been there for several versions, Microsoft still does not provide API to access it programmatically. Do experts here know any means to access these Office clipboards? Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    There is no programmatic access to the Office Clipboard. A clunky Windows API method to clear the Office Clipboard has been posted in the newsgroups, but nothing to access individual items, as far as I know.

    Perhaps you can use the DataObject object from the MSForms library. The easiest way to create a reference to this library is to insert a userform. Even if you then delete the userform, the reference will remain set. You can type DataObject in the Visual Basic Editor and press F1 to get help. Look at the SetText and GetText methods.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Office API controlling Clipboard (Microsoft Of

    How does your program access the clipboard, and what type of data are you trying to preserve from deletion or replacement? If it's text or HTML, you could use Windows API calls to safely copy that data from and restore that data to the clipboard after you use it. However, I've never worked with any other type of clipboard data.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    Hi,

    Could you perhaps explain what it is that your program does with the clipboard?

    Are you by any chance creating a custom toolbar with custom button faces? If so, note that Office 2003 has a way to do that without the use of pasteface, thus circumventing the clipboard entirely (check out the Picture and Mask properties).
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  5. #5
    Lounger
    Join Date
    Jan 2003
    Location
    Brisbane, Queensland, Australia
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    My program moinitors the user's actions in the Word document, and tries to get the what the user has inserted in the document (e.g. rich formated text, clip arts or shapes). The only way I currently know to get data out of a Word document is to copy it to the clipboard and read it in RTF format. Now copy is done with Word API (e.g. Range.Copy), and clipboard reading is done with Windows API (i.e. the DataObject).

    In this way, I can only use the first clipboard, which overlaps with the Windows clipboard. So I have to backup the clipboard content before copying and restore it afterwards. Since Word has so many clipboards, I feel my current approach a waste.

    After all, my final purpose is to get part of the document (e.g. a Range) out of the document (e.g. a RTF string). If there are other ways to do it, I can avoid using the clipboard.

    Thanks.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    One possible approach is to use <code>ActiveDocument.HTMLProject.HTMLProjectItems( 1).Text</code> - this returns a text string containing the entire document formatted as HTML.

    See Working with the HTMLProject object for more information.

    StuartR

  7. #7
    Lounger
    Join Date
    Jan 2003
    Location
    Brisbane, Queensland, Australia
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    Thank you, StuartR.

    This approach is able to provide me the whole document. However, what I want is only a small section of it. For example, the shape the user just created. With a whole document in HTML, I have to integrate a HTML parser to get the correct content. And for a long document, getting the whole document might be slow.

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    It is fairly easy to use a temporary document to get just the range you want, without disturbing the clipboard. But you would need to parse the html, or save it as an html document. What do you need to do with the output?

    StuartR
    <code>
    Public Function GetHTML(rngIn As Range) As String
    Dim docNew As Document
    Set docNew = Application.Documents.Add
    docNew.Content.FormattedText = rngIn.FormattedText
    GetHTML = docNew.HTMLProject.HTMLProjectItems(1).Text
    docNew.Close SaveChanges:=False
    Set docNew = Nothing
    End Function

    Sub TestIt()
    Debug.Print GetHTML(ActiveDocument.Paragraphs(2).Range)
    End Sub
    </code>

  9. #9
    Lounger
    Join Date
    Jan 2003
    Location
    Brisbane, Queensland, Australia
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    Thank you very much, StuartR. This solved the problem that has puzzled me for months.

    I save the user input into a file and later replay it to Word to simulate an editing session, so I need the content to be self-contained and can exist independent of Word. With the HTML text and Selection information, I can achieve this purpose now.

  10. #10
    Lounger
    Join Date
    Jan 2003
    Location
    Brisbane, Queensland, Australia
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office API controlling Clipboard (Microsoft Office 2003)

    Hi, StuartR,

    The approach works perfect when I get things out from a document. However, when I tried to put data back into the document, problems occurs.

    When I tried to put the data into the temporary document with

    Document.HTMLProject.HTMLProjectItems(1).Text = Something

    Data do not appear in the document (neither in the interface or from the API) until I call Document.HTMLProject.RefreshDocument. The RefreshDocument method has a series of sideeffect. First, the refreshing process is slow. Second, I hid the temporary document before, but this function call make it appear. Although I can hide it again, the flashing effect is undesirable. Finally, after this method is called, the Document object becomes unavailable. I have to get this object back, which is troublesome.

    Any suggestion is appreciated.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •