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

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.

Related

How can I expose Word VBA macro code as text files in VSC to better commit and track code changes?

Background and Problem
I lead a team of tech writers. Our team has a Word template (.dotm file) that has a bunch of VBA macros we've created over the years to help us automate tasks in Word. The macros are obviously stored inside of a .dotm template. Given that the code is stored in a binary file, the code is not easily traceable across commits.
Currently, we have to pull the changes on the branch and then open the macro inside Word's embedded code editor from the .dotm. But there's no way to see what exact code changes someone else made line by line or resolve conflicts using something like Beyond Compare.
What I Need
I'd like to find a way to automatically expose the VBA code from the .dotm as some kind of text file so that we can:
Do Word VBA coding tasks within VSC
Easily track and review each others code changes at the line level using our git repository
Resolve merge conflicts more easily
Has anyone found a way to do this?
What I've tried
I've tried searching online and on stackoverflow.
Hoping it might help, I installed ScriptLab, and I after following the MS tutorial, I now have Office Add-Ons working with VSC.
(https://code.visualstudio.com/docs/other/office),
(https://learn.microsoft.com/en-us/office/dev/add-ins/tutorials/word-tutorial) While the Office Add-Ons through VSC does provide an alternate (more modern) way of automating using Javascript, and the .js files are commit-able, it's not providing a way to expose the Word VBA, at least not that I could see.
Thank you in advance!
ScriptLab add-on is for playing with JavaScript code instantly, not designed as a macro infrastructure in Office.
Sounds like you need to switch your VBA macros to COM add-ins rails. There is no converters that can do this part of work for you automatically.
You may consider creating a VB.NET based add-in. In that case you will avoid language translating issues. You may find the Converting Code from VBA to Visual Basic .NET article helpful. It presents issues to consider when converting your Microsoft Office solutions from Visual Basic for Applications (VBA) to Visual Basic .NET. Provides a general overview of their differences, and then uses examples from Word and Excel to describe how you can use Visual Studio Tools for the Microsoft Office System to convert your code.

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.

How to create an Excel file in VB

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.

Storing excel data on Sharepoint

I'm looking for a way to store data generated by an excel template somewhere on SharePoint 2010. I am very new to SharePoint and still learning some of the abilities etc.
The goal is to store the data created by an excel template when a staff creates a quote using the template. I want to have the quotes available for historic purposes and compare the data.
I understand that SharePoint may not be the correct solution and that there exist much better platforms but right now I'm stuck with SharePoint. Any advice would be appreciated.
I found the answer to this. SharePoint has a number of webservices that can be used. After doing a heap of searching I found that using the Visual Studio Professional a Excel Workbook Project can be setup with embedded Visual C# code. I am currently implementing this.

Tools to manipulate doc file and convert them to pdf

I am looking for some good tools (free or paid, though free tool is always preferred)
for doing following operations on word doc files:
Manipulation of doc/docx/text files (like replacing some placeholders with DB values) as well as
converts doc files to .pdf
Because, I will be using this tool in my WCF service library,
So I am looking for a code library and not for a GUI based product.
Please share your experience regarding same.
Thank you!
Aspose has a decent collection of MS Office and PDF manipulation libraries.
Aspose Homepage
On the off chance that you're only looking for PDFs for viewing or archival purposes, you could also setup a PDF print driver and print your office files into a given location using Automation. You could also edit Office files through Automation although this may be tedious.
VSTO would give you access to the save as PDF from the Office applications.
Please see my answer to a related question on SO where I recommend a number of ways to convert your Word document to a format that is more easy to manipulate programmatically (using XSL-FO).