Automating VBA code modifications

U

UnkleVo

We need to add one line of VBA script to 350 word documents.

Is there any way to automate this?

For example looping through all this docs from DOTNET program and
access and modify VBA script that way..


Please help.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < UnkleVo > écrivait :
In this message, < UnkleVo > wrote:

|| We need to add one line of VBA script to 350 word documents.
||
|| Is there any way to automate this?
||
|| For example looping through all this docs from DOTNET program and
|| access and modify VBA script that way..
||

Here is an code sample to get you going:
(This a sample to programmatically create a macro procedure, but you can
easily modify to edit an existing macro instead.)

'_______________________________________
Sub CreateMacro()
'Originally posted by Dave Lett
Dim strVBProj As String
Dim strVBMod As String
' Specify the project name for your template or document
' and module in the project to store the macro.
strVBProj = "Project"
strVBMod = "Module1"
On Error GoTo cmErrHandler
With VBE.VBProjects(strVBProj).VBComponents(strVBMod).CodeModule
.DeleteLines Startline:=1, Count:=.CountOfLines
' you might want to investigate the addfromfile method
' you might want to investigate the replaceline method
.InsertLines Line:=1, String:="Sub MyMacro"
.InsertLines Line:=2, String:=" ' Created by code."
.InsertLines Line:=3, String:="End Sub"
End With
cmErrHandler:
If Err.Number <> 0 Then
MsgBox "Error: Specified Project and/or Module does not exist."
End If
End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Marcel

Jean-Guy Marcil said:
Bonjour,

Dans son message, < UnkleVo > écrivait :
In this message, < UnkleVo > wrote:

|| We need to add one line of VBA script to 350 word documents.
||
|| Is there any way to automate this?
||
|| For example looping through all this docs from DOTNET program and
|| access and modify VBA script that way..
||

Here is an code sample to get you going:
(This a sample to programmatically create a macro procedure, but you can
easily modify to edit an existing macro instead.)

'_______________________________________
Sub CreateMacro()
'Originally posted by Dave Lett
Dim strVBProj As String
Dim strVBMod As String
' Specify the project name for your template or document
' and module in the project to store the macro.
strVBProj = "Project"
strVBMod = "Module1"
On Error GoTo cmErrHandler
With VBE.VBProjects(strVBProj).VBComponents(strVBMod).CodeModule
.DeleteLines Startline:=1, Count:=.CountOfLines
' you might want to investigate the addfromfile method
' you might want to investigate the replaceline method
.InsertLines Line:=1, String:="Sub MyMacro"
.InsertLines Line:=2, String:=" ' Created by code."
.InsertLines Line:=3, String:="End Sub"
End With
cmErrHandler:
If Err.Number <> 0 Then
MsgBox "Error: Specified Project and/or Module does not exist."
End If
End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org

Thanks Jean-Guy,

I haven't seen code like this before.

Marcel
 
M

Marcel

Jean-Guy Marcil said:
Bonjour,

Dans son message, < UnkleVo > écrivait :
In this message, < UnkleVo > wrote:

|| We need to add one line of VBA script to 350 word documents.
||
|| Is there any way to automate this?
||
|| For example looping through all this docs from DOTNET program and
|| access and modify VBA script that way..
||

Here is an code sample to get you going:
(This a sample to programmatically create a macro procedure, but you can
easily modify to edit an existing macro instead.)

'_______________________________________
Sub CreateMacro()
'Originally posted by Dave Lett
Dim strVBProj As String
Dim strVBMod As String
' Specify the project name for your template or document
' and module in the project to store the macro.
strVBProj = "Project"
strVBMod = "Module1"
On Error GoTo cmErrHandler
With VBE.VBProjects(strVBProj).VBComponents(strVBMod).CodeModule
.DeleteLines Startline:=1, Count:=.CountOfLines
' you might want to investigate the addfromfile method
' you might want to investigate the replaceline method
.InsertLines Line:=1, String:="Sub MyMacro"
.InsertLines Line:=2, String:=" ' Created by code."
.InsertLines Line:=3, String:="End Sub"
End With
cmErrHandler:
If Err.Number <> 0 Then
MsgBox "Error: Specified Project and/or Module does not exist."
End If
End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org



Hi,

In my case I get an security warning that the following statement is not
trusted!

With VBE.VBProjects(strVBProj).VBComponents(strVBMod).CodeModule

and the executions stops.

Regards,

Marcel
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Marcel > écrivait :
In this message, < Marcel > wrote:

|| Hi,
||
|| In my case I get an security warning that the following statement is not
|| trusted!
||
|| With VBE.VBProjects(strVBProj).VBComponents(strVBMod).CodeModule
||
|| and the executions stops.
||

What security level is currently set?
Did you check the option to trust add-ins and/or visual basic project (Tools
Macro > Security > Trusted Sources tab)?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Marcel

Jean-Guy Marcil said:
Bonjour,

Dans son message, < Marcel > écrivait :
In this message, < Marcel > wrote:

|| Hi,
||
|| In my case I get an security warning that the following statement is not
|| trusted!
||
|| With VBE.VBProjects(strVBProj).VBComponents(strVBMod).CodeModule
||
|| and the executions stops.
||

What security level is currently set?
Did you check the option to trust add-ins and/or visual basic project (Tools

The security level is low, but I see that in the second tab the second
checkbox is unchecked. I checked it and the instruction is accepted.

Thanks Jean-Guy for your effort.

Marcel
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Marcel > écrivait :
In this message, < Marcel > wrote:

|| The security level is low, but I see that in the second tab the second
|| checkbox is unchecked. I checked it and the instruction is accepted.
||
|| Thanks Jean-Guy for your effort.
||

Glad I could help!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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