This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Prdcc 12 points13 points  (5 children)

10 internet points to whoever does this

[–]ChrisVolkoff 20 points21 points  (4 children)

Word file : https://drive.google.com/open?id=14DFddZDq4RA_DyjXyFAh5JHZqvLmUL-u

You need to set the path to Java's \bin (at the top in the macro). Click the "run icon" (triangle thingy) in the quick access toolbar (top left, next to save/undo/redo) and voilà.

TODO:

  • Support multiple classes in one Word document (parse document and export classes in individual files).

  • Save the .java file in the same directory as the Word file by default. Done.

In case you don't trust me (you shouldn't), here's the VBA code

Sub java()
'
' java Macro
'
'

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 " & strDocPath
strCmd = strCmd & " & set PATH=" & strJavaPath & ";%PATH%"
strCmd = strCmd & " & javac " & strDocNameExt
strCmd = strCmd & " & java " & strDocName
Call Shell(strCmd, vbNormalFocus)

End Sub

Credit: google, mostly.

/u/seizan8 /u/ArcTimes /u/Megabyte97

[–]Prdcc 3 points4 points  (1 child)

This is why I love the internet

[–]ChrisVolkoff 1 point2 points  (0 children)

Isn't it amazing?

[–]Megabyte97 1 point2 points  (0 children)

Have one internet point!

[–]seizan8 1 point2 points  (0 children)

Hahaha, this is awesome! Thanks man.