Microsoft Office Forums Inserting code line in Word Module

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-10-2017, 06:27 AM
mihnea96 mihnea96 is offline Inserting code line in Word Module Windows 7 32bit Inserting code line in Word Module Office 2016
Novice
Inserting code line in Word Module
 
Join Date: Apr 2017
Posts: 26
mihnea96 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 08-14-2017, 06:05 PM
macropod's Avatar
macropod macropod is offline Inserting code line in Word Module Windows 7 64bit Inserting code line in Word Module Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,644
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try:
Set VBProj = Wordapp.ActiveDocument.VBProject
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 08-15-2017, 04:57 AM
mihnea96 mihnea96 is offline Inserting code line in Word Module Windows 7 32bit Inserting code line in Word Module Office 2016
Novice
Inserting code line in Word Module
 
Join Date: Apr 2017
Posts: 26
mihnea96 is on a distinguished road
Default

it works!
Thank you very much!
Reply With Quote
Reply

Tags
vba excel, vba word

Thread Tools
Display Modes


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
Inserting code line in Word Module 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


All times are GMT -7. The time now is 05:13 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft