DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

Programming Microsoft Word - 04 - From Form to Word Document

What are we going to do in this lesson?

The purpose of this lesson is to create a windows form that will take user input and insert it in a Word document. One of the best aspects of software is its re-usability. Software code is just electronic text that can be reused over and over again. To demonstrate this feature, we will built on top of the application made in the previous.

First, we are going to save our previous program as a template. If you have not built the program from the previous lesson, do so now before proceeding.

In the first steps, we are going to reopen our tray-application and save it as a template. From then on, this template will be available for reuse in future lessons.

Step 1

Fire up Visual Studio and open your tray application from the previous lesson. Do so by opening via the menu: File → Open Project.

Now, select your tray application built in the previous lesson and open the project.

Once you have opened the tray application, you export this project as a template (I assume you have build/run the tray application at least once already). Export it as a template through: File → Export Template.

A window pops up with the question to choose a template type. I suggest you choose 'Project template' for now and click 'Next'. Leave everything as it is in the following window (not shown below) other than choosing a relevant Template name and a proper Template description if you want. Then click 'Finnish'

Okay, you did it. Your template is ready for the next part of this tutorial.

Step 2

In the following step, you are going to use your freshly created template to make an application based on that template. That will save you from building the tray application from scratch so we can get right to the Word automation part. Great, isn't it?

Go to: File → New Project and search for your new template (example shown below).

Select the template and create your new project. As you can see, the tray application is already present in your new project. That saves a lot of time coding the tray application from scratch!

Step 3

Depending on where you finished with the original tray application before you made the template, there is an extra step involved. We want to be sure that the tray application has an actual menu. If your application already has a menu, goto step 4 below. Otherwise, proceed as follows. Make sure that the form is visible by double clicking on Form.vb (or whatever the name of your form is) in the Solution Explorer. Now click on the ContextMenuStrip1 item (see picture below).

The context menu appears in the design view of the form. Make sure to create at least one menu item. Making menu items is fairly intuitive in Visual Studio so I will let you work this out yourselves. When done, test the menu by running the application. If you did things right, an icon will appear in the tray that will show your menu structure when right-clicked. If so, you are ready for the next step.

Step 4

The next few steps will be about showing a new form when the user clicks an item of the tray menu system and building some routine in that new form to show Word automation in action. First, we will create the new form.

Right-click on the application's name in the solution explorer. Through the menu that appears, goto: Add ⇒ Windows Form.

Choose a name for the new form. I simply chose form2.vb (the default name presented) and click 'Add'. A new form is added to your project and a form2.vb is shown in design view. Now, switch back to form1.vb and click the ContextMenuStrip1 again. The context menu strip appears on the form again.

In my example, I go to one of the sub menu's created and I double click on one of the items (see picture). You can do the same with one of your own menu items you created earlier.

Double clicking the menu item of the context menu strip brings you to the code editor. Don't worry, the next step is very easy.

Step 5

When the user clicks that menu item, we want to show the new form (form2) we just created. That's easy. Just insert the following code:

Form2.Show()

That's it! Run the application and test it by clicking the relevant menu item from the tray application. Your new Form2 will be shown.

Step 6

Before we move to the Word automation part, we are going to set up the form (form2). The user will provide input by means of text boxes on the form that will capture that input. Go to the form again in Visual Studio.

Make sure that the toolbox is shown on the left side of the Visual Studio program. With the little ping on top of the toolbox window, you can pin the toolbox so that it remains open on the left side (the left side is the default view of the toolbox in Visual Studio). Now, place one button on the lower right side of the form and add 4 textboxes, arranged in rows. Do so by either dragging these elements from the toolbox onto the form, or double click each item in the toolbox so they appear on the form. Rearrange as shown in the picture below. A smart tip is to create one textbox on the form, select it and dragging it while holding Ctrl. Visual Studio will then make a copy of the textbox, making it easy to create a lot of textboxes quickly.

Step 7

The final step before working on Word automation is a quick routine showing how the textboxes can help you capture input from the user. The textboxes on our form are named by default 'TextBox1' , 'TextBox2', 'TextBox3' and 'TextBox4'. You are going to write code to capture any text input from those textboxes and show this input through a standard popup message window.

First, click the button and you are taken to the code behind the button. Insert the following code between the 'Private Sub' and 'End Sub': Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        MsgBox("This is the contents of Textbox 1: " & TextBox1.Text & vbCrLf _
                  & "This is the contents of Textbox 2: " & TextBox2.Text & vbCrLf _
                  & "This is the contents of Textbox 3: " & TextBox3.Text & vbCrLf _
                  & "This is the contents of Textbox 4: " & TextBox4.Text)
    End Sub

Now, run the program, bring up form2, type some text the four textboxes and hit the button. The messagebox shows the contents of the textboxes.

We will use this method in a similar manner in our first Word automation program. That will be covered in the next tutorial.

Computing A-Z | Programming | Software


QR Code
QR Code programming_microsoft_word_-_04_-_from_form_to_word_document (generated for current page)
 

Advertise with Anonymous Ads