Hi there,
Through a macro in excel, I'm trying to insert a code in a module in word, but when it comes to "Set VBProj = ActiveDocument.VBProject" I get an error "Run-time error '424': Object required" and most probably I'll get this error for the VBComponents and CodeModule as well.
Can you masters please help me??
Thank you very much!
Code:
Sub openwd()
Dim Wordapp As Object
Set Wordapp = CreateObject("Word.Application")
Wordapp.documents.Open "link for the document"
Wordapp.Visible = True
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim StartLine As Long
Dim NumLines As Long
Dim ProcName As String
Dim SL As Long
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim Found As String
Dim Found2 As String
Dim StrLineText As String
Dim StrLineText2 As String
Set VBProj = ActiveDocument.VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
FindWhat1 = "insert here:"
FindWhat2 = "insert here2:"
StrLineText = "'TESTING THIS THING"
StrLineText2 = "'TESTINGTHIS THING 123"
With CodeMod
SL = 1
EL = .CountOfLines
SC = 1
EC = 255
Found = .Find(Target:=FindWhat1, StartLine:=SL, StartColumn:=SC, _
EndLine:=EL, EndColumn:=EC, _
wholeword:=True, MatchCase:=False, patternsearch:=False)
If Found = True Then
SL = CStr(SL) + 1
.InsertLines CStr(SL), StrLineText
Else
.InsertLines CStr(SL) + 1, StrLineText
End If
Found2 = .Find(Target:=FindWhat2, StartLine:=SL, StartColumn:=SC, _
EndLine:=EL, EndColumn:=EC, _
wholeword:=True, MatchCase:=False, patternsearch:=False)
If Found2 = True Then
SL = CStr(SL) + 1
.InsertLines CStr(SL), StrLineText2
Else
.InsertLines CStr(SL) + 1, StrLineText2
End If
End With
End Sub