CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Restart application

    Hey all,

    Is there any possible way to make a program (standard exe) close and restart itself? I know that sounds kinda rediculous but I'm running out of options.

    I have put up a couple posts in the last week or so regarding Tables in MS Word. My problem is that my program has trouble creating a table on a Word Document after it works successfully once.

    That first time I click my report button, MS Word pops up with my table and all the data formatted real pretty. After that first time though, every time I click my report button, Word pops up with all the data strung together and no Table to format it in.

    If I close my app and re-run it, It will generate the Table again... once. Then I have to close my app again and re-run it to do another report. I haven't had a bit of luck troubleshooting this bug.

    If there is a way to make my app exit and reload itself between reports, that would at least save the users the trouble of having to restart the app. If anyone has any ideas, I'd greatly appreciate it. I realize this solution is nothing more than a work-around, so if anyone has an idea why I can't create tables in Word after the first try I will be totally thrilled.

    Thanks guys!

    Jeff


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Restart application

    This code starts another instance of itself

    public Sub ShutDownAndStart()
    ' actually, StartAndShutDown
    Shell App.Path & "\" & App.ExeName

    ' unload every form, and exit
    Dim frm as form
    for Each frm in Forms
    Unload frm
    next frm

    End

    End Sub




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Restart application

    Great code- it does exactly what it says it does, but i'm still having the same overall problem with the Table in Word. As before, If I manually close my app and re-enter it, I can display the Table again. And then I'm back to closing the app so that yet another table can be displayed. I guess I'll just keep playing with it. Thanks for the swift reply!

    Jeff


  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Restart application

    Do you shut down Word everytime, I mean, everytime you do that table stuff. Maybe Word stays open and reacts strange or so.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Restart application

    Thanks again for responding...

    After Word opens up and I have my beautiful table filled with data, the user would typically hit print or save or something, then close Word and go back to my app to select another grouping of data to be processed and put into another Word table.

    So the WINWORD.EXE application is exited completely as far as I can tell. Initially my problem was that WINWORD.EXE was staying in memory and messing up the works, but as far as I can tell (and Task Manager can tell) it is really exitting now.

    Here is my code if you'd like to play with it...


    'create instance of word
    Dim wrdapp as new Word.Application


    'check to see if wrdapp object is already instantiated
    'if so, move on. if not, then set it to Word.Application
    on error resume next
    Err = 0
    a = wrdapp.Visible
    If Err = 462 then
    set wrdapp = new Word.Application
    End If
    Err = 0
    wrdapp.Visible = false
    If Err <> 0 then
    wrdapp.Quit
    set wrdapp = new Word.Application
    wrdapp.Visible = false

    End If

    'add a document to Word
    wrdapp.Documents.Add


    'slap a heading on that bad boy
    wrdapp.Selection.Font.Bold = true
    wrdapp.Selection.Font.Size = 16
    wrdapp.Selection.Font.Underline = wdUnderlineSingle
    wrdapp.Selection.Paragraphs.Alignment = wdAlignParagraphCenter
    wrdapp.Selection.TypeText "Solid Waste Management" & vbCrLf & "Crew Departure time Report" & vbCrLf & vbCrLf
    wrdapp.Selection.TypeText "Zone Statistics for dates " & StartDate & " to " & EndingDate & "." & vbCrLf & vbCrLf & vbCrLf

    wrdapp.Selection.Font.Size = 11
    wrdapp.Selection.Paragraphs.Alignment = wdAlignParagraphLeft
    wrdapp.Selection.Font.Underline = wdUnderlineNone
    wrdapp.Selection.Font.Bold = false



    'create the table

    wrdapp.Selection.Tables.Add Range:=Selection.Range, NumRows:=21, NumColumns _
    :=5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    wrdapp.Selection.Tables(wrdapp.Selection.Tables.Count).AutoFormat Format:=wdTableFormatContemporary, _
    ApplyBorders:=true, ApplyShading:=true, ApplyFont:=true, ApplyColor:=true _
    , ApplyHeadingRows:=true, ApplyLastRow:=false, ApplyFirstColumn:=true, _
    ApplyLastColumn:=false, AutoFit:=true

    MsgBox wrdapp.Selection.Tables.Count


    'table heading
    wrdapp.Selection.TypeText "Location"
    wrdapp.Selection.MoveRight
    wrdapp.Selection.TypeText "Zone"
    wrdapp.Selection.MoveRight
    wrdapp.Selection.TypeText "Minimum"
    wrdapp.Selection.MoveRight
    wrdapp.Selection.TypeText "Mean"
    wrdapp.Selection.MoveRight
    wrdapp.Selection.TypeText "Maximum"
    wrdapp.Selection.MoveRight



    wrdapp.Visible = true





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured