Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-18-2023, 11:02 AM
RobiNew RobiNew is offline Microsoft Learn - Title: Range.InsertAfter method (Word)" Windows 10 Microsoft Learn - Title: Range.InsertAfter method (Word)" Office 2016
Competent Performer
Microsoft Learn - Title: Range.InsertAfter method (Word)"
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question Microsoft Learn - Title: Range.InsertAfter method (Word)"

I found the code here below in "Microsoft Learn - Title: Range.InsertAfter method (Word)"


But it seems to fail. Can someone explain? Thanks!

Code:
Set doc = ActiveDocument 
Set rngRange = _ 
 doc.Range(doc.Paragraphs(1).Start, _ 
 doc.Paragraphs(1).End - 1) 
rngRange.InsertAfter _ 
 " This is now the last sentence in paragraph one."
Reply With Quote
  #2  
Old 11-18-2023, 01:25 PM
Italophile Italophile is offline Microsoft Learn - Title: Range.InsertAfter method (Word)" Windows 11 Microsoft Learn - Title: Range.InsertAfter method (Word)" Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

The code examples in the documentation aren't always accurate.

In this one the Range object has been omitted. It should be:

Code:
Dim doc As Document, rngRange As Range
Set doc = ActiveDocument
Set rngRange = _
    doc.Range(doc.Paragraphs(1).Range.Start, _
    doc.Paragraphs(1).Range.End - 1)
rngRange.InsertAfter _
    " This is now the last sentence in paragraph one."
Reply With Quote
  #3  
Old 11-18-2023, 03:21 PM
RobiNew RobiNew is offline Microsoft Learn - Title: Range.InsertAfter method (Word)" Windows 10 Microsoft Learn - Title: Range.InsertAfter method (Word)" Office 2016
Competent Performer
Microsoft Learn - Title: Range.InsertAfter method (Word)"
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Well, never mind! Many thanks, Italophile!
Reply With Quote
  #4  
Old 11-18-2023, 11:00 PM
gmaxey gmaxey is offline Microsoft Learn - Title: Range.InsertAfter method (Word)" Windows 10 Microsoft Learn - Title: Range.InsertAfter method (Word)" Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You are right. The documentation is often wrong. The documentation on CustomXMLParts is deplorable. The documentation is not only often wrong. It is often more complex than in needs to be. In this case, I would have simply used:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  oRng.End = ActiveDocument.Range.Paragraphs(1).Range.End - 1
  oRng.InsertAfter " This is now the last sentence in paragraph 1"
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-19-2023, 12:44 AM
RobiNew RobiNew is offline Microsoft Learn - Title: Range.InsertAfter method (Word)" Windows 10 Microsoft Learn - Title: Range.InsertAfter method (Word)" Office 2016
Competent Performer
Microsoft Learn - Title: Range.InsertAfter method (Word)"
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks, Gmaxey, for the simplification and comments!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Learn - Title: Range.InsertAfter method (Word)" Changing Recorded Macro Selection/Clipboard Method to Range/FormattedText Method zanodor Word VBA 5 07-15-2022 01:25 AM
Microsoft Learn - Title: Range.InsertAfter method (Word)" Intersect(Target, Sh.Range("AN4:AN5000")) can i reference this range another way? trevorc Excel Programming 2 04-30-2021 05:05 AM
Change the Column reference 'S' to a range ??? Sheets("Customer").Range("S" & ActiveCell.Row) = Date trevorc Excel Programming 1 03-04-2021 02:39 PM
Microsoft Learn - Title: Range.InsertAfter method (Word)" Title case macro gets a "type mismatch" error at the Instr method marceepoo Word VBA 1 03-05-2020 09:41 PM
Microsoft Learn - Title: Range.InsertAfter method (Word)" Is there a Microsoft Word Form Method That Restricts Placement, Length, and Spell Che petec Word 10 09-25-2011 08:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft