anyone knows how to copy picture or text and so, we can use ctrl+v paste on word or excel?
Printable View
anyone knows how to copy picture or text and so, we can use ctrl+v paste on word or excel?
Huh?
copy the picture , something like ctrl +C in own vb program then we can go to word or excel and then use CTRL+ V paste it, i think is something about clib
try to use sendkeys()
if it's text its:
Clipboard.SetData "Your Text Here", or
Clipboard.SetData yourStringHere
that should take along any formatting to the text as well.
HOw about picture?
and how can i copy the data in a grid and paste it to excel or other grid?
Here is how you can copy Selected Data from MS Flex Grid to the clipboard.
To copy Picture to the Clipboard use the following codeCode:
Dim startCol As Integer
Dim endCol As Integer
Dim startRow As Integer
Dim endRow As Integer
Dim i As Integer
Dim j As Integer
Dim strData As String
Dim temp As Integer
With MSFlexGrid1
startCol = .Col
endCol = .ColSel
If startCol > endCol Then
temp = endCol
endCol = startCol
startCol = temp
End If
startRow = .Row
endRow = .RowSel
strData = ""
For i = startRow To endRow
For j = startCol To endCol
strData = strData & .TextMatrix(i, j) & " "
Next j
strData = strData & vbCrLf
Next i
End With
'MsgBox strData
Clipboard.Clear
Clipboard.SetText (strData)
MsgBox "Data has been copied to the Clipboard", vbInformation
Clipboard.SetData Picture1.Picture
Hope this helps
Danial
we can highlight some cells (a portion of it) , can we just copy those, instead of the whole grid?
Danial:
Private Sub Command1_Click()
MSFlexGrid1.Rows = 3
MSFlexGrid1.Cols = 3
MSFlexGrid1.TextMatrix(1, 1) = "hi"
MSFlexGrid1.TextMatrix(2, 2) = "what?"
End Sub
Private Sub Command2_Click()
your code
end sub
then i go to excel and paste... it doesn't work good...
want to give a hand?
Hi Dragonyian,
Try this code, i just modified it so that u can copy it to an excel file, infact my code will copy the Data from selected Cells to an Excel file .
Hope this helps.
Danial
Code:'Add a Reference to "Microsoft Excel xx Object Library".
Function copyData()
Dim startCol As Integer
Dim endCol As Integer
Dim startRow As Integer
Dim endRow As Integer
Dim i As Integer
Dim j As Integer
Dim strData As String
Dim temp As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
' Assign object references to the variables. Use
' Add methods to create new workbook and worksheet
' objects.
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
With MSFlexGrid1
startCol = .Col
endCol = .ColSel
If startCol > endCol Then
temp = endCol
endCol = startCol
startCol = temp
End If
startRow = .Row
endRow = .RowSel
strData = ""
For i = startRow To endRow
For j = startCol To endCol
xlSheet.Cells(i, j).Value = .TextMatrix(i, j)
Next j
Next i
End With
xlSheet.SaveAs "C:\test.xls"
MsgBox "The selected data has been copied to the file C:\test.xls", vbInformation
' Close the Workbook
xlBook.Close
' Close Microsoft Excel with the Quit method.
xlApp.Quit
' Release the objects.
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
End Function
Private Sub Command1_Click()
Call copyData
End Sub
danial
first, thanks reply for me
i try to avoid open excel, because some machine might not have excel, so i modify your codes
and insert a excel sheet objext
strData = ""
For i = startRow To endRow
For j = startCol To endCol
Sheet1.Cells(i, j).Value = .TextMatrix(i, j)
Next j
Next i
but it doesn't take the sheet1.cells function, wonder why?
again, thanks
Hi,
I am a little bit confused, i am not sure what exactly you are trying to do.
My first Pice of code copies the Selected Cells of an MSFlexGrid to the Clipboard. You need to select some cells/rows/columns before u call the function.
But the problem with this method is that the data copied to the Clipboard is a long String and when you paste this to Excel it only pastes it into one cell.
Thats why i gave you the second method which saves the selectd Data to an Excel file.
You need to declare Sheet one as an Excel WorksheetQuote:
but it doesn't take the sheet1.cells function, wonder why?
e.g.
Code:Dim Apps As Excel.Application
Dim Sheet1 As Excel.Worksheet
Dim Book As Excel.Workbook
Set Apps = New Excel.Application
Set Book = Apps.Workbooks.Add
Set Sheet1 = Book.Worksheets.Add
Sorry to catch up so late....
what iwas trying to do is copy the data on the grid... so, the user can paste it later, the user might paste it to excel (but not necessary), because the user might paste it back to VB GRID .
I avoid to use open excel object , because some machine might not having execl installed.... so i try to use Excel OCX, .... there is a Sheet ocx we can use in vb... but somehow, it can not work.... like the problem i mentioned above..
Ya, you are right, clipboard write them as string, so, it will not take empty cell.... wonder any way to do that
Once again, thanks for the hint that you gave
Anyone knows how can we copy the contains in the fixed rows or column?
Please i tried to use the codes above but can't copy the data from teh fixed rows or cols....
Help ie appreciated
Hi,
Here is how you can copy the contents of fixed column from a MSFlexGrid Control.
Hope this helps.
Danial
Code:Private Sub Command1_Click()
Dim sString As String
Dim iStart As Integer
Dim iEnd As Integer
iStart = MSFlexGrid1.FixedCols
iEnd = MSFlexGrid1.Cols - 1
sString = ""
For i = iStart To iEnd
sString = sString & " " & MSFlexGrid1.TextMatrix(0, i)
Next i
'MsgBox sString
Clipboard.Clear
Clipboard.SetText (sString)
End Sub
I'm sorry people but why are you all using Clipboard.Settext when he want's to place pictures?
Use
Clipboard.SetData Picture1.Picture
instead.
it wouldn't be that hard to set the focus to Excel and then sendkeys(^V, True) to it.
Jop:
I am not sure the method that you are referring to... can you make a small example? then everyone can try your method.. as i thought settext is what others are using now..thanks.....
And how about pasting? we talk about copying and hope can we paste thing into Grid?
hmmm everybody is using settext but you'd better use SetData
hmmm I made a simple example with notepad, you can change it to excel and the grid..
If you need help finding excel, just ask :)
Code:'Put a commandbutton and a textbox on your form
Private Sub Command1_Click()
Shell "notepad.exe", vbNormalFocus
Clipboard.SetText Text1.Text
SendKeys "^V", True
End Sub
Actually CTRL+C and CTRL+V are built into windows soooooo they work automatically in itself. Unless you manually want to send that data.
Jop , Chris_SE and others
Thanks for the reply....
I am using the Grid Control.... so it is kind of messy around here... i try to copy stuff from excel and paste them to Grid...
But i don't know how can we send them to each cells.. the example from MSDN or other articles are talking about single cell.... so not sure i can do it ....
Anyone have any idea why this will work if you Shell to Notepad, but will not work if you shell to Word?Quote:
Originally posted by Jop
hmmm everybody is using settext but you'd better use SetData
hmmm I made a simple example with notepad, you can change it to excel and the grid..
If you need help finding excel, just ask :)
Code:'Put a commandbutton and a textbox on your form
Private Sub Command1_Click()
Shell "notepad.exe", vbNormalFocus
Clipboard.SetText Text1.Text
SendKeys "^V", True
End Sub