• 0

VB.NET Opening external EXE files


Question

Hello,

I am able to export Rich text from my VB.net app and export it to Microsoft Word; but, I want some error control if the user using the app doesn't have Microsoft Word. Right now, the app just gives a nasty .NET error message that the end user wouldn't know what is going on.

This is the code I have so far, and it isn't working, any idea how to do this?

 oWord = CreateObject("Word.Application")
        If CreateObject("Word.Application") = False Then MessageBox.Show("My Silly attempt at Error messages.")

I want it to show the above message and quit the operating of trying to export to Word.

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

well, i have no idea where you got that from.. I'm not surprised it doesn't work cos as far as I know you have to use the COM interop to access office from .NET but you could just stick it in a try catch, i.e.

Try
	Dim x As Integer
	x = "text" 'won't work
Catch ex As Exception
	MsgBox("I am a friendly error")
End Try

Link to comment
Share on other sites

  • 0

Well I just tried the friendly error, but it doesn't work...

Try

oWord = CreateObject("Word.Application")

oWord.Visible = True

Catch

MessageBox.Show("My Silly attempt at Error messages.")

End Try

Link to comment
Share on other sites

  • 0

Try
	Dim oWord As Word.Application
	oWord = CreateObject("Word.Application")
	oWord.Visible = True
Catch ex As Exception
	MsgBox("You don't have word installed")
End Try

you also need to add the COM object "Microsoft Word Object Library" to your solution, and use an Imports statement for it

Imports Microsoft.Office.Interop

post-24841-1252717060_thumb.png

Edited by BGM
Link to comment
Share on other sites

  • 0

I have the Interop imported...but still no luck, the default .NET error message is appearing, and the custom one is now appearing on the system that does have Word installed...

Link to comment
Share on other sites

  • 0
I have the Interop imported...but still no luck, the default .NET error message is appearing, and the custom one is now appearing on the system that does have Word installed...

wtf.. lol

show me your whole method please, and also how are you deploying it to another machine?

Link to comment
Share on other sites

  • 0

I am deploying it to my VM of Windows 2000.

    Private Sub WordSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oPara1 As Word.Paragraph
            oWord = CreateObject("Word.Application")
            oWord.Visible = True

            'Start Word and open the document template.
            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add()

            'Personal Information
            oPara1 = oDoc.Content.Paragraphs.Add
            oPara1.Range.Text = RichTextBox2.Text & " " & RichTextBox3.Text & " " & RichTextBox4.Text
            oPara1.Range.Font.Bold = False
            oPara1.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            oPara1.Format.SpaceAfter = 1    '24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter()

            'All done. Close this form.
            Me.Close()
            MessageBox.Show("Exporting to Microsoft Word completed successfully.")

        Catch ex As Exception
            MsgBox("You don't have Word installed")
        End Try
    End Sub

Link to comment
Share on other sites

  • 0

right well that method works just fine for me... in the fact that i get no error message when it launches word and inserts the text from a richtextbox.

so i can only assume that there is something else going on

i cannot test it on a machine without word, but i am going to work with the assumption that you are not deploying it correctly.. so how are you transferring it to your VM ?

also can you show a screenshot of when the error(s) occur, thanks! ;)

Link to comment
Share on other sites

  • 0

right, so seeing as you haven't specified (and are making this difficult) i assume that is the error from the VM?

does your machine with word work ok now? it seems to have been forgotten about..

anyway, try packaging up the office .dll into your deployment package and deploying it again, you still havn;t said how you are deploying so again, i am making an assumption

work with me here! ;)

Link to comment
Share on other sites

  • 0

what i would do is

1) save the richtext to a temp file in the appdata directory

2) call winword with the file

That will open office word on computers that have it and wordpad on computers without word. Everybody happy ;)

Dim startInfo As System.Diagnostics.ProcessStartInfo
Dim pStart As New System.Diagnostics.Process

startInfo = New System.Diagnostics.ProcessStartInfo("winword ""path_to_tempfile""")

pStart.StartInfo = startInfo
pStart.Start()

god i hate vb syntax, why do people torture themselves with this ****

Link to comment
Share on other sites

  • 0
right, so seeing as you haven't specified (and are making this difficult) i assume that is the error from the VM?

does your machine with word work ok now? it seems to have been forgotten about..

anyway, try packaging up the office .dll into your deployment package and deploying it again, you still havn;t said how you are deploying so again, i am making an assumption

work with me here! ;)

Yes, my machine with Word is working without issue now...I had left some unused code, and it was causing the issue there. I am just copying the .exe file over to the VM....is there some other file that needs to go along with it?

Link to comment
Share on other sites

  • 0

copying the .exe only will not work because on the Vm you are still referencing something that doesn't exist.

i would add a setup package to your solution, and create an installer (which will include the necessary references) then you can take that installer file to your VM and install it

good luck! :)

post-24841-1252767704.png

Link to comment
Share on other sites

  • 0
copying the .exe only will not work because on the Vm you are still referencing something that doesn't exist.

i would add a setup package to your solution, and create an installer (which will include the necessary references) then you can take that installer file to your VM and install it

good luck! :)

sweet! that was it! it works now, I really appreciate your help!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.