Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Working with Bookmarks– Insert text before Bookmark

For insert­ing the text before the Book­mark in an exist­ing Microsoft Word Doc­u­ment using Microsoft Excel, you need to fol­low the steps below:

Add a Book­mark “IMBOOKMARK” with name “bookmark_1”

  • Cre­ate the object of Microsoft Word
  • Cre­ate Doc object using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path
  • Make the MS Word visible
  • Get the range object for the bookmark
  • Insert the text before the bookmark
  • Save the word document
  • Close the word document


 

Cre­ate the object of Microsoft Word

Set obj­Word = CreateObject(“Word.Application”)

Cre­ate Doc object using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path

Set obj­Doc = objWord.Documents.Open(“D:\OpenMe.docx”)

Make the MS Word Visible

objWord.Visible = True

Get the range object for the bookmark

Set objRange = objDoc.Bookmarks(“bookmark_1”).Range

Insert the text before the bookmark

objRange.InsertBefore (“I will be added before bookmark        ”)

Save the Word Document

objDoc.Save

Close the word document

obj­Word.Quit

 

Com­plete Code:

Function FnBookMarkInsertBefore()

   Dim objWord

   Dim objDoc

   Dim objRange

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Open("D:\OpenMe.docx")

   objWord.Visible = True

 Set objRange = objDoc.Bookmarks("bookmark_1").Range

    objRange.InsertBefore ("I will be added before bookmark        ")

End Function
Insert Text Before Bookmark

Insert Text Before Bookmark

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: