Be the first user to complete this post
|
Add to List |
VBA-Excel: Working with Bookmarks- Insert text before Bookmark
For inserting the text before the Bookmark in an existing Microsoft Word Document using Microsoft Excel, you need to follow the steps below:
Add a Bookmark “IMBOOKMARK” with name “bookmark_1”
- Create the object of Microsoft Word
- Create Doc object using MS word object, Open the existing word document by providing the complete 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
Create the object of Microsoft Word
Set objWord = CreateObject(“Word.Application”)
Create Doc object using MS word object, Open the existing word document by providing the complete path
Set objDoc = 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
objWord.Quit
Complete 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
Also Read:
- VBA-Excel: Modified Consolidator – Merge or Combine Multiple Excel Files Into One Where Columns Are Not In Order
- VBA-Excel: Add Table and fill data to the Word document
- VBA-Excel: Format already written text in a word document – Format All Content
- Excel-VBA : Send Unique Images Embedded to Mail Body, With Every Mail From MS Outlook using Excel.
- VBA-Excel: Create a new Word Document