-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJavaWord.bas
More file actions
54 lines (45 loc) · 1.45 KB
/
JavaWord.bas
File metadata and controls
54 lines (45 loc) · 1.45 KB
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
Attribute VB_Name = "NewMacros"
Sub JavaWord()
' JavaWord macro
' runs a Word file as Java code
'
' @author christophebedard
Dim strDocName As String
Dim strDocNameExt As String
Dim intPos As Integer
Dim strJavaPath As String
Dim strCmd As String
' Set path to java bin
strJavaPath = "C:\Program Files\Java\jdk-9.0.4\bin"
' Retrieve name of ActiveDocument
strDocPath = ActiveDocument.Path
strDocName = ActiveDocument.Name
intPos = InStrRev(strDocName, ".")
strDocName = Left(strDocName, intPos - 1)
strDocNameExt = strDocName & ".java"
' Test if Activedocument has previously been saved
If ActiveDocument.Path = "" Then
' If not previously saved
MsgBox "The current document must be saved at least once."
Else
' If previously saved, create a copy
Set myCopy = Documents.Add(ActiveDocument.FullName)
' Force the file to be saved
If myCopy.Saved = False Then myCopy.Save FileName:=strDocPath & "\" & strDocName
' Save file with new extension
myCopy.SaveAs2 FileName:=strDocPath & "\" & strDocNameExt, FileFormat:=wdFormatText
' Close copy
myCopy.Close
End If
' Call command to
' 1- cd to current directory
' 2- Add java to PATH
' 3- Compile .java file
' 4- Run .java file
strCmd = "cmd.exe /S /K"
strCmd = strCmd & "CD /D " & strDocPath
strCmd = strCmd & " & set PATH=" & strJavaPath & ";%PATH%"
strCmd = strCmd & " & javac " & strDocNameExt
strCmd = strCmd & " & java " & strDocName
Call Shell(strCmd, vbNormalFocus)
End Sub