Refresh modules after importing/replacing source code

A

Alex

Hi,

(sorry for my very bad English !)

I'm using Word 97 on NT4 (ok, our boss takes his time to
upgrade our softwares !).

I'm creating a macro "UpgradeSourceCode" (called by the
AutoOpen macro) to upgrade the VBA source code while
opening the document:
this macro reads all .bas files (the up-to-date modules)
in a specific folder and:
- replaces the content of the existing modules by the code
found in the corresponding file
- imports the new modules
The only module that is not upgraded is the module where
UpgradeSourceCode and AutoOpen are.

Everything is working fine when I call UpgradeSourceCode
only.
But if AutoOpen call a macro located in an upgraded module
after calling UpgradeSourceCode, I get a Dr Watson (access
violation) !
I think the problem is that the source code is not
refreshed after the upgrade. I don't find any way to
refresh or reload it.
Can anybody help me ??

Alex

Below my code:
-----------------------------------------------------------
Sub AutoOpen()
UpgradeSourceCode
'Dr Watson appears just here (while CALLING
DocOpening, *NOT* while RUNNING DocOpening)
DocOpening ' <-- A macro located in an upgraded module
End Sub

Sub UpgradeSourceCode()
'Upgrade the source modules
Const strPath As String = "\\rixsffs04
\RditProdProg\RditPrPrMpr\"

Dim bolError As Boolean
Dim bolFound As Boolean
Dim bolExists As Boolean
Dim strFileName As String
Dim strModuleName As String
Dim comp As VBComponent
Dim i As Integer

On Error GoTo ErrUpgradeSourceCode
Application.StatusBar = "Upgrading the source code..."
With Application.VBE.ActiveVBProject
strFileName = Dir(strPath & "*.bas")
If strFileName <> "" Then
Do
strModuleName = Left(strFileName, Len
(strFileName) - 4)
bolFound = False
For i = 1 To .VBComponents.Count
If .VBComponents(i).Name =
strModuleName Then
bolFound = True
Exit For
End If
Next i
If bolFound Then
'The module exists -> replace his code
.VBComponents
(strModuleName).CodeModule.DeleteLines _
StartLine:=1, Count:=.VBComponents
(strModuleName).CodeModule.CountOfLines
.VBComponents
(strModuleName).CodeModule.AddFromFile strPath &
strFileName
Else
'The module does not exist -> import
.VBComponents.Import strPath &
strFileName
End If
strFileName = Dir
Loop While strFileName <> ""
End If
End With
Exit Sub
ErrUpgradeSourceCode:
MsgBox "An error occured while upgrading the source
code (" & Err.Description & ")." & vbCrLf & _
"The report will not be opened",
vbCritical, "MPR Version " & strVersion
ActiveDocument.Close SaveChanges:=False
End Sub
 
M

murdoch

Hi

I've had similar things happen after copying modules by
hand from one template to another. I usually make a
trivial change (overtype a letter or two) and then save
the template. Maybe you could do something similar in
code, perhaps adding a blank line? Sorry - I've not had
time to test this.
 
P

Peter Hewett

Hi Alex

Instead of:
DocOpening

Try:
Application.Run "DocOpening".

Application.Run can be *very* flakey, so use it as little as possible. Also be aware that
when using the extended syntax:
Application.Run "Project.Module.Procedure"

It becomes incredibly unreliable and try and avoid this variant if possible.

HTH + Cheers - Peter
 
A

Alex

Thanks for the idea... but the problem stays the same.

Alex
-----Original Message-----
Hi Alex

Instead of:
DocOpening

Try:
Application.Run "DocOpening".

Application.Run can be *very* flakey, so use it as little
as possible. Also be aware that
 
P

Peter Hewett

Hi Alex

Try saving the template before calling the macro.

HTH + Cheers - Peter
 
A

Alex

Peter,

Thanks for wasting your time on my case but I tried that
as well without success.

Cheers

Alex
 
P

Peter Hewett

Hi Alex

I'm not one to give up that easy! Have you tried:
Application.OnTime Now + TimeValue("00:00:01"), "DocOpening"

Also I'd break pint the code where you call (or should I say try to call) DocOpening. I'd
then use the IDE's Object Browser to checkup on the template project and see just what VBA
thinks is in the project. Also, the code that's being generated is valid? If you stop
the procedure immediately before you call DocOpening with and End statement and then
manually from the Immediate Window execute the DocOpening statement does that work. If it
does not - does it if you save the document and then try it again manually. If not does
it if you close/open the document and then try it again manually?

HTH + Cheers - Peter
 

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