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

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
以下是使用VBA代码将Excel中的数据填入Word模板中的基本步骤: 1. 打开Word模板文件并创建Word文档对象。 ```VBA Dim wordApp As Word.Application Dim wordDoc As Word.Document Set wordApp = New Word.Application Set wordDoc = wordApp.Documents.Open("C:\Template.docx") ``` 2. 在Word模板中插入书签并保存。 在Word模板中插入书签,书签将用于标识我们要填充数据的位置。例如,我们可以在Word模板中插入名为"Name"和"Age"的两个书签。 ```VBA wordDoc.Bookmarks.Add "Name", wordDoc.Range(0, 0) wordDoc.Bookmarks.Add "Age", wordDoc.Range(0, 0) wordDoc.Save ``` 3. 打开Excel文件并读取数据。 ```VBA Dim excelApp As Excel.Application Dim excelWorkbook As Excel.Workbook Dim excelWorksheet As Excel.Worksheet Set excelApp = New Excel.Application Set excelWorkbook = excelApp.Workbooks.Open("C:\Data.xlsx") Set excelWorksheet = excelWorkbook.Worksheets("Sheet1") Dim name As String Dim age As Integer name = excelWorksheet.Range("A2").Value age = excelWorksheet.Range("B2").Value excelWorkbook.Close ``` 4. 在Word文档中填充数据。 ```VBA wordDoc.Bookmarks("Name").Range.Text = name wordDoc.Bookmarks("Age").Range.Text = age ``` 5. 关闭Word文档和Excel应用程序并保存Word文档。 ```VBA wordDoc.Close SaveChanges:=True wordApp.Quit excelApp.Quit ``` 完整的VBA代码如下: ```VBA Sub FillWordTemplate() Dim wordApp As Word.Application Dim wordDoc As Word.Document Dim excelApp As Excel.Application Dim excelWorkbook As Excel.Workbook Dim excelWorksheet As Excel.Worksheet '打开Word模板文件 Set wordApp = New Word.Application Set wordDoc = wordApp.Documents.Open("C:\Template.docx") '在Word模板中插入书签并保存 wordDoc.Bookmarks.Add "Name", wordDoc.Range(0, 0) wordDoc.Bookmarks.Add "Age", wordDoc.Range(0, 0) wordDoc.Save '打开Excel文件并读取数据 Set excelApp = New Excel.Application Set excelWorkbook = excelApp.Workbooks.Open("C:\Data.xlsx") Set excelWorksheet = excelWorkbook.Worksheets("Sheet1") Dim name As String Dim age As Integer name = excelWorksheet.Range("A2").Value age = excelWorksheet.Range("B2").Value excelWorkbook.Close '在Word文档中填充数据 wordDoc.Bookmarks("Name").Range.Text = name wordDoc.Bookmarks("Age").Range.Text = age '关闭Word文档和Excel应用程序并保存Word文档 wordDoc.Close SaveChanges:=True wordApp.Quit excelApp.Quit End Sub ``` 以上代码仅供参考,具体实现可能需要根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值