Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to replace vba code in one excel workbook from another workboo

Hi all,
Let say that I have over 2000 old excel files in one folder. Each of these
excel files contain one macro and this is the same macro in all excel files.

I want to be able to create a script that will open each file one by one,
delete or overwrite the current macro by a new one.

All my code to open excel files is done and working. My problem is that I
don't know
how to retrieve the current macro once the file is open. Neither how to
overwrite it.

Any help will be greatly appreciated.

Christian
  #2   Report Post  
Posted to microsoft.public.excel.programming
ste ste is offline
external usenet poster
 
Posts: 6
Default How to replace vba code in one excel workbook from another workboo

Hi Christian,

this is just the opposite ... : )
but with a bit of editing... I guess it's the right way

Sub DeleteMyCode()
'On Error Resume Next

Dim m_ThisWB
Set m_ThisWB =
ThisWorkbook.VBProject.VBComponents.Item(1).Collec tion("ThisWorkbook").CodeModule

m_ThisWB.deletelines 4, m_ThisWB.countoflines
m_ThisWB.deletelines 2

DeleteComponent "Mod1"
DeleteComponent "Mod2"
DeleteComponent "Class1"

ThisWorkbook.Save
End Sub

Sub DeleteComponent(sz_CompName As String)
Dim vbc
Set vbc = ThisWorkbook.VBProject.VBComponents.Item(sz_CompNa me)
ThisWorkbook.VBProject.VBComponents.Remove vbc
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to replace vba code in one excel workbook from another wor

Hi ste,
thank's very much, you put me on the right way.
Here is a part of code I use to do what I want. There is more with it in an
hta.
Again, thank's for your help.

'--------------------------------------------------------------------------------------------------
'
================================================== ===============================================
' TITLE: ReplaceMacro
'
' PURPOSE: Open each Excel files in a specified folder, Delete all
vbcode in ThisWorkbook.
' Open a text file containing a new vbcode and insert it in module
' ThisWorkbook in each Excel files. Save the file and close Excel.
'
' PARAMETERS: strSourceFolder [STRING] = Folder containing Excel files to
work with.
'
' HOW TO USE: ReplaceMacro strSourceFolder
'
================================================== ===============================================
Sub ReplaceMacro(strSourceFolder)
Dim objFSO, objFolder, objFile, objComps, objComp
'Instantiate objects.
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Connect to folder to work with.
Set objFolder = objFSO.GetFolder(strSourceFolder)

'Loop through all files in current folder.
For Each objFile In objFolder.Files
ProgressWindow strSourceFolder & "\" & objFile.Name, "Open"
objXLS.Workbooks.Open strSourceFolder & "\" & objFile.Name 'Open Excel file.
Set objComps = objXLS.ActiveWorkbook.VBProject.VBComponents 'Connect to
VBE module.
ProgressWindow strSourceFolder & "\" & objFile.Name, "Del"
'Delete vb code in ThisWorkbook Project.
DelMacro objComps, objComp
'Insert module code in module named ThisWorkbook.
ProgressWindow strSourceFolder & "\" & objFile.Name, "Insert"
AddProcedure objComps
ProgressWindow strSourceFolder & "\" & objFile.Name, "Save"
objXLS.ActiveWorkbook.Save 'Save workbook and close it.
Next
End Su
'--------------------------------------------------------------------------------------------------
'
================================================== ===============================================
' TITLE: DelMacro
'
' PURPOSE: Delete all macro code in an Excel file.
'
' PARAMETERS: vbComps [OBJECT] = Contain components code module collection.
' cbComp [OBJECT] = Contain Vbcode itself in a module.
'
' HOW TO USE: DelMacro objComps, objComp
'
================================================== ===============================================
Sub DelMacro(vbComps, vbComp)
'Delete vb code in ThisWorkbook Project.
For Each vbComp In vbComps
Select Case vbComp.Name
Case "ThisWorkbook" 'If name of current module is ThisWorkbook,
Delete code in it.
With vbComp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next
End Su
'--------------------------------------------------------------------------------------------------
'
================================================== ===============================================
' TITLE: AddProcedure
'
' PURPOSE: Add a new procedure in module ThisWorkbook based on a text
file.
'
' PARAMETERS: vbComps [OBJECT] = Contain components code module collection.
'
' HOW TO USE: AddProcedure objComps
'
================================================== ===============================================
Sub AddProcedure(objVBModule)
On Error Resume Next
Dim objFSO
Dim intLineNum
Dim strCode

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open code.txt file which is the template macro code for Excel to copy in
module Achat.
Set strTemplFile = objFSO.OpenTextFile(SrcTemplateFolderName.Value &
"\code.txt")
'Connect to module Achat.
Set objVbModule =
objXLS.ActiveWorkbook.VBProject.VBComponents("This Workbook").CodeModule
'Loop through each lines in code.txt file and add each line in module Achat.
Do Until strTemplFile.AtEndOfStream
'Read line one by one.
strCode = strTemplFile.ReadLine
intLineNum = intLineNum + 1 'Increment line number counter.
On Error Resume Next
objVbModule.InsertLines intLineNum, strCode 'Add line of code in module.
Loop
'Clean up objects.
strTemplFile.Close
Set objFSO = Nothing
End Sub

"ste" wrote:

Hi Christian,

this is just the opposite ... : )
but with a bit of editing... I guess it's the right way

Sub DeleteMyCode()
'On Error Resume Next

Dim m_ThisWB
Set m_ThisWB =
ThisWorkbook.VBProject.VBComponents.Item(1).Collec tion("ThisWorkbook").CodeModule

m_ThisWB.deletelines 4, m_ThisWB.countoflines
m_ThisWB.deletelines 2

DeleteComponent "Mod1"
DeleteComponent "Mod2"
DeleteComponent "Class1"

ThisWorkbook.Save
End Sub

Sub DeleteComponent(sz_CompName As String)
Dim vbc
Set vbc = ThisWorkbook.VBProject.VBComponents.Item(sz_CompNa me)
ThisWorkbook.VBProject.VBComponents.Remove vbc
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How to replace vba code in one excel workbook from another workboo

Hi Chris,

To cycle through the files, see VBA help on the Dir function or adapt Ron de
Bruin's sample code at:

http://www.rondebruin.nl/copy3.htm

To delete, amend or insert code, see Chip Pearson's page on programming to
the VBE at:

http://www.cpearson.com/excel/vbe.htm

I would suggest, however, that rather than replacing the existing code in
each of the 2000 workbooks, you should consider creating a single Addin and
making the Addin available to all appropriate users.

In this way, future editing could be limited to a single file,


---
Regards,
Norman



"ChrisSaw" wrote in message
...
Hi all,
Let say that I have over 2000 old excel files in one folder. Each of these
excel files contain one macro and this is the same macro in all excel
files.

I want to be able to create a script that will open each file one by one,
delete or overwrite the current macro by a new one.

All my code to open excel files is done and working. My problem is that I
don't know
how to retrieve the current macro once the file is open. Neither how to
overwrite it.

Any help will be greatly appreciated.

Christian



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to replace vba code in one excel workbook from another wor

I've already visited these sites and they are very helpful. But in this case,
this customer want an external solution to manage his excel files because it
was a one
shot deal.

So I've built a vbscript in an hta to do this.
But thank's for your suggestion I appreciate.

"Norman Jones" wrote:

Hi Chris,

To cycle through the files, see VBA help on the Dir function or adapt Ron de
Bruin's sample code at:

http://www.rondebruin.nl/copy3.htm

To delete, amend or insert code, see Chip Pearson's page on programming to
the VBE at:

http://www.cpearson.com/excel/vbe.htm

I would suggest, however, that rather than replacing the existing code in
each of the 2000 workbooks, you should consider creating a single Addin and
making the Addin available to all appropriate users.

In this way, future editing could be limited to a single file,


---
Regards,
Norman



"ChrisSaw" wrote in message
...
Hi all,
Let say that I have over 2000 old excel files in one folder. Each of these
excel files contain one macro and this is the same macro in all excel
files.

I want to be able to create a script that will open each file one by one,
delete or overwrite the current macro by a new one.

All my code to open excel files is done and working. My problem is that I
don't know
how to retrieve the current macro once the file is open. Neither how to
overwrite it.

Any help will be greatly appreciated.

Christian






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default How to replace vba code in one excel workbook from another workboo

Christian,

If the code is exactly the same in all these workbooks, it may make
sense to create an addin to contain the code. You should save code
maintenance headaches over time.

Paul

-------------------------------------------------------------------------

Hi all,
Let say that I have over 2000 old excel files in one folder. Each of these
excel files contain one macro and this is the same macro in all excel files.

I want to be able to create a script that will open each file one by one,
delete or overwrite the current macro by a new one.

All my code to open excel files is done and working. My problem is that I
don't know
how to retrieve the current macro once the file is open. Neither how to
overwrite it.

Any help will be greatly appreciated.

Christian


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
data from one workbook automatically entered into another workboo. cfrt Excel Discussion (Misc queries) 1 September 10th 08 01:11 AM
transfer formulas/formats from 2007 workbook to a new 2008 workboo jodsbug Excel Worksheet Functions 1 November 28th 07 01:38 PM
How to find/replace all of the names of files in EXCEL workbook. Peter Multach Excel Discussion (Misc queries) 2 March 5th 07 02:52 PM
Tranferring names defined in one excel workbook to another workboo Dinesh Excel Discussion (Misc queries) 1 July 1st 06 05:34 AM
Can you code a macro so it runs multiple times in the same workboo dpmac Excel Discussion (Misc queries) 2 April 18th 06 07:40 PM


All times are GMT +1. The time now is 02:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"