Skip to content

Commit f9d1be8

Browse files
committed
For safekeeping
1 parent f5a2dd0 commit f9d1be8

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

tools/Macros.vba

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Public Const basedir = "C:\Users\Bruce\Dropbox\__TIJ4-ebook\ExtractedExamples\"
2+
3+
Sub AutoOpen()
4+
ActiveWindow.View.Draft = True
5+
End Sub
6+
7+
Function fileExists(fname As String) As Boolean
8+
On Error GoTo fail
9+
Dim fn As String
10+
fn = basedir & fname
11+
12+
Open basedir & fname For Input As #1
13+
Close #1
14+
fileExists = True
15+
Exit Function
16+
fail:
17+
fileExists = False
18+
End Function
19+
20+
Private Sub updateFile()
21+
Dim oldCode As String, i As Integer
22+
Dim fname As String, ln As String
23+
Dim newCode As String
24+
25+
' Get the file name
26+
oldCode = Selection
27+
i = InStr(5, oldCode, ".java")
28+
fname = Mid(oldCode, 5, i)
29+
fname = Replace(fname, "/", "\")
30+
31+
If Len(fname) = 0 Then ' No .java file found
32+
Exit Sub
33+
End If
34+
35+
' Does the file exist?
36+
If Not fileExists(fname) Then
37+
If MsgBox(fname & " could not be found! Continue replace?", vbYesNo + vbExclamation, "UpdateCode") = vbNo Then
38+
Exit Sub
39+
End If
40+
End If
41+
42+
'Open the file
43+
Open basedir & fname For Input As #1
44+
While Not EOF(1)
45+
Line Input #1, ln
46+
newCode = newCode & ln & vbCrLf
47+
Wend
48+
Close #1
49+
50+
'Add the new code and change the style
51+
Selection = Left(newCode, Len(newCode) - 2)
52+
Selection.Style = ActiveDocument.Styles("Code")
53+
End Sub
54+
55+
Sub UpdateCode()
56+
' Update a single code listing
57+
Selection.Find.ClearFormatting
58+
With Selection.Find
59+
.Text = "//: ?@///:~"
60+
.Forward = True
61+
.Wrap = wdFindContinue
62+
.Format = False
63+
.MatchCase = False
64+
.MatchWholeWord = False
65+
.MatchAllWordForms = False
66+
.MatchSoundsLike = False
67+
.MatchWildcards = True
68+
End With
69+
Selection.Find.Execute
70+
Call updateFile
71+
Selection.MoveRight wdCharacter, 1
72+
End Sub
73+
74+
Sub FreshenAllCode()
75+
' Update the entire book's code listings
76+
77+
'Application.ScreenUpdating = False
78+
Application.EnableCancelKey = xlInterrupt
79+
80+
Selection.HomeKey Unit:=wdStory
81+
82+
Selection.Find.ClearFormatting
83+
With Selection.Find
84+
.Text = "//: ?@///:~"
85+
.Forward = True
86+
.Wrap = wdFindContinue
87+
.Format = False
88+
.MatchCase = False
89+
.MatchWholeWord = False
90+
.MatchAllWordForms = False
91+
.MatchSoundsLike = False
92+
.MatchWildcards = True
93+
End With
94+
Selection.Find.Execute
95+
96+
While Selection.Find.Found
97+
Call updateFile
98+
Selection.MoveRight wdCharacter, 1
99+
Selection.Find.Execute
100+
Wend
101+
End Sub
102+
103+
Sub SaveAsText()
104+
'
105+
' SaveAsText Macro
106+
'
107+
'
108+
ChangeFileOpenDirectory "C:\Users\Bruce\Dropbox\__TIJ4-ebook\"
109+
ActiveDocument.SaveAs2 FileName:="TIJDirectorsCut.txt", FileFormat:= _
110+
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
111+
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=True, _
112+
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
113+
False, Encoding:=1252, InsertLineBreaks:=False, AllowSubstitutions:=False _
114+
, LineEnding:=wdCRLF, CompatibilityMode:=0
115+
End Sub
116+
117+

0 commit comments

Comments
 (0)