Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

launching Java from Excel VBA

Status
Not open for further replies.

TarunMakhija

Programmer
Jun 14, 2005
13
US
I am trying to launch a Java application at the click of an excel button...

For starters all I am trying to do is, run the following program which creates a new file on my Desktop at the click of an excel button.

import java.io.File;
import java.io.FileWriter;

public class Test {
public static void main(String[] args) {
try {
File f = new File("C:/Documents and Settings/Tarun Makhija/Desktop/tarun.txt");
FileWriter out = new FileWriter("C:/Documents and Settings/Tarun Makhija/Desktop/tarun.txt");
out.write(" ");
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Any Ideas how I can go about doing this?

I have tried using the Shell function in Excel VBA .. but it doesnt work ..

Thanks,
Tarun
 
From what little I know about Java, this looks as if it opens a file and writes a space to it then closes the file.

Assuming that you have got the command line correct from your shell command, and it works fine when run outside of Excel VBA, then I can't offer much help other than to say to convert this program to Excel VBA and run it from within Excel would be far neater than calling an external program.

Something like the following should do (not tested, written off the top of my head):

Code:
Public Sub WriteFile

  On Error Goto err_WriteFile

  Dim i As Integer

  i = FreeFile

  Open "C:\Documents and Settings\Tarun Makhija\Desktop\tarun.txt" For Append As #i
  Print #i, " "
  Close #i
  Exit Sub

Err_WriteFile:
  MsgBox Err.Number & " " & Err.Description

End Sub

John
 
Thanks John. I actually managed to have it work using the Shell command.. I was making a really stupid mistake..

My next question however is...

Since I am trying to launch the java application from with the same excel sheet which I am trying to "modify" using the java app.. I am going to have an access error since the excel file is already open....

any ideas how to go about solving this problem?

I was thinking more in lines of writing the output to another file, then parsing it using VBA and writing it to the excel file.. but obviously, thats gonna be highly inefficient and bad design...

Please let me know if u have a better idea

Thanks a lot,
Tarun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top