PDA

View Full Version : How to Determine the DataType from Clipboard



fionabolt
07-27-2007, 07:40 AM
Is there a way I can determine the datatype of the contents of the clipboard?

I have created a Word template that creates a document with document protection applied so that users can only edit areas with a particular style. My problem is that users frequently cut and paste between documents.

If they perform a straight paste, then text is pasted into the document in a paragraph style that will become protected by a later routine.

I have intecepted Sub EditPaste() so that it forces the wdDialogEditPasteSpecial. However, users may still select the wrong datatype. (Especially they may select Formatted Text, when what we want them to select if Unformatted Text).

I then tried intercepting Sub EditPasteSpecial() so that it just performed a PasteSpecial DataType:wdPasteText.

However, this was no good, because if they had copied a picture, the picture didn't paste, and if they had copied a table, only the text and not the table came into the document.

Therefore, I am assuming that if I can termine the datatype of the clipboard I would use select case to determine the type of pastespecial to apply.

Many thanks

fionabolt
08-01-2007, 12:37 AM
Can anyone help me with this? I have searched the web but can't find anything that will let me determine the datatype of the contents of the clipboard.

To overcome my problem I have written this which seems to work, but isn't exactly the solution I was hoping for:
'++*******************************************************************
' Procedure: EditPaste
'* July 2007
'*
' Modified:
' Description: Intercepts the Paste function
' checks if the insert point includes a car return
' and then invokes EditPasteSpecial
' Parameters: N/A
' Returns:
'--********************************************************************
Sub EditPaste()
Dim selStr As String
selStr = Selection.Range.Text
'make sure the slection does not contain a carriage return
If Right(selStr, 1) = Chr(13) Or Right(selStr, 1) = Chr(10) Then
Selection.SetRange Selection.Range.Start, Selection.Range.End - 1
End If

'users should always use EditPasteSpecial in this template
Call EditPasteSpecial

End Sub
'++*******************************************************************
' Procedure: EditPasteSpecial
'* July 2007
'*
' Modified:
' Description: Intercepts the PasteSpecial function
' checks if the insert point includes a car return
' and then
' ascertains if the object to be inserted is text
' if it is text, unicode or html, then inserts the object
' in the format of the surrounding style
' if not, then invokes the pastespecialdialog box
' Parameters: N/A
' Returns:
'--********************************************************************
Sub EditPasteSpecial()
Dim selStr As String
Dim TxtStr As String
Dim myData As DataObject
Dim dlgE As Dialog
Dim dlgEResponse As Long
Dim MsgRes As Long
Set myData = New DataObject

selStr = Selection.Range.Text
'make sure the paste will not overtype the paramarker
If Right(selStr, 1) = Chr(13) Or Right(selStr, 1) = Chr(10) Then
Selection.SetRange Selection.Range.Start, Selection.Range.End - 1
End If
Set dlgE = Application.Dialogs(wdDialogEditPasteSpecial)
'convert selection to a string
myData.GetFromClipboard
If myData.GetFormat(1) = False Then
'entry in clipboard is not a string
GoTo ShowDialog
End If
TxtStr = myData.GetText
checkforCarRet:
'check if the string to insert finishes with a car return
If Right(TxtStr, 1) = Chr(13) Or Right(TxtStr, 1) = Chr(10) Or Right(TxtStr, 1) = Chr(32) Then
TxtStr = Left(TxtStr, Len(TxtStr) - 1)
GoTo checkforCarRet 'check for multiple instances of car return
Else:
End If

ShowDialog:
Select Case dlgE.datatype
Case "Text", "Unicode", "HTML"
'if the entry in the clipboard is either text, unicode or HTML
'just go ahead and paste it anyway
Selection.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis)
GoTo L99
Case Else
dlgEResponse = dlgE.Display
If dlgEResponse = -1 Then
Select Case dlgE.datatype
Case "Text", "Unicode", "HTML" '2 'wdPasteText '2 'text
With Selection
.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis)
GoTo L99
End With
Case Else
MsgRes = MsgBox("You have selected Paste Special as: """ & dlgE.datatype & """" & "." & vbCr & vbCr & _
" Do you wish to re-select UNFORMATTED TEXT? ", vbYesNo, "DataType selection")
If MsgRes = vbYes Then
GoTo ShowDialog
ElseIf MsgRes = vbNo Then
GoTo ExecDialog
End If
End Select
ElseIf dlgEResponse = 0 Then
GoTo L99
End If
End Select

ExecDialog:
dlgE.Execute
L99:
End Sub

fumei
08-01-2007, 10:51 AM
Would not:

myData.GetFromClipboard

fail if it is a graphic? DataObjects can only be text.

BTW: please try to use the underscore character. Thanks.

fionabolt
08-02-2007, 12:39 AM
Fumei


In my testing, the code doesnt fail on
myData.GetFromClipboard

However
If myData.GetFormat(1) = False

Tells me if it's a graphic or not.

So this code achieves what I want it to in this template. However, I'd still like to be able to determine what the DataType is of the object in the Clipboard - that's if it is possible!

Thanks
F

fumei
08-06-2007, 05:18 AM
If myData.GetFormat(1) = False

Tells me if it's a graphic or not.

No, that is not correct.

It means the GetFormat method found no format witha value = 1.

Look at your own code. If myData.GetFormat(1) = False Then
'entry in clipboard is not a string
Goto ShowDialog
End If My bold. Yet, what does ShowDialog say??? Partial code:
Select Case dlgE.datatype
Case "Text", "Unicode", "HTML"
'if the entry in the clipboard is either text, unicode or HTML
'just go ahead and paste it anywayHmmmm, the routine ShowDialog is called if...and I quote..."'entry in clipboard is not a string".

Text, Unicode, HTMl...ummm, looks rather string-like to me.

From Help:
The GetFormat method searches for a format in the current list of formats on the DataObject. If the format is on the DataObject, GetFormat returns True; if not, GetFormat returns False.

The DataObject currently supports only text formats.

So it does NOT tell you if the contents are a graphic, or not. It tells you if the format is there. DataObjects can accept custom user-defined formats. However, ALL of them are text-based.

So a partial answer for the question of "what the DataType is" - if you are bringing the Clipboard contents via DataObject...the data-type will always be text-based.

Clipboard graphic content can NEVER get into a DataObject.

You CAN get wdPasteDataType though.

Again, though, could you please edit your post and use the underscore character? Trying to look at your code is a pain.

fionabolt
08-06-2007, 07:19 AM
OK - here's my code again, with underscores! (It doesn't seem possible to edit previously posted articles).

Sub EditPasteSpecial()
Dim selStr As String
Dim TxtStr As String
Dim myData As DataObject
Dim dlgE As Dialog
Dim dlgEResponse As Long
Dim MsgRes As Long
Set myData = New DataObject
Stop
selStr = Selection.Range.Text
'make sure the paste will not overtype the paramarker
If Right(selStr, 1) = Chr(13) Or Right(selStr, 1) = Chr(10) Then
Selection.SetRange Selection.Range.Start, Selection.Range.End - 1
End If
Set dlgE = Application.Dialogs(wdDialogEditPasteSpecial)
'convert selection to a string
myData.GetFromClipboard
If myData.GetFormat(1) = False Then
'entry in clipboard is not TEXT
'skip the next steps (to remove car returns from string)
'and go to ShowDialog
GoTo ShowDialog
Else:
'entry in clipboard is TEXT
'we can put the contents of the clipboard into a string
TxtStr = myData.GetText
checkforCarRet:
'and check if the string finishes with a carriage return
If Right(TxtStr, 1) = Chr(13) Or _
Right(TxtStr, 1) = Chr(10) Or _
Right(TxtStr, 1) = Chr(32) Then
TxtStr = Left(TxtStr, Len(TxtStr) - 1)
GoTo checkforCarRet 'check for multiple instances of car return
End If
End If
ShowDialog:
MsgBox dlgE.datatype
Select Case dlgE.datatype
Case "Text", "Unicode", "HTML"
'if the entry in the clipboard is either text,
'unicode or HTML just go ahead and paste it anyway
Selection.PasteAndFormat _
(wdFormatSurroundingFormattingWithEmphasis)
GoTo L99
Case Else
dlgEResponse = dlgE.Display
If dlgEResponse = -1 Then
Select Case dlgE.datatype
Case "Text", "Unicode", "HTML"
With Selection
.PasteAndFormat _
(wdFormatSurroundingFormattingWithEmphasis)
GoTo L99
End With
Case Else
MsgRes = MsgBox("You have selected Paste Special as: """ & _
dlgE.datatype & """" & "." & vbCr & vbCr & _
" Do you wish to re-select UNFORMATTED TEXT? ", _
vbYesNo, "DataType selection")
If MsgRes = vbYes Then
GoTo ShowDialog
ElseIf MsgRes = vbNo Then
GoTo ExecDialog
End If
End Select
ElseIf dlgEResponse = 0 Then
GoTo L99
End If
End Select

ExecDialog:
dlgE.Execute
L99:
End Sub


NB - I have amended my comment from:
'entry in clipboard is not a string
to
'entry in clipboard is not TEXT
which is a more accurate description. Since GetFormat (1) is the Text Format of a data object.

Note: ShowDialog is ALWAYS called in this routine. Even if GetFormat(1) = True. If it's false we just skip a few lines of code.

Also, this code just gets around my specific problem. It is not the solution to my original question. "How do I determine the datatype from the Clipboard?"

So to reply to your quote: "if you are bringing the Clipboard contents via DataObject...the data-type will always be text-based." I am ONLY using the DataObject because I don't know how to return the datatype from the clipboard!!!!!!

Fiona

fumei
08-09-2007, 05:31 PM
Sorry...last post on this.

The data-type of the clipboard is not exposed. The paste data-type of the clipboard is. But you set this. In other words you can set what data-type Word will use.

So regarding the issues you raised in your first post, I do not think you can actually do this. The Clipboard holds data. It is either text-based, or graphic-based. That is it. PasteSpecial determines what data-type to use. Word will ignore silly choices.

For example, if you copy a image, and select PasteSpecial HTML format...it will paste the graphic.

If you copy an image into the Clipboard, and use PasteSpecial, and select RTF text....it will paste the graphic.

So...you CAN'T return the data-type of the Clipboard, as far as I know. You can return PasteSpecial choices.

There may be a way with heavy use of API, but I don't know how. The Clipboard seems easy, but it actually is a very intense API usage. 14 separate API calls are made just to put something into the clipboard. We are mostly hidden from this with higher level instructions like SetText.

Sorry, I don't think you can determine the data-type of the Clipboard, as I do not think it actually has any. At least not in the sense that would be useful for you.

fionabolt
08-10-2007, 01:56 AM
OK fumei, many thanks for your help.


Unfortunately I have to go away for a week or so.

Have a great time wherever you are.:hi: