karoti99
New Member
- Joined
- Feb 25, 2023
- Messages
- 8
- Office Version
- 365
- 2021
- 2019
- 2016
- Platform
- Windows
- 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.
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.