Using C# instead of VBA

  • Thread starter Jens Gyldenkærne Clausen
  • Start date
J

Jens Gyldenkærne Clausen

Hi group. I'm having a hard time programming a Word template. I'm
looking for code to manage the contentcontrols in a Document, but the
samples I can find in Word Developer Reference are all in VBA whereas I
would like to work in C# (which is also the language and code template
that Visual Studio's Word Template Project starts up with).

I think I'm missing some basic understandning of the possibilities with
VBA compared to .NET/C# in programming a Word template. Can anybody
guide me towards the right way(tm) to build a Word Template Project?
 
J

Jean-Guy Marcil

Jens Gyldenkærne Clausen said:
Hi group. I'm having a hard time programming a Word template. I'm
looking for code to manage the contentcontrols in a Document, but the
samples I can find in Word Developer Reference are all in VBA whereas I
would like to work in C# (which is also the language and code template
that Visual Studio's Word Template Project starts up with).

I think I'm missing some basic understandning of the possibilities with
VBA compared to .NET/C# in programming a Word template. Can anybody
guide me towards the right way(tm) to build a Word Template Project?

If you are going to use VisualStudio, you can use Visual Basic instead of
C#. While Visual Basic .Net is different from VBA, it will be a lot easier to
adapt the samples from the Word Developer Reference or the code from the
suggestions you may get from these groups. C# is not related to VBA in any
ways, Visual Basic .Net is an "offspring" of VB 6, which is also the "father"
of VBA... in a manner of speaking...
 
J

Jens Gyldenkærne Clausen

Jean-Guy Marcil said:
If you are going to use VisualStudio, you can use Visual Basic instead of
C#. While Visual Basic .Net is different from VBA, it will be a lot easier to
adapt the samples from the Word Developer Reference or the code from the
suggestions you may get from these groups. C# is not related to VBA in any
ways, Visual Basic .Net is an "offspring" of VB 6, which is also the "father"
of VBA... in a manner of speaking...

Thanks for the reply. I'm aware of the similarities between VB.NET and
VBA - and have experience in programming VB.NET as well. But my problem
is not really the difference in language syntax (although the subject
might imply so) - but the scopes of the underlying object models/API's.
As far as I can see, it should be possible to program Word using either
VBA (from "inside" Word) or a .NET language (from "outside"). But I
can't see the clear border between these two approaches.
 
J

Jay Freedman

Jens said:
Thanks for the reply. I'm aware of the similarities between VB.NET and
VBA - and have experience in programming VB.NET as well. But my
problem is not really the difference in language syntax (although the
subject might imply so) - but the scopes of the underlying object
models/API's. As far as I can see, it should be possible to program
Word using either VBA (from "inside" Word) or a .NET language (from
"outside"). But I can't see the clear border between these two
approaches.

Regardless of whether you're using C# or VB.Net in your application, you're
going to have to deal with the object model of the application that you're
automating. Because the Office object models are rooted in VBA and (mostly)
VB 6, there will always be some translation involved. I don't think there is
any "clear border" -- there is no C# function or API that operates
independently of the object model.*

There's an article in the current issue of MSDN Magazine, which you can read
at http://msdn.microsoft.com/en-us/magazine/cc507643.aspx, that discusses
translating a VBA macro to VSTO in C#. It doesn't give a lot of help, but
it's a starting point.

*If you want to manipulate the XML-based files from Office 2007 outside the
Office applications, you can do that entirely in C#. The Open XML Format SDK
is available at
http://www.microsoft.com/downloads/...FB-4A1D-4C52-BDB5-7DD7E816D046&displaylang=en
as a technology preview.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jonathan West

Jens Gyldenkærne Clausen said:
Thanks for the reply. I'm aware of the similarities between VB.NET and
VBA - and have experience in programming VB.NET as well. But my problem is
not really the difference in language syntax (although the subject might
imply so) - but the scopes of the underlying object models/API's. As far
as I can see, it should be possible to program Word using either VBA (from
"inside" Word) or a .NET language (from "outside"). But I can't see the
clear border between these two approaches.

This article may be of interest

Control Word from Excel
http://www.word.mvps.org/FAQs/InterDev/ControlWordFromXL.htm

Again, it is controlling Word from another VBA application, but it does
demonstrate the concept.

The Word object model is exactly the same irrespective of whether you access
it from VBA, VB.NET or C#. The syntax varies according to the language you
use. For instance, C# doesn't use named arguments. That means that you need
to list all the arguments when calling from C# and specify most of them with
the value indicating the object model should treat the argument as missing.

The Word object model help provides its code examples in VBA. In order to
make use of the Word object model from C#, you are simply going to have to
translate it yourself. There may be a few C# code examples floating around,
but not the kind of fairly comprehensive set that you get for VBA with the
help.
 
C

Cindy M.

Hi Jens,
I'm having a hard time programming a Word template. I'm
looking for code to manage the contentcontrols in a Document
Have you looked at the examples in the VSTO documentation?
http://msdn.microsoft.com/en-us/library/bb157891.aspx

You may also find some code examples by searching in the VSTO
forum
http://forums.microsoft.com/MSDN/ShowForum.aspx?
ForumID=16&SiteID=1&PageID=0

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top