locked
Copying text from table to clipboard, word RRS feed

  • Question

  • Hi

    I have to create macro, which puts button in a table and copies value from previous cell (same column, previous row) to clipboard. The thing is, there may be 10-20 or even more buttons, depends on document. I have no experience in vba

    This is what I've found so far

    Module 1:          

    Sub CreateButton()
        'Add a command button to a new document
        Dim doc As Word.Document
        Dim shp As Word.InlineShape

        Set doc = ActiveDocument

        Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
        shp.OLEFormat.Object.Caption = "Kopiuj"


        'Add a procedure for the click event of the inlineshape
        '**Note: The click event resides in the This Document module
        Dim sCode As String
        sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
                "  Call SetClipBoardText()" & vbCrLf & _
                "End Sub"
        doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

    End Sub

    Module 2

    Public Sub SetClipBoardText()
          Dim MyData As DataObject
          Set MyData = New DataObject
          Dim TableText As String
          On Error GoTo NotText

          TableText = ActiveDocument.Tables(1).Cell(Row:=4, Column:=3).Range.Text
          ' Get data from the clipboard.
          MyData.SetText (TableText)
          MyData.PutInClipboard

          ' Assign clipboard contents to string variable.

    NotText:
          If Err <> 0 Then
             MsgBox "Data on clipboard is not text."
          End If
          End Sub

    Problems with the code:

    1. Unfortunately, when I run macro, button is created AND value is copied at the same time. So I get the value before clicking button. But when I copy something else and click button it copies proper value so code seems ok

    2. Now buttons are created on the top of page also copied value is "hard coded"

    How can I draw button in specified cell in the table?

    If for example I could place button in Row x Col y, then I could condition copy value by Row x-1, y, which would solve second problem

    3. Whole concept of string building click event seems weird - is there another way to do this, considering I have no idea how many buttons will be needed?

                      
    Tuesday, February 21, 2012 3:01 PM

All replies

  • How about simplifying it and forget the buttons: just assign a shortcut key that copies the cell above the one where the cursor is?

    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management

    Tuesday, February 21, 2012 8:14 PM
  • Unfortunately I can't do that this way

    But the problem changed

    I'm receiving word document with created x buttons

    I have to determine which button was clicked, get its name and iterate through Fields collection to find Field of exact name and copy it to clipboard

    Right now I'm struggling with determining which button was clicked, also it seems there is no such property as "Name", When I double click on button in design mode it creates somename_Click event, but I can't find out what name this button has

    Any ideas?

    Wednesday, February 22, 2012 10:56 AM
  • I am not quite sure what you are trying to accomplish.

    Do you want to click on a button, and based on that Button, get the name of that Button and copy content from a Field collection which name matches and copy that to a Clipboard?


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer as an answer when your question is being answered.

    Sunday, March 11, 2012 3:50 AM