The function ClipboardIsEmpty is also needed; and I added a check for an image on the clipboard, which otherwise causes a runtime error. I suspect the problem is related to a combination of pre-Unicode code in Word and some DataType confusion. Haven't investigated further. The updated code (in case anyone is interested!) looks like:
Sub CaseChange()
' Gets around a problem with SimSun font sometimes for an unknown reason replacing (some/all?) Latin Extended-B characters with SimSun font.
' Something to do with the use of Pinyin and Chinese characters, at least in my instance
' Note that font attributes are lost unless the entire word or selected text is not all of the same attribute(s). Highlighting is always lost.
' To avoid this, use the standard Word Change Case button (which may then result in the Pinyin/SimSun problem!)
Dim sStart As Long, sEnd As Long, sStart1 As Long, sEnd1 As Long
sStart = Selection.Start
sEnd = Selection.End
If Selection.Start = Selection.End Then
Selection.Words(1).Select
Do While Selection.Characters.Last = " "
Selection.End = Selection.End - 1
Loop
End If
sStart1 = Selection.Start
sEnd1 = Selection.End
Selection.Range.Case = wdNextCase
Selection.Start = sStart1
Selection.Copy
Call PasteFormat
Selection.Start = sStart
Selection.End = sEnd
End Sub
Sub PasteFormat()
' Paste text and in the format of text into which it has been pasted.
' Note that if a style itself has a font attribute such as italics that is overridden in the actual text,
' it will revert to the original attribute when pasted as plain text.
' This does not happen when the text is pasted manually, only when the pasting is done programmatically
Dim sEnd As Long
Select Case ClipboardIsEmpty
Case Is = 0
Exit Sub
Case Is = 999
MsgBox "To paste unformatted, the clipboard content must be text, not an image.", , "Paste Unformatted"
Exit Sub
End Select
On Error GoTo DoRegularPastePlainText ' For when PasteSpecial fails
sEnd = Selection.End
Selection.PasteSpecial Link:=False, DataType:=20, Placement:=wdInLine, DisplayAsIcon:=False
Exit Sub
DoRegularPastePlainText:
' Sometimes, the above returns an error due to clipboard content that PasteSpecial cannot handle (such as copying text from the VBA editor),
' so revert to the simple way of doing it
If Selection.End = sEnd Then Selection.PasteAndFormat (wdFormatPlainText)
End Sub
Function ClipboardIsEmpty() As Integer
Dim MyDataObject As DataObject
Set MyDataObject = New DataObject
On Error GoTo NotTextContent
MyDataObject.GetFromClipboard
' ClipboardIsEmpty = (Len(MyDataObject.GetText(1)) = 0) ' Use if ClipboardIsEmpty is Boolean
ClipboardIsEmpty = (Len(MyDataObject.GetText(1)))
Exit Function
NotTextContent:
ClipboardIsEmpty = 999
End Function