none
How Do You Create A Visual Basic Program Without Using A Form? RRS feed

  • Question

  • I'm trying to create a VB program that automatically creates a Word document, but without using a Form user interface.  This is how the beginning of my program currently looks:


    Module mainModule

    Imports Word = Microsoft.Office.Interop.Word Sub Main() 'Public Class Form1 'Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Title Table for [AC] Dim wordApp As New Word.Application wordApp.Visible = True Dim doc As Word.Document = wordApp.Documents.Add() Dim range As Word.Range = doc.Content

    The code works fine when I use "Public Class Form1" and "Private Sub Button1_Click", instead of "Module mainModule" and "Sub Main()".  However, I don't want to use a Form, that's why I commented out "Public Class Form1" and "Private Sub Button1_Click".  I'm trying to bypass the Form aspect of Visual Basic and automatically generate a Windows document instead, without any Form user interface.

    The way the code is currently setup, it does not recognize the "Imports Word = Microsoft.Office.Interop.Word" statement, and therefore the rest of my code won't work either.  This program is currently running as a Windows Forms Application.

    Thank you in advance,

    Jim Hutchinson

    Sunday, February 25, 2018 2:32 AM

Answers

  • Following worked just fine for me -

    • Marked as answer by jnhutchi3 Tuesday, February 27, 2018 9:45 AM
    Monday, February 26, 2018 2:12 AM
  • The program successfully compiled after I changed "Module mainModule" to "Module Module1".  However, now I'm getting the following runtime error in the terminal window:

    "Process is terminated due to StackOverflowexception."

    When I ran this program as a Windows Forms application, I did not receive this error and the program would successfully run. 

    I'm wondering if there is just too much code for my Word document program to run as a Console Application?  It is a pretty large program.

    • Marked as answer by jnhutchi3 Tuesday, February 27, 2018 9:45 AM
    Tuesday, February 27, 2018 2:45 AM

All replies

  • You say you don't want to use a form but you are running a Windows Forms application.

    In the Visual Studio new project wizard, select the option to create a console application.  Then copy your Word related code into that new project, add required references and correct errors.


    • Edited by RLWA32 Sunday, February 25, 2018 11:21 AM
    Sunday, February 25, 2018 11:20 AM
  • Thank you for the help.  I think I'm headed in the right direction now.  However, I'm getting an error that states "Sub Main was not found" when I try to run the program as a console application.  The good news is that I think my Microsoft Word code is going to work, if I can get passed this one error.

    Again, here is the beginning of my code.  I don't know what's causing the Sub Main() error:

    Imports Word = Microsoft.Office.Interop.Word Module mainModule Sub Main() 'Title Table for [AC] Dim wordApp As New Word.Application wordApp.Visible = True Dim doc As Word.Document = wordApp.Documents.Add() Dim range As Word.Range = doc.Content range = doc.Range() 'range.Text = "RMF Test Procedures: All Security Controls" doc.Sections(1).PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape


    Monday, February 26, 2018 1:41 AM
  • The full error is this:

    "Sub Main was not found in Test_Procedures.Module1" 

    "Test_Procedures" is the name of my VB console application project.  I did not name any file with a .Module1 extension, I'm not sure what that is.

    Monday, February 26, 2018 1:53 AM
  • Following worked just fine for me -

    • Marked as answer by jnhutchi3 Tuesday, February 27, 2018 9:45 AM
    Monday, February 26, 2018 2:12 AM
  • Maybe I need the "wordApp.Quit()".  I'll try it and get back to you.  Thank you.
    Monday, February 26, 2018 4:24 AM
  • Hi Jim,

    >> "Test_Procedures" is the name of my VB console application project

    Do you create a new VB Console application from scratch or modify on the original Form application?

    For your error, it is not related with wordApp, I suggest you create a new Console app and paste the code to Main method.

    If you are modifying on the original Form, I suggest you go to Project Property->Application->Application type as Console Application->Startup Object->mainModule.

    If you fail to make it work, it would be helpful if you could share us your simple project through oneDrive.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 26, 2018 6:58 AM
  • Maybe I need the "wordApp.Quit()".  I'll try it and get back to you.  Thank you.

    That has nothing to do with the error you received.  I earlier suggested that you use the Visual Studio new project wizard to create a console application.  It doesn't appear to me that you started with a fresh, new VB.net project created by the wizard.  Did you do what was suggested?

    • Edited by RLWA32 Monday, February 26, 2018 10:18 AM
    Monday, February 26, 2018 10:15 AM
  • The program successfully compiled after I changed "Module mainModule" to "Module Module1".  However, now I'm getting the following runtime error in the terminal window:

    "Process is terminated due to StackOverflowexception."

    When I ran this program as a Windows Forms application, I did not receive this error and the program would successfully run. 

    I'm wondering if there is just too much code for my Word document program to run as a Console Application?  It is a pretty large program.

    • Marked as answer by jnhutchi3 Tuesday, February 27, 2018 9:45 AM
    Tuesday, February 27, 2018 2:45 AM
  • Hi jnhutchi,

    >>The program successfully compiled after I changed "Module mainModule" to "Module Module1"

    I am glad you have resolved your original error related with "Sub Main was not found", I would suggest you mark the helpful reply as answer to close this thread.

    >>Process is terminated due to StackOverflowexception.

    For this new issue, I would suggest you post a new thread, and share us where you got this error.

    A simple project to reproduce your issue would be helpful.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 27, 2018 2:48 AM
  • I created a new Console app from scratch and I copied and pasted my code from my Windows Form app.  As I mentioned previously, the application now compiles, but I'm getting a Stack Overflow error.  I will create a new thread topic for that. 

    However, I did not get this Stack Overflow error when I ran the program as a Windows Form app.  My code is exactly the same as before.  I think I may just have too large of a program for a VB Console application.

    I just marked the posts that provide answers to my original problem.  Thank you.

    Tuesday, February 27, 2018 9:54 AM