Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-05-2021, 09:15 AM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default Extracting text from Start and End position

I know how to extract text in MS Word Content between 2 words using `.Find`

I am trying to extract text (with all formatting) between 2 positions. For example 10 and 200.



If I use MID then I lose formatting. One thought process is

1. Delete text from position 1 till 2
2. Delete text from position 201 till end of document

This is where I am stuck. If the logic is correct then how do I delete the relevant text?
Attached Images
File Type: png Sample.png (94.2 KB, 11 views)
Attached Files
File Type: docx Sample.docx (15.5 KB, 0 views)
Reply With Quote
  #2  
Old 05-05-2021, 02:10 PM
macropod's Avatar
macropod macropod is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,200
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

Simple - have your macro:
1. Mark all content as hidden.
2. Find the text you want to preserve and unhide it
3. Find and delete all hidden text.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-05-2021, 03:41 PM
Guessed's Avatar
Guessed Guessed is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,324
Guessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to behold
Default

The formatting of the last paragraph in the range requires the paragraph mark to be included since that is where the paragraph formatting is held. But you could also just apply the last paragraph formatting to the pasted area after the copy has happened.

The property you should be using is .FormattedText
Do a search for this term on this forum to see examples which duplicate ranges with formatting.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 05-05-2021, 09:32 PM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Simple - have your macro:
1. Mark all content as hidden.
2. Find the text you want to preserve and unhide it
3. Find and delete all hidden text.
Thanks Paul for your reply. I am not sure I understand. What I want is not to hide but discard the text completely. Finally I would be saving the document.

Quote:
Find the text you want to preserve
There is no fixed text as I mentioned in the question. It is more about start position or end position. I have these as numbers and not text. The text/character at the start position can be a "Space" or a "Full Stop".

For example, let's say I have this simple text

Quote:
The quick brown fox jumps over the lazy dog
And I want to keep only the text from 11th position till 31st position with the formatting and delete everything. If it was an unformatted string, I could have used

Code:
MyString = "The quick brown fox jumps over the lazy dog"
Debug.Print Mid(MyString, 11, 31 - 11)
This would give me the desired text "brown fox jumps over" but the formatting is lost.
Reply With Quote
  #5  
Old 05-05-2021, 09:36 PM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
The formatting of the last paragraph in the range requires the paragraph mark to be included since that is where the paragraph formatting is held. But you could also just apply the last paragraph formatting to the pasted area after the copy has happened.

The property you should be using is .FormattedText
Do a search for this term on this forum to see examples which duplicate ranges with formatting.
Thanks Andrew for your reply . I am not sure I understand. This is not about paragraph per se or have I misunderstood something?. I have explained it a bit more in the above post. Do let me know if you need any other information?
Reply With Quote
  #6  
Old 05-05-2021, 09:36 PM
macropod's Avatar
macropod macropod is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,200
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

Quote:
Originally Posted by NameIsJustAnIllusion View Post
What I want is not to hide but discard the text completely.
Which is exactly what the process I described will achieve. Read point 3.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-05-2021, 09:44 PM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default

My Apologies. I was editing my reply. As I mentioned in my previous post

Quote:
Find the text you want to preserve
There is no fixed text as I mentioned in the question. It is more about start position or end position. I have these start position or end position as numbers and not text. The text/character at the start position can be a "Space" or a "Full Stop". So I can't find it or am I missing something here?

Thanks again for patiently helping me out Paul.
Reply With Quote
  #8  
Old 05-05-2021, 09:51 PM
macropod's Avatar
macropod macropod is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,200
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

For example:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
  With .Range
    .Font.Hidden = True
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Format = False
      .Forward = True
      .Wrap = wdFindContinue
      .MatchWildcards = True
      .Text = "Section 1.10.32*voluptatem.^13"
      .Replacement.Text = "^&"
      .Replacement.Font.Hidden = False
      .Execute Replace:=wdReplaceAll
      .Replacement.ClearFormatting
      .Font.Hidden = True
      .Format = True
      .Text = "*"
      .Replacement.Text = ""
      .Execute Replace:=wdReplaceAll
    End With
  End With
End With
Application.ScreenUpdating = True
End Sub
Quote:
Originally Posted by NameIsJustAnIllusion View Post
There is no fixed text as I mentioned in the question. It is more about start position or end position. I have these start position or end position as numbers and not text. The text/character at the start position can be a "Space" or a "Full Stop". So I can't find it or am I missing something here?
So how do you know what the starting position & end position are if you don't know what the content is?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 05-05-2021, 10:00 PM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default

The starting position & the end position (which is a number) is fed by another macro.
Reply With Quote
  #10  
Old 05-05-2021, 10:03 PM
macropod's Avatar
macropod macropod is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,200
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

In which case:
Code:
Sub Demo(StartPos As Long, EndPos As Long)
With ActiveDocument
  .Range(StartPos, EndPos).Cut
  .Range.Paste
End With
End Sub
You 'other macro' still has to know something about the content before it can pass anything meaningful about the starting & end positions...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 05-05-2021, 10:10 PM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
In which case:
Code:
Sub Demo(StartPos As Long, EndPos As Long)
With ActiveDocument
  .Range(StartPos, EndPos).Cut
  .Range.Paste
End With
End Sub
You 'other macro' still has to know something about the content before it can pass anything meaningful about the starting & end positions...
Ah! I think this might do it. Let me try this.
Reply With Quote
  #12  
Old 05-05-2021, 10:15 PM
NameIsJustAnIllusion NameIsJustAnIllusion is offline Extracting text from Start and End position Windows 10 Extracting text from Start and End position Office 2019
Novice
Extracting text from Start and End position
 
Join Date: May 2021
Posts: 12
NameIsJustAnIllusion is on a distinguished road
Default

PURRRRFECT!

Thanks a ton Paul.

Code:
.Range(StartPos, EndPos).Cut
This is exactly what I was looking for
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Choose multiple start and end position of window in a row... Ganesh Kumar Excel 9 04-01-2019 08:26 AM
Help with Formula to extracting text from text string Haha88 Excel 7 01-13-2019 01:33 AM
Extracting text from Start and End position Help with Formula to extracting text from text string Haha88 Excel 9 02-05-2018 01:04 AM
Extracting text from Start and End position Start from the previous position Dimsok Word VBA 7 09-14-2014 06:07 AM
Justify Paragraph Start position with VBA jeff_kaufman Word VBA 2 11-09-2013 12:15 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 02:12 AM.


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