How to create an Excel file in VB - vb.net

I need to write a VB class that will create an Excel file and pass into this file a list of variables.
Afterwards I need to be able to convert the program to a .dll so that it can be called by a program.
Its been a while since I have written any code so thought that VB would be a good refresher but all the example I can find all start from within Excel. I need the call to create the file to be from outside of Excel.

I would recommend you look at the OpenXML SDK For Microsoft Office. Unlike many of the examples you'll find that use Microsoft.Office.Interop, this approach does not require Microsoft Office to be installed.
OpenXML SDK 2.0 for Microsoft Office
OpenXML SDK 2.5 for Microsoft Office
There is a complete how-to on MSDN for generating Excel workbooks.

Related

How do you manipulate a MS Word document (.docx) programmatically using vb.net?

I am trying to read from a .docx file (MS Word 2013) using vb.net and find and replace multiple words within the same document. I do not want to make use of a plugin or add-on within vb.net but rather program this myself.
Which libraries or references within vb.net will give me the ability manipulate MS Word documents with the .docx file extension.
Any related vb.net code for Visual Studio 2013 would be greatly appreciated.
I have found the following related information (https://support.microsoft.com/en-us/kb/316383) although I am finding difficulty in adapting it for Visual Studio 2013.
Below is a list of COM references directly from Visual Studio 2013 and the only similar option with reference to the above link seems to be the checked item in the screen print below:
Seems that after a bit of "connecting the dots" from everyone's commentary (thank you) I was missing an imports reference that works with the "Microsoft Word 14.0 Object Library" associated with MS Word 2013.
With reference to https://support.microsoft.com/en-us/kb/316383, the imports reference that I found was adapted from the question: How to load Word Object Library properly in Visual Studio Express i.e. adding the following to the top of the object class solves this question:
Imports Microsoft.Office.Interop
You can use DocX to replace text in one or muliple documents, check the sample code here - Replace text across many documents in Parallel . Hope I am not misunderstanding you.
To run your application you need to have MS Word installed on the PC. I'd consider using the Open XML SDK instead. See Welcome to the Open XML SDK 2.5 for Office for more information.

Sharing VBA modules across MS Office Applications

I have a substantial bank of VBA modules written in an Excel 2010 add-in. Some of these are specific to Excel, but many are more general. For example one takes a part number and re-formats it; another contains a Case Select function to find a file in a network drive.
I want to use the common functions in Word and Outlook. I could copy and paste from the Excel to Word add-ins, but this makes it difficult to keep my code up to date - when I make an edit in one application, I must remember to copy to all the others.
My question is, is there any means of writing common code in one place (e.g. in the Excel add-in, or some other common location) so that all MS Office applications can access it as it if it's just another module?
The way this used to be done was to take your code and compile it into a COM (or ActiveX) DLL using Visual Basic 6. Then you could add that DLL, using the VBA editor's "Tools...References" dialog, from any Office (or other) product that supported VBA, the same way you might use, say, the Microsoft Scripting Runtime, which is super-handy for things like Dictionary, FileSystemObject and TextStream.
Problem is, VB6 was released sometime in 1998 and has not been available from or supported by Microsoft for years now. There seem to be quite a few download sites offering the package - I can't offer any advice about the legality or security issues that might be experienced by using them...
Shamefully, Microsoft dropped the VBA ball years ago - it seems they mostly wish it would just Go Away.
You would need to create a COM add-in (.dll) for that, which would require Visual Studio or some other tool capable of creating COM exposed addins. There hasn't been any facility for this in Office since the old Office Developer edition.
VB6 is best way. VB6 is still supported for compiled programs. The IDE been tested and found to work up to Windows 10 (32 bit only) by MS but is unsupported.
If you want to convert to vbscript you can use wsc files instead of a dll.
From Script Components Overview
Windows® Script Components are an exciting new technology that allows you to create powerful, reusable COM components with easy-to-use scripting languages such as Microsoft® Visual Basic® Scripting Edition (VBScript) and Microsoft® JScript®.
Make one, make a type library, set a reference to typelibrary in Word and Excel.
Maybe can help somebody.
My solution has been set conditional compiler arguments in project properties:
In excel project: SOFT_EXCEL = 1 : SOFT_OUTLOOK = 0
In outlook project: SOFT_EXCEL = 0 : SOFT_OUTLOOK = 1
Then, in module:
Public Sub as_email()
#If SOFT_EXCEL Then
Debug.Print "this executes and compiles in excel"
#End If
#If SOFT_OUTLOOK Then
Debug.Print "this executes and compiles in outlook"
#End If
End Sub

OpenXML with mailmerge fields in a Word DOCX file - full example

I know there are heaps and heaps of threads out there on recommending and using OpenXML to manipulate a Word 2007 (docx) document.
But none I could find, have a practical example (full solution in zip) that I am able to try out and demonstrate to learn quickly how to use OpenXML.
What I am after is a way to open a Word 2007 document template with several mailmerge fields, populate those fields from a dataset (SQL Server 2005) and save the template as a Word document ready to email.
UPDATE: We are using Visual Studio 2005 with .Net 2. Is that going to be a problem? Could we have .Net 3 installed but still use VS2005? Porting to VS2010 is in the cards for us, but not right now. We need a solution to this problem before we go upgrading to 2010.
And please do not recommend 3rd party products as we have to look for a free workable solution.
Much appreciate your time. Thanks
Here's an example.
It uses docx4j.NET - which is open source and which you can get from Nuget - not Microsoft's Open XML SDK.

Fragility of using Excel Object Library in Visual Basic

A bit of preface: this project, while technically homework, is completely under my 5-man team's control. We came up with the project, defined the scope, and exercise full creative control. So it's not traditional homework, it's equivalent to a senior project.
One of the outputs of the system I'm developing should be very similar to a well formatted Excel spreadsheet. We could create (or find) a graphics library and handle the printing in our own code... but ultimately we feel that an Excel spreadsheet file will be more portable. As the user can open it up in Excel, edit it, e-mail it, etc.
Accessing Excel programatically seems simple enough (i.e: http://support.microsoft.com/kb/302094)
But my question is: what happens when "Office 2013" comes out, and the user removes Office 2010 and installs 2013?
I won't be around to maintain this project... and I'd hate to force someone into opening up my code just to reference the Excel 13.0 COM.
I'll be using incredibly basic functions of the API.
So long as I can read-and-write to a range of cells, and adjust the interior color of cells, my code will work.
In general, how can I make my VB.Net code access whatever Microsoft Excel API is available on the target system? (Including future Excel APIs.)
I recommend first developing using the Excel PIA (Primary Interop Assemblies) so you get autocomplete and online help in Visual Studio.
Once your program is complete, I recommend switching to late-binding before publishing, so your EXE works with different versions of Excel.
Develop first using the current Excel PIA until it works as you
want (Google "Excel PIA" to download the assemblies)
When your program is complete, add Option Strict Off to the top of the modules that use Excel objects
Replace all Excel PIA classes with Object e.g.Dim xls As Excel.Application becomesDim xls As Object
Replace xls = New Excel.Applicationwith xls = CreateObject("Excel.Application")
Remove the Excel PIA reference and tidy up

Building a Com addin for Office 2000 / Office 2007

I am struggling to find a straight forward guide to creating office addins using VSTO and VB.net.
Specifically I would like to know how to be able to create a addin/ dll which can either be referenced from VBA in the form:-
Addin.method(argument) or Addin.property = X
Or which would install its own custom toolbars/ ribbon interface to an aspect of office for example Word.
I've checked MSDN and in terms of legibility and usability of the explanations I have drawn a blank so far.
I currently have a requirement to create at least one addin for Office 2000 to run and manipluate SQL and then a suite of addins for a customized Office 2007 (Word) set-up.
Adding global named items to the script engine from VBA code requires the script engine expose itself via IActiveScript::AddNamedItem. I doin't think Microsoft would do it anytime soon as this could break existing VBA code.
Got this working :-)
Using a class libary project and ComInterface / ClassInterface.