#1
|
|||
|
|||
Inserting code line in Word Module
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 |
#2
|
||||
|
||||
Try:
Set VBProj = Wordapp.ActiveDocument.VBProject
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
it works!
Thank you very much! |
Tags |
vba excel, vba word |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Want to delete balnk line row from table with MS word using VBA code | jarunsona | Word VBA | 3 | 07-21-2016 04:54 AM |
Word inserting line breaks / wrapping text | Girloutside | Word | 1 | 08-10-2015 12:45 PM |
VBA Code in a UserForm module to delete a Command Button which opens the userform | Simoninparis | Word VBA | 2 | 09-21-2014 03:50 AM |
Inserting a Word Module into excel | SarahBear | Excel Programming | 12 | 06-11-2014 05:26 AM |
AZWizard Module - ?hidden module | pcaldwell | Word | 1 | 08-22-2012 01:19 PM |