Walkthrough: Collecting Data Using a Windows Form
This walkthrough demonstrates how to open a Windows Form from a document-level customization for Microsoft Office Excel, collect information from the user, and write that information into a worksheet cell.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects. See Features Available by Office Application and Project Type.
Although this walkthrough uses a document-level project for Excel specifically, the concepts demonstrated by the walkthrough are applicable to other Office projects.
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configuring a Computer to Develop Office Solutions.
Excel 2013 or Excel 2010.
The first step is to create an Excel Workbook project.
Create an Excel Workbook project with the name WinFormInput, and select Create a new document in the wizard. For more information, see How to: Create Office Projects in Visual Studio.
Visual Studio opens the new Excel workbook in the designer and adds the WinFormInput project to Solution Explorer.
Select cell A1 on
In the Name box, type formInput.
The Name box is located to the left of the formula bar, just above column A of the worksheet.
A NamedRange control is added to cell A1. There is no visible indication on the worksheet, but formInput appears in the Name box (just above the worksheet on the left side) and in the Properties window when cell A1 is selected.
Create a Windows Form to prompt the user for information.
Select the project WinFormInput in Solution Explorer.
On the Project menu, click Add Windows Form.
Name the form GetInputString.vb or GetInputString.cs, and then click Add.
The new form opens in the designer.
Select the button, find the property Text in the Properties window, and change the text to OK.
Next, add code to
ThisWorkbook.cs to collect the user's information.
Create an instance of the
GetInputString Windows Form and display it, and then write the user's information into a cell in the worksheet.
Right-click ThisWorkbook.vb or ThisWorkbook.cs in Solution Explorer, and then click View Code.
In the Open event handler of
ThisWorkbook, add the following code to declare a variable for the form
GetInputStringand then show the form.
Create a method called
WriteStringToCellthat writes text to a named range. This method is called from the form, and the user's input is passed to the NamedRange control,
formInput, on cell A1.
Next, add code to the form to handle the button's click event.
Right-click GetInputString in Solution Explorer, and then click View Designer.
Double-click the button to open the code file with the button's Click event handler added.
Add code to the event handler to take the input from the text box, send it to the function
WriteStringToCell, and then close the form.
You can now run the project. The Windows Form appears, and your input appears in the worksheet.
Press F5 to run your project.
Confirm that the Windows Form appears.
Type Hello World in the text box, and then click OK.
Confirm that Hello World appears in cell A1 of the worksheet.
This walkthrough shows the basics of showing a Windows Form and passing data to a worksheet. Other tasks you may want to perform include:
Use Windows Forms controls on an Excel workbook or a Word document. For more information, see Windows Forms Controls on Office Documents Overview.
Modify the user interface of a Microsoft Office application from a document-level customization or an VSTO Add-in. For more information, see Office UI Customization.