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
Option Explicit

Sub Main
' Find a block of text in syntax window and replace it by another block.

' Requirements:
' Enter the target lines (those to be replaced) at the beginning of the designated window.
' Enter 2 \\'s at the beginning of the following line
' Enter the replacement lines starting immediately after the above mentionned \\\\
' Enter 2 \\'s at the beginning of the following line
' Do not leave any trailing spaces at the end of your lines of codes
' Caveats:
' The script replaces consecutive lines which are *identical* to the target lines by
' the replacement lines.

' Posted to spss newsgroup by Raynald Levesque on 2003/09/10.

	Dim objSyntaxDoc As ISpssSyntaxDoc
	Dim strSynCmd As String
	Dim intPos As Integer
	Dim strScript As String
	Dim strReplace As String
	Dim strFind As String

	Set objSyntaxDoc=objSpssApp.GetDesignatedSyntaxDoc
	Debug.Print objSyntaxDoc.GetDocumentPath

	'Save designated syntax window (if there is a problem, use can undo the "find & replace"
	'by reloading the file).
	If objSyntaxDoc.GetDocumentPath <> "" Then
		objSyntaxDoc.SaveAs (objSyntaxDoc.GetDocumentPath)
	Else
		MsgBox ("Save the designated syntax window " & vbCrLf & "then try again.")
		Exit Sub
	End If
	Debug.Clear

	strSynCmd = objSyntaxDoc.Text

	Debug.Print strSynCmd
	strFind = Left(strSynCmd,InStr(strSynCmd,"\\\\") - 1)
	Debug.Print "strFind=" & vbCrLf & strFind

	'Redefine rest of syntax
	strSynCmd = Mid(strSynCmd,InStr(strSynCmd,"\\\\") + 4)
	Debug.Print "Rest of syntax " & vbCrLf & strSynCmd

	strReplace = Left(strSynCmd,InStr(strSynCmd,"\\\\") - 1)
	Debug.Print "Replace by" & vbCrLf & strReplace

	'Redefine rest of syntax
	strSynCmd = Mid(strSynCmd,InStr(strSynCmd,"\\\\") + 4)
	Debug.Print "Rest of syntax " & vbCrLf & strSynCmd

	strSynCmd=Replace(strSynCmd,strFind,strReplace)
	Debug.Print strSynCmd

	objSyntaxDoc.Text = strFind & "\\\\" & vbCrLf & strReplace & "\\\\" & vbCrLf & strSynCmd
	Set objSyntaxDoc=Nothing
End Sub