1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'Begin Description
'This script works like Autorecode, but in the other direction:
'The script generates a new string variable which values are equal
'to the labels of the source variable.
'So someone can export a variable to other applications with saving
'his labels.
'If there are no variables or no variables with labels in the current
'document, a warning appears.
'If a variable is selected while entering this script, the variable is selected
'as default in the dialog box
'If you like to translate the script into other languages, simple change the text
'in the following lines

'End Description

'Author: Bernhard Witt, SPSS Germany

'Constants for Textoutput
'English expressions
Const TextNoVariables = "There are no variables in the document."
Const TextNoVarWithLabels = "There are no variables with value labels!"
Const TextTargetAlreadyExist = "The output variable does already exist. Please enter a new variable name."
Const TextPleaseEnterTarget = "Please enter a name for the output variable."
Const TextWarning = "Warning"
Const TextDialogBoxTitle = "Label2String"
Const TextDialogBoxVariable = "Variable"
Const TextDialogBoxTarget = "Output variable"
Const TextDialogBoxSource = "Input variable"
Const TextDialogBoxHelp = "Help"
Const TextHelpText = "Label2String creates a new variable which includes the labels of" + Chr$(13)+ "the input variable as strings."+Chr$(13) + Chr$(13)+ "(c) 1997 by SPSS Germany - Author: Bernhard Witt"
Const TextHelpTextHead = "Help Label2String"
Const TextTargetTooLong = "The Output variable is too long (max. 8 characters)"


'German expressions
'Const TextNoVariables = "Es sind keine Variablen vorhanden."
'Const TextNoVarWithLabels = "Es sind keine Variablen mit Wertelabels vorhanden!"
'Const TextTargetAlreadyExist = "Die Zielvariable ist bereits vorhanden. Bitte geben Sie eine andere Variable an."
'Const TextPleaseEnterTarget = "Bitte geben Sie eine Zielvariable ein."
'Const TextWarning = "Achtung"
'Const TextDialogBoxTitle = "Label2String"
'Const TextDialogBoxVariable = "Variable"
'Const TextDialogBoxTarget = "Zielvariable"
'Const TextDialogBoxSource = "Ausgangsvariable"
'Const TextDialogBoxHelp = "Hilfe"
'Const TextHelpText = "Label2String erstellt eine neue Variable, in der die Labels der" + Chr$(13)+ "Ausgangsvariable als String verwendet werden."+Chr$(13) + "(Label2String ist somit das Gegenteil von Autorecode)"+Chr$(13)+Chr$(13)+ "(c) 1997 by SPSS Germany - Autor: Bernhard Witt"
'Const TextHelpTextHead = "Hilfe zu Label2String"
'Const TextTargetTooLong = "Die Zielvariable ist lдnger als 8 Zeichen"

Public dlg						'Dialog box
Public VarLabel$()				'Array with the actual Valuelabels of the selected variable
Public VarWert()				'Array with the actual Values of the selected variable
Public Variablenliste$()		'Array with includes all variables with at least one
								'value label
Public AnzLabVariablen			'number of valuelabels of the selected variable
Public BigVarliste$()			'Array with a all variables of the document
Public BigVarlistedim			'number of elements in BigVarListe$()
Public selectedVariable			'the number of the selected variable in Variablenliste$()

Sub Main

	Call GetVariablenliste
	If AnzLabVariablen > 0 Then
		Call myDialog
	Else
		MsgBox TextNoVarWithLabels,64,TextWarning
	End If
	
End Sub


'#########################################################################
Sub Label2String (Varnummer, Varneu$)
'#########################################################################
Dim objSpssApp As Object
Dim strCommands As String
Set objSpssApp = CreateObject("SPSS.Application")

Set objDocuments=objSpssApp.Documents
Set objDataDoc = objDocuments.GetDataDoc(0)

NumCases = objDataDoc.GetNumberOfCases
Original = objDataDoc.GetTextData (Variablenliste$(Varnummer), Variablenliste$(Varnummer), 1, NumCases)

'How long is the longest Label of the current variable?
MaxLabelLength = 0
For count = 0 To NumCases-1
	AktLabel = Original (0,count)
	If Len(AktLabel) > MaxLabelLength Then
		MaxLabelLength = Len(AktLabel)
	End If
Next

'create the new variable
strCommands = "string "+Varneu$+"(A"+LTrim$(Str$(MaxLabelLength))+")." & vbCr
strCommands = strCommands & "execute." & vbCr
objSpssApp.ExecuteCommands strCommands, False
While objSpssApp.IsBusy
	'wait until spss processor is ready
Wend

Original = objDataDoc.GetTextData (Variablenliste$(Varnummer), Variablenliste$(Varnummer), 1, NumCases)
For count = 0 To NumCases-1
	objDataDoc.SelectCells (VarNeu$, VarNeu$, count+1, count+1)
	While objSpssApp.IsBusy
		'wait until spss processor is ready
	Wend
	Clipboard Original (0,count)
	While objSpssApp.IsBusy
		'wait until spss processor is ready
	Wend
	objDataDoc.Paste
	While objSpssApp.IsBusy
		'wait until spss processor is ready
	Wend
Next


End Sub


'#########################################################################
Sub myDialog
'Creates Dialog Box
'#########################################################################
	Begin Dialog UserDialog 450,126,TextDialogBoxTitle,.Maskenfunktion
		GroupBox 10,7,320,105,TextDialogBoxVariable,.Variable
		TextBox 20,42,110,21,.Varneu
		ListBox 160,42,140,63,Variablenliste(),.ListBox1
		Text 20,28,150,14,TextDialogBoxTarget,.Text2
		Text 160,28,120,14,TextDialogBoxSource,.Text3
		OKButton 360,14,70,21
		PushButton 360,91,70,21,TextDialogBoxHelp,.Hilfe
		CancelButton 360,42,70,21,.Abbrechen
	End Dialog

Dim dlg As UserDialog
dlg.ListBox1 = selectedVariable
erg=Dialog (dlg)
If erg = -1 Then
Call Label2String (dlg.ListBox1,dlg.Varneu)
End If

End Sub


'#########################################################################
Function Maskenfunktion(SteuerelementBez As String, Aktion As Integer , ZusatzWert As Integer )  As Boolean
'#########################################################################
Select Case Aktion
Case 1	' Init
Case 2	' A Dialogfield was selected
	Select Case SteuerelementBez
	Case "OK"
		If Len (DlgText$("varneu")) = 0 Then
			MsgBox TextPleaseEnterTarget,64,TextWarning
			DlgFocus "varneu"
			Maskenfunktion=True
		End If
		If Len (DlgText$("varneu")) > 8 Then
			MsgBox TextTargetTooLong,64,TextWarning
			DlgFocus "varneu"
			Maskenfunktion=True
		Else
			'Check, if the new variable name is already in use
			'if so, then msgbox and do not leave dialog box
			Valid=1
			For count = 0 To BigVarlistedim
				If UCase$(DlgText$("Varneu")) = UCase$(BigVarListe$(count)) Then
					valid = 0
				End If
			Next
			If valid = 0 Then
				MsgBox TextTargetAlreadyExist,64,TextWarning
				Maskenfunktion=True			
			Else
				Maskenfunktion=False
			End If
		End If

	Case "Hilfe"
	MsgBox TextHelpText, 64, TextHelpTextHead
		Maskenfunktion=True
	Case Else
		Maskenfunktion=False
	End Select
Case 3	' Textfield was changed

Case 4	' focus has changed

Case 5	' nothing else to do

Case Else

End Select

End Function


'#########################################################################
Sub GetVariablenliste
'creates a list with all variables, which are using labels
'#########################################################################

Dim objSpssInfo As ISpssInfo
Dim strVarName As String, strLabel As String
Dim spss As Object
Set spss=CreateObject("SPSS.application")
Set objSpssInfo=objSpssApp.SpssInfo

'determine the selected variable, if exist
Dim Anfang As Long, Ende As Long
Dim Selektiert As Long
temp = objSpssInfo.GetSelectedVariables (Anfang, Ende)
Selektiert=Anfang

Dim AnzVariablen As Integer, countvar As Integer
AnzVariablen = objSpssInfo.NumVariables

If AnzVariablen > 0 Then
	'count the variables, which are using at least one label
	'and store all variable names
	AnzLabVariablen = 0

	ReDim BigVarListe$(AnzVariablen-1)
	BigVarlisteDim = AnzVariablen-1
	
	'count variables with at least one label
	For count = 0 To AnzVariablen-1
		BigVarListe$(count) = objSpssInfo.VariableAt(count)
		If objSpssInfo.NumberOfValueLabels(count) >0 Then
			AnzLabVariablen = AnzLabVariablen +1
		End If
	Next

	'If ther exist at leas one variable with a label, build the varlist-
	If AnzLabVariablen>0 Then
		ReDim Variablenliste$(AnzLabVariablen-1)

		mycount=0
		For count = 0 To AnzVariablen-1
			If objSpssInfo.NumberOfValueLabels(count)>0 Then
				Variablenliste$(mycount) = objSpssInfo.VariableAt(count)
				'If the aktual variable is selected, then store the index in 
				If count=selektiert Then
					selectedVariable = mycount
				End If
				mycount = mycount+1
			End If
		Next
	End If
Else
	MsgBox TextNoVariables,64,TextWarning
End If

End Sub


---------------------------------------------------------------------------