PDA

View Full Version : Solved: How to "replace" an existing module from a BAS file



hkeiner
10-23-2010, 12:12 PM
In short, how do I write a macro that will "replace" an existing module (modCustomModule) containing the same name as the imported module (modCustomModule), rather than adding a duplicate module to the active document?

A longer explanation: I am trying to create an AutoOpen macro that will automatically import/update a custom module (modCustomModule) in to Word 2007 documents when they are opened by certain users. To do this, I save (export) the latest version of the custom module to a BAS file (CustomModule.bas) located on a shared server (G./) and then have the below code (located in an AutoOpen macro of the involved users) do the importing. The AutoOpen macro works OK if the custom module does not already exist in the opened document, but if a previous version of the module with the same name already exists on the document, it creates another copy of the module (adding a suffix to the module name) rather than replacing the existing module.


Thanks in advance for any advice. A little error control code would be helpful too, such as if the server is offline and the BAS file can not be imported successfully.


Sub AutoOpen()
Dim GetDoc As Document
Set GetDoc = ActiveDocument
GetDoc.VBProject.VBComponents.Import "G:\CustomModule.bas"

End Sub

gmaxey
10-24-2010, 07:30 AM
Something like this:

Sub AutoOpen()
Dim GetDoc As Document
Dim vbCom As Object
Set GetDoc = ActiveDocument
Set vbCom = GetDoc.VBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("Test")
GetDoc.VBProject.VBComponents.Import "C:\Test.bas"
End Sub






In short, how do I write a macro that will "replace" an existing module (modCustomModule) containing the same name as the imported module (modCustomModule), rather than adding a duplicate module to the active document?

A longer explanation: I am trying to create an AutoOpen macro that will automatically import/update a custom module (modCustomModule) in to Word 2007 documents when they are opened by certain users. To do this, I save (export) the latest version of the custom module to a BAS file (CustomModule.bas) located on a shared server (G./) and then have the below code (located in an AutoOpen macro of the involved users) do the importing. The AutoOpen macro works OK if the custom module does not already exist in the opened document, but if a previous version of the module with the same name already exists on the document, it creates another copy of the module (adding a suffix to the module name) rather than replacing the existing module.


Thanks in advance for any advice. A little error control code would be helpful too, such as if the server is offline and the BAS file can not be imported successfully.


Sub AutoOpen()
Dim GetDoc As Document
Set GetDoc = ActiveDocument
GetDoc.VBProject.VBComponents.Import "G:\CustomModule.bas"

End Sub

hkeiner
10-24-2010, 09:50 AM
Sub AutoOpen()
Dim GetDoc As Document
Dim vbCom As Object
Set GetDoc = ActiveDocument
Set vbCom = GetDoc.VBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("Test")
GetDoc.VBProject.VBComponents.Import "C:\Test.bas"
End Sub


Thanks. Your code is a great start for me.

The above code works fine if the file (Test.bas) is located on a local drive (or other volume that is ALWAYS available to the user when the AutoOpen macro is run). The "old" module is removed OK and then the "new" module is imported OK and has the same name as the old module. However, I need to add some error control to keep from messing up the document when the volume with the BAS file is not available to the user at the time the AutoOpen macro is run.

In my environemnt, the volume with the BAS file is a shared networked volume that may not always be available to the users when the AutoOpen macro is run (such as when a user has not logged in to the Novell network or the Novell network is temporarily down). In this situation, the old module would be deleted OK but it would not not replaced with the new module. In this case, a "replacement" does not really occur and the document contains neither the old or new module. Not OK. I am thinking that some type of ON ERROR or IF test would be needed to test for this condition before the Remove occurs.

Also, as a further refinement is there a way to stop the remove/import from occuring if it is not needed, such as when the new module is the same as the old module? The "new" module is not changed very often (perhaps once a month) while hundreds of documents are opened and closed daily by users. I am thinking that stopping the remove/import activity when it is not needed would cut down on unnecessary network traffic and lag/delay in opening documents on a daily basis. Perhaps there is a property of the BAS file (such as date last edited) that could be read and evaluated before the remove/imports commands are performed (or not).

The advice I get on this forum is great and much appreciated...

gmaxey
10-24-2010, 10:03 AM
Try this:

Sub AutoOpen()
Dim GetDoc As Document
Dim vbCom As Object
Set GetDoc = ActiveDocument
Set vbCom = GetDoc.VBProject.VBComponents
On Error Resume Next
GetAttr ("C:\Test.bas")
If Err.Number = 0 Then
On Error GoTo 0
vbCom.Remove VBComponent:=vbCom.Item("Test")
GetDoc.VBProject.VBComponents.Import "C:\Test.bas"
Else
MsgBox "The replacement module is not available"
End If
End Sub

I don't know how you would compare the two modules for changes.



Sub AutoOpen()
Dim GetDoc As Document
Dim vbCom As Object
Set GetDoc = ActiveDocument
Set vbCom = GetDoc.VBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("Test")
GetDoc.VBProject.VBComponents.Import "C:\Test.bas"
End Sub


Thanks. The above code works fine if the file (Test.bas) is located on a local drive (or other volume that is ALWAYS available to the user when the AutoOpen macro is run). The "old" module is removed OK and then the "new" module is imported OK and has the same name as the old module. Your code is a great start for me, but I need to add some error control to keep from messing up the document when the volume with the BAS file is not available to the user at the time the AutoOpen macro is run.

In my environemnt, the volume with the BAS file is a shared networked volume that may not always be available to the users when the AutoOpen macro is run (such as when a user has not logged in to the Novell network or the Novell network is temporarily down). In this situation, the old module would be deleted OK but it would not not replaced with the new module. In this case, a "replacement" does not really occur and the document contains neither the old or new module. Not OK. I am thinking that some type of ON ERROR or IF test would be needed to test for this condition before the Remove occurs.

Also, as a further refinement is there a way to stop the remove/import from occuring if it is not needed, such as when the new module is the same as the old module? The "new" module is not changed very often (perhaps once a month) while hundreds of documents are opened and closed daily by users. I am thinking that stopping the remove/import activity when it is not needed would cut down on unnecessary lag/delay when opening documents. Perhaps there is a property of the BAS file (such as date last edited) that could be read and evaluated before the remove/imports commands are performed (or not).

The advice I get on this forum is great and much appreciated...

hkeiner
10-24-2010, 10:21 AM
Greg,

Thanks for the added error code. This should take care of my primary concern (that the documents will get messed up) and I plan to go live with this code soon.

Once I get the code implemented, I'll see if my second concern (excessive network traffic and delay/lag in opening documents) is a noticable problem or not. If it is, I'll repost a new question on that specific matter.

Thanks again.

hkeiner
10-25-2010, 03:46 PM
I implemented the suggested code and it works great for "replacing" the modules on the active document. I also was able to adapt it for replacing some userforms on the same active documents. Thanks so much for the code.

As it happens, I also have a module on the Normal template that I would like to "replace" using the same methodology. I would like to adapt your code for this purpose but I have no idea how to specify the Normal template (instead of the active document) so that the remove and import commands work. Hopefully this can be done with modules on the Normal template too.

Any advice on this would be appreciated.



Sub AutoOpen()
Dim GetDoc As Document
Dim vbCom As Object
Set GetDoc = ActiveDocument
Set vbCom = GetDoc.VBProject.VBComponents
On Error Resume Next
GetAttr ("C:\Test.bas")
If Err.Number = 0 Then
On Error GoTo 0
vbCom.Remove VBComponent:=vbCom.Item("Test")
GetDoc.VBProject.VBComponents.Import "C:\Test.bas"
Else
MsgBox "The replacement module is not available"
End If
End Sub

gmaxey
10-25-2010, 04:11 PM
Sub AutoOpen()
Dim oTmp As Template
Dim vbCom As Object
Set oTmp = NormalTemplate
Set vbCom = oTmp.VBProject.VBComponents
On Error Resume Next
GetAttr ("C:\Test.bas")
If Err.Number = 0 Then
On Error GoTo 0
vbCom.Remove VBComponent:=vbCom.Item("Test")
oTmp.VBProject.VBComponents.Import "C:\Test.bas"
Else
MsgBox "The replacement module is not available"
End If
End Sub