Avatar of gycitsupport
gycitsupport

asked on 

How to run a VB macro in Word from C#

Hi
I would like to write a C# application where the user can copy and paste a macro from a Word document which would then be run against a number of Word documents in a particular folder. I can open and close Word documents through C#, rotate through the files and even run an existing macro but I have the problem that I don't want the user to have to create the macro on each document (this would be a waste of time really) but to be able to copy and paste a working macro into a textbox say and then run it against the other docs.

Thanks for any help!
ProgrammingMicrosoft WordC#

Avatar of undefined
Last Comment
gycitsupport
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Word macros run in the VBA environment. Editing them programatically will cause a Security message to be shown. It is hard to see how putting the code in a text box will help, unless you want it printed with the document.

It might be better to leave the macro in the template, and attach the template if it is not the already so attached.
Avatar of gycitsupport
gycitsupport

ASKER

Hi
What I really was interested in was whether the .NET word object had an ability to run code as a macro outwith Word itself - e.g. apply it to the Word documents as they were opened and then saved and closed. Do you not think this would be possible?  If not, would a VBA script in say, Access, be better?

Thanks.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

I don't do .net, but but I understand that COM (OLE Automation) is still supported.

If you don't use VB.net, you could translate the macro to C# code.

Here is an example that does a mail merge from C#:

https://www.experts-exchange.com/questions/21767403/Automating-an-MS-Word-Mail-Merge-using-C.html
Avatar of gycitsupport
gycitsupport

ASKER

Thanks for your help. It appears that C# may not be the way to go. Would an Access front end with VBA be a better way to go? I really need my end user to be able to copy and paste in a simple macro (e.g  to bold all text in  a paragraph) so that it can then be used on all documents rather than adding any C# amendments.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gycitsupport
gycitsupport

ASKER

Thanks for your help.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo