Code

From Oh Mad Lab
Jump to navigation Jump to search

OpenOffice macro to paste text as plain and removes tabs:

Sub PasteUnformattedText
	Dim systemClipboard : systemClipboard = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
	Dim converter : converter = createUnoService("com.sun.star.script.Converter")
	Dim clipboardContents : clipboardContents = systemClipboard.getContents()
	Dim transferDataFlavors : transferDataFlavors = clipboardContents.getTransferDataFlavors()
	Dim flavorIndex% : flavorIndex = -1
	Dim convertedString$
	Dim x%

	' Check for the text/plain flavor
	For x = LBound(transferDataFlavors) To UBound(transferDataFlavors)
		If transferDataFlavors(x).MimeType = "text/plain;charset=utf-16" Then
			flavorIndex = x
			Exit For
	    End If
	Next
	' If we found the flavor we want, then...
	If (flavorIndex >= 0) Then
		Dim lines
		Dim pasteText
		Dim document
		Dim dispatcher
		Dim args(0) as new com.sun.star.beans.PropertyValue
		' Grab the cliboard data
		convertedString = converter.convertToSimpleType(clipboardContents.getTransferData(transferDataFlavors(flavorIndex)), com.sun.star.uno.TypeClass.STRING)
		' Split the cliboard data into an array base on tabs
		lines = Split(convertedString, Chr(9))
		' Convert tabs to spaces
		pasteText = ""
		For x = LBound (lines) to UBound (lines)
			pasteText = pasteText + " " + lines(x)
		Next
		pasteText = Trim(pasteText)
		' Get the current spreadsheet document
		document = ThisComponent.CurrentController.Frame
		' Use the dispatch helper to paste the text to the spreadsheet
		dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
		args(0).Name = "StringName"
		args(0).Value = pasteText
		dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args())
	End If
End Sub