PDA

View Full Version : Add dynamic vba-code...



kenan
09-04-2014, 02:37 AM
Hi,
thanks for your answers. it helps really.

I have got new problem!
I wanna add dynamic vba code to "Thisdocuemnt"
if I try to add vba code with vbCrLf or Chr(13) & Chr(10) then crash office 2003.

Have anybody an idea, how can I solute this problem?

I use the followinfg code to add dynamicly ba code:
ThisDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString " Sub ff()" & Chr(13) & Chr(10) & " End Sub "

macropod
09-04-2014, 02:56 AM
Try:
"Sub ff()" & vbCr & "End Sub"

snb
09-06-2014, 01:33 PM
If the codemodule already contains a macro with the same name the macro will fail:
If the codename of the document isn't "thisDocument" the macro will fail:


Sub M_snb_000()
ThisDocument.VBProject.VBComponents(ActiveDocument.CodeName).CodeModule.Add FromString Replace("Sub M_snb()~~End Sub", "~", vbCrLf)
End Sub