Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-29-2023, 04:59 PM
skarden skarden is offline Word Range/Selection exist if document is closed? Windows 10 Word Range/Selection exist if document is closed? Office 2010 64bit
Novice
Word Range/Selection exist if document is closed?
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 26
skarden is on a distinguished road
Question Word Range/Selection exist if document is closed?

I was trying to put formatted text into a dictionary.

I got it to work using Selection.FormattedText or putting the selection into a Range.

I verified that by putting the selection/range into a new document using my dictionary and the key that went with it.

However, if I close the original document, while dictionary.count still shows the correct number of items, when I try to put it into a document it errors out.

I thought that once I put either Selection.Formatted text or a range into a dictionary it would exist in the dictionary even it the original document was closed. But that doesn't seem the case.

I endup writing the text to an RTF and then inserting that, but if there was any way I could store the formatted text, a/k/a "rich text" into a dictionary that would be much preferred.

I created a blank document and then typed some "rich text" (This is bold, this is underlined), etc. and selected it. Then I ran this sub:

Dim dicTest as Object
Dim rng1 as Range

Set dicTest = CreateObject("Scripting.Dictionary")

Set dicTest(1) = Selection.FormattedText 'Had to use "Set" go keep it as FormattedText

Set rng1 = Selection.FormattedText



Set dicTest(2) = rng1.FormattedText

Documents([Put in # or name of a blank document]).Activate

Selection.FormattedText = dicTest(1)

or
Selection.FormattedText = dicTest(2).

Either of those work, BUT if I close document 1 then trying to put either dicTest 1 or 2 into a document will fail.

Is there any way to store formatted text in a dictionary or another variable/object?
Reply With Quote
  #2  
Old 08-29-2023, 11:51 PM
Italophile Italophile is offline Word Range/Selection exist if document is closed? Windows 11 Word Range/Selection exist if document is closed? 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

You are not storing any text in the dictionary. Look at your code again. You will see that you are storing ranges in your dictionary, not formatted text. Not that it matters as it is not possible to store formatted text except by writing it to another document. Any other method will strip the formatting.
Reply With Quote
  #3  
Old 08-30-2023, 06:29 AM
skarden skarden is offline Word Range/Selection exist if document is closed? Windows 10 Word Range/Selection exist if document is closed? Office 2010 64bit
Novice
Word Range/Selection exist if document is closed?
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 26
skarden is on a distinguished road
Unhappy

Unfortunately, as I suspected, and you confirmed, a range is only a pointer, not what is in the range itself. I verified it with this macro:

Sub FormattedTextAsRangeTest()

'This proves that storing a range to a dictionary only stores a pointer to the range
'NOT what was in the range itself
'This proves it because we do not tell the dictionary to change what is stored in it (i.e. rng1)
'
'Be sure to start in a document with some formatted text
'and to set sBlankDocName to the name of an empty document

Dim dicTest As Object

Dim rng1 As Range

Dim sAdName As String
Dim sBlankDocName As String

sBlankDocName = "[Name of blank document here]"

sAdName = ActiveDocument.Name

Set dicTest = CreateObject("Scripting.Dictionary")

Selection.WholeStory

Set rng1 = Selection.FormattedText

Set dicTest(1) = rng1.FormattedText

Documents(sBlankDocName).Activate

Selection.FormattedText = dicTest(1)

Documents(sAdName).Activate

Selection.MoveLeft , 1

Selection.TypeText "Newly Added "

Documents(sBlankDocName).Activate

Selection.FormattedText = dicTest(1)

End Sub
Reply With Quote
Reply

Tags
dictionaries, dictionary, formatted text



Similar Threads
Thread Thread Starter Forum Replies Last Post
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Ddadoo57 Word VBA 18 02-13-2023 12:25 PM
Word Range/Selection exist if document is closed? Word crashes if the merged document file is closed first reidtaylor Mail Merge 7 05-07-2019 04:19 AM
Hyperlinks that Do Not Exist in the Original Word Document Appearing in the PDF Document diarrheaofthewprocessor Word 11 01-24-2017 01:52 PM
Word Range/Selection exist if document is closed? Name a Range in a Word Document and then copy that range to the end of the doc w button click DanNatCorning Word VBA 1 04-29-2016 10:47 PM
Selection (range) in Word or Excel table NobodysPerfect Word VBA 2 09-16-2014 12:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:41 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