Deleting content between two bookmarks in Word via Excel VBA

karoti99

New Member
Joined
Feb 25, 2023
Messages
8
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
  2. MacOS
Hello! First post, after years of visiting this site and finding a lot of solutions to my VBA challenges.

I have a workbook that generates a word document.

Part of the code populates content controls with data successfully

'populate content controls
With wordApp
.ActiveDocument.SelectContentControlsByTitle("Client").Item(1).Range.Text = Sheets("Data").Range("C1").Value
.ActiveDocument.SelectContentControlsByTitle("Total_Hours").Item(1).Range.Text = Sheets("Data").Range("B1").Value
.ActiveDocument.SelectContentControlsByTitle("Rate1").Item(1).Range.Text = "$" & Sheets("Data").Range("B2").Value
.ActiveDocument.SelectContentControlsByTitle("Rate2").Item(1).Range.Text = "$" & Sheets("Data").Range("B3").Value
End With

However the code immediately after fails. When I try to delete data between bookmark ranges, I get the dreaded 424 object required error

'delete unnecessary content within bookmark range
With wordApp
.ActiveDocument.Range(ActiveDocument.Bookmarks("OverviewBegin").Range.Start, ActiveDocument.Bookmarks("OverviewEnd").Range.End).Delete
.ActiveDocument.Range(ActiveDocument.Bookmarks("InvestmentBegin").Range.Start, ActiveDocument.Bookmarks("InvestmentEnd").Range.End).Delete
.ActiveDocument.Range(ActiveDocument.Bookmarks("OrderSummaryBegin").Range.Start, ActiveDocument.Bookmarks("OrderSummaryEnd").Range.End).Delete
.ActiveDocument.Range(ActiveDocument.Bookmarks("TermsBegin").Range.Start, ActiveDocument.Bookmarks("TermsEnd").Range.End).Delete
End With
End If

But, if I copy and paste that code into a macro-enabled word document (sans the with wordApp parameters) and run it, it works perfectly and deletes the data within those bookmark ranges successfully.

Any ideas? I need to get Excel VBA to initiate the deletion of the data within the listed bookmark ranges.
 
Would be great if I could avoid loading the object library though
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I was able to dig into this further and modified my original code by putting 'wordapp' in front of ActiveDocument and it worked. That alleviated the need to load the reference library.

Seems like double qualified since I already had 'with wordApp' and the previous four lines didn't need it, but it works so....

Final code

With wordApp
.ActiveDocument.SelectContentControlsByTitle("Client").Item(1).Range.Text = Sheets("Data").Range("C1").Value
.ActiveDocument.SelectContentControlsByTitle("Total_Hours").Item(1).Range.Text = Sheets("Data").Range("B1").Value
.ActiveDocument.SelectContentControlsByTitle("Rate1").Item(1).Range.Text = "$" & Sheets("Data").Range("B2").Value
.ActiveDocument.SelectContentControlsByTitle("Rate2").Item(1).Range.Text = "$" & Sheets("Data").Range("B3").Value
.ActiveDocument.Range(wordApp.ActiveDocument.Bookmarks("OverviewBegin").Range.Start, wordApp.ActiveDocument.Bookmarks("OverviewEnd").Range.End).Delete
.ActiveDocument.Range(wordApp.ActiveDocument.Bookmarks("InvestmentBegin").Range.Start, wordApp.ActiveDocument.Bookmarks("InvestmentEnd").Range.End).Delete
.ActiveDocument.Range(wordApp.ActiveDocument.Bookmarks("OrderSummaryBegin").Range.Start, wordApp.ActiveDocument.Bookmarks("OrderSummaryEnd").Range.End).Delete
.ActiveDocument.Range(wordApp.ActiveDocument.Bookmarks("TermsBegin").Range.Start, wordApp.ActiveDocument.Bookmarks("TermsEnd").Range.End).Delete
End With
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top