Solved

VB6 - Copy to clipboard all cells including headers row 0

Posted on 2014-04-05
8
327 Views
Last Modified: 2014-04-05
Hi all,

I have this code below that copy to clipboard the selected cells.

I have 2 things i would like to add to the below script.

1- I would like to auto select all cells from the grid automatically
2- I would like to also copy to clipboard the row o with the headers.

How can i do that?

Thanks again


  With MSHFlexGrid_pref_carrier
        .Redraw = False              'Find range to copy
        min_row = .Row: max_row = .RowSel
        If min_row > max_row Then Tmp = max_row: max_row = min_row: min_row = Tmp
        min_col = .Col: max_col = .ColSel
        If min_col > max_col Then Tmp = max_col: max_col = min_col: min_col = Tmp

        my_text = ""                   'Get the text
        For i = min_row To max_row
            For j = min_col To max_col
                my_text = my_text & .TextMatrix(i, j)
                If j < max_col Then my_text = my_text & vbTab
            Next j
            If i < max_row Then my_text = my_text & vbCrLf
        Next i
        .Redraw = True
    End With

    If my_text <> "" Then
        Clipboard.Clear
        Clipboard.SetText (my_text)
    Else
        Beep
        MsgBox "Please select some text.", vbExclamation, "Nothing selected!"
    End If

Open in new window

0
Comment
Question by:Wilder1626
  • 4
  • 4
8 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39980474
This copies all the text to the clipboard without user selection.

Dim strText As String
Dim intCol As Integer
Dim intRow As Integer

With MSFlexGrid1
    For intCol = 0 To .Cols - 1
        For intRow = 0 To .Rows - 1
            strText = strText & .TextMatrix(intRow, intCol) & " "
        Next
    Next
End With
Clipboard.Clear
Clipboard.SetText strText

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39980484
HI

i think we are almost there.

The result you gave me gives me the 2 column of data in 1 single column.
Ex:
ASD_RUN
01:00 Mon-Sat
04:30 Sun
10:45 (B) Sat
10:45 (A) Sun-Fri
12:50 (A) Sat
20:45 Sat
20:30 Sun-Fri
23:00 (A) Sun-Thu
23:00 (B) Fri
CARRIER_ID
ROBERT
ROBERT
ROBERT
ROBERT
ROBERT
ROBERT
ROBERT
ROBERT
ROBERT



When what i want, is really each column of same row should be one beside the other

ex:
01:00 Mon-Sat       ROBERT
04:30 Sun      ROBERT
10:45 (B) Sat      ROBERT
10:45 (A) Sun-Fri      ROBERT
12:50 (A) Sat      ROBERT
20:45 Sat      ROBERT
20:30 Sun-Fri      ROBERT
23:00 (A) Sun-Thu      ROBERT
23:00 (B) Fri      ROBERT
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39980580
Try this. You could also change line 9 to

strText = strText & .TextMatrix(intRow, intCol) & vbTab


Dim strText As String
Dim intCol As Integer
Dim intRow As Integer

With MSFlexGrid1
    For intCol = 0 To .Cols - 1
        For intRow = 0 To .Rows - 1
            If intRow <> .Rows - 1 Then
                strText = strText & .TextMatrix(intRow, intCol) & " "
            Else
                strText = strText & .TextMatrix(intRow, intCol) & vbCrLf
            End If
        Next
    Next
End With
Clipboard.Clear
Clipboard.SetText strText

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39980615
Looks like i'm still having some problem.

Here is what the grid looks like:
grid
When i copy ti clipboard, and when i will paste the date, it will give:

ASD_RUN      CARRIER_ID
01:00 Mon-Sat       ROBERT
04:30 Sun      ROBERT
10:45 (B) Sat      ROBERT
10:45 (A) Sun-Fri      ROBERT
12:50 (A) Sat      ROBERT
20:45 Sat      ROBERT
20:30 Sun-Fri      ROBERT
23:00 (A) Sun-Thu      ROBERT
23:00 (B) Fri      ROBERT
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39980631
I assume you got that result because you reversed lines 6 and 7 above but in any case try this. Change the "25" in line 6 to whatever looks best, and if column 0 could have a width greater than what you show we could calculate the value needed to show the data properly.

Dim strText As String
Dim intCol As Integer
Dim intRow As Integer

With MSFlexGrid1
    For intRow = 0 To .Rows - 1
        For intCol = 0 To .Cols - 1
            If intCol <> .Cols - 1 Then
                strText = strText & .TextMatrix(intRow, intCol) & Space(25 - Len(.TextMatrix(intRow, intCol)))
            Else
                strText = strText & .TextMatrix(intRow, intCol) & vbCrLf
            End If
        Next
    Next
End With
Clipboard.Clear
Clipboard.SetText strText

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39980660
wow, this is more then what i was wishing for

Thank you so much for your help
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 39980661
thanks again for your help
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39980679
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now