Consulting

Results 1 to 7 of 7

Thread: Solved: paste from clipboard and start find a word

  1. #1

    Solved: paste from clipboard and start find a word

    Hallo I need help.
    I have edited a dictionary and for search word into this dictionary, I when the file Dictionary.doc start it must copy from clipboard a word, paste the same word in Dictionary.doc and begin to search in Dictionary.doc the data pasted from clipboard.
    I edited this macro:
    File Dictionary.doc in VBA ThisDocument I have stored:
    [vba]
    Private Sub Document_Open()
    Application.Run MacroName:="ClipBoard_GetData"
    End Sub
    [/vba]
    In a new module of file Dictionary.doc I have stored:
    [vba]
    Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
    As Long
    Declare Function CloseClipboard Lib "User32" () As Long
    Declare Function GetClipboardData Lib "User32" (ByVal wFormat As _
    Long) As Long
    Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal _
    dwBytes As Long) As Long
    Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
    As Long
    Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
    As Long
    Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) _
    As Long
    Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
    ByVal lpString2 As Any) As Long

    Public Const GHND = &H42
    Public Const CF_TEXT = 1
    Public Const MAXSIZE = 4096

    Function ClipBoard_GetData()
    Dim hClipMemory As Long
    Dim lpClipMemory As Long
    Dim MyString As String
    Dim RetVal As Long

    If OpenClipboard(0&) = 0 Then
    MsgBox "Cannot open Clipboard. Another app. may have it open"
    Exit Function
    End If

    ' Obtain the handle to the global memory
    ' block that is referencing the text.
    hClipMemory = GetClipboardData(CF_TEXT)
    If IsNull(hClipMemory) Then
    MsgBox "Could not allocate memory"
    Goto OutOfHere
    End If

    ' Lock Clipboard memory so we can reference
    ' the actual data string.
    lpClipMemory = GlobalLock(hClipMemory)

    If Not IsNull(lpClipMemory) Then
    MyString = Space$(MAXSIZE)
    RetVal = lstrcpy(MyString, lpClipMemory)
    RetVal = GlobalUnlock(hClipMemory)

    ' Peel off the null terminating character.
    MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
    Else
    MsgBox "Could not lock memory to copy string from."
    End If

    OutOfHere:

    RetVal = CloseClipboard()
    ClipBoard_GetData = MyString

    End Function
    [/vba]
    I must insert in Dictionary.doc also a macro that in automatic paste the clipboard data and begin to search the pasted clipboard data in the file Dictionary.doc.
    I am not be able to make this macro please give me an help.
    pasquale
    Last edited by Pasquale; 11-30-2006 at 12:37 AM.

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Pasquale,

    I'm not quite sure what you're trying to do but this should paste the clipboard in your document when it opens ...

    Private Sub Document_Open()
    Selection.Paste
    End Sub
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    Hi Tony,
    thanks for replay.

    Your macro paste very good the clipboard data in my document (i have deleted my previous macro). But my problem isn't to paste in a document the clipboard data but to grab the clipboard data and at the same time start the search of the grabbed clipboard data while the document is opening?

    My problem is copy from clipboard generally one word or a phrase, and at the same time to start to find the word or a phrase while the document is opening.

    pasquale

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Pasquale,

    So you want the contents of the clipboard available in VBA, not in your document?

    I had to experiment a little to make this run without Word being a bit too clever, but I think it's OK now - see if it gets you started:

    [VBA]
    Sub Document_Open()
    With Selection
    .HomeKey Unit:=wdStory
    .TypeParagraph
    .HomeKey Unit:=wdStory
    .Paste
    .Find.Text = ActiveDocument.Range(0, .Start)
    With ActiveDocument
    .Undo 2
    .Saved = True
    End With
    .Find.Execute
    End With
    End Sub
    [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    Hi Tony,
    it works very well.

    Exscuse me Tony there is two question:
    1) When the clipboard is blank the macro crash and show error.

    2) it is possible also to be continued the search in the rest of document with the same data?


    thanks for help

    pasquale
    Last edited by Pasquale; 11-30-2006 at 02:13 PM.

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    1) You can trap the error ...

    [VBA]
    Sub Document_Open()
    With Selection
    .HomeKey Unit:=wdStory
    .TypeParagraph
    .HomeKey Unit:=wdStory
    On Error Resume Next
    .Paste
    If Err.Number = 0 Then
    .Find.Text = ActiveDocument.Range(0, .Start)
    ActiveDocument.Undo 2
    .Find.Execute
    Else
    ActiveDocument.Undo 1
    End If
    End With
    ActiveDocument.Saved = True
    End Sub
    [/VBA]

    2) You can use the browse buttons - below the vertical scrollbar
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    Hi Tony the macro works very very well.

    Thanks Pasquale

Posting Permissions

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