|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Selecting Text within a document with known start/stop (+searching and tools)
We have documents that have a section with a known start, which starts on its own page. This section ends with a "Section Break (Next Page)".
Looks like this: [new page] TITLE THAT IS KNOWN [multiple paragraphs of text] [Section Break (Next Page)] I'd like to select everything between TITLE THAT IS KNOWN and [Section Break (Next Page)]. Is this possible? And, if you had this problem, how would you go about searching for a solution and what tools do you use? I get pages like this: Working with the Selection Object | Microsoft Learn Selecting Text in a Document | Microsoft Learn Which are effectively useless. And, I don't get links into this forum. Does the forum search here work better than searching via a search engine? Are their additional tools/locations that might have examples/answers? Thank you. |
#2
|
|||
|
|||
If your cursor is at the start of the known title, you can run this macro:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oRng As Range Set oRng = Selection.Range oRng.EndOf Unit:=wdSection, Extend:=wdExtend oRng.Select lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Sorry for my delay in responding. I got sidetracked by life events, including vacation.
I also tried ChatGPT on a version of this, to get some input from it. I tried a more complex version of what I asked here, and the code it created did not totally work. It was nice to see another way of approaching things, though. I'm still working through it. And I'll try your code, too, either on my lunch or over the weekend. If I get something that works, I'll report back. Thank you again. |
#4
|
|||
|
|||
I just can't figure this out. I have a document that looks like this:
Part 1... ends with Section Break (Next page) Part 2... ends with Section Break (Next page) Part 3... document ends I want to copy all three parts to different documents. Here is test code to copy only the first Part: Code:
Sub SplitDocumentTest() Dim specStart As Range, specEnd As Range Dim specDoc As Document ' Set the section ranges based on the page breaks Set specStart = ActiveDocument.Range ' Something like this might work to go to a section break, not sure we need the 1.... Set specEnd = specStart.GoTo(wdGoToSection, wdGoToFirst) ' specStart.EndOf Unit:=wdSectionBreakNextPage, Extend:=wdExtend ' specStart.EndOf Unit:=wdSection, Extend:=wdExtend ' Set specEnd = specStart.GoTo(wdGoToSection, wdGoToNext, 1) ' Set specEnd = specStart.GoTo(wdGoToPage, wdGoToAbsolute, 2) ' specStart.Select ' Create new document for this section and copy the content Set specDoc = Documents.Add() specStart.Copy specDoc.Content.Paste specDoc.SaveAs2 FileName:="Specification", FileFormat:=wdFormatXMLDocument End Sub As you can see, I've tried a ton of stuff, including Gmaxy's recommendation. The entire document, including all three parts, gets copied to the "Specification" document, instead of only Part 1. What am I doing wrong? |
#5
|
|||
|
|||
Actually you weren't doing anything right.
Code:
Sub SplitDocumentTest() Dim oRng As Range Dim oDoc As Document Set oRng = ActiveDocument.Range With oRng .Collapse wdCollapseStart .EndOf Unit:=wdSection, Extend:=wdExtend .MoveEnd wdCharacter, -1 Set oDoc = Documents.Add() .Copy oDoc.Content.Paste oDoc.SaveAs2 FileName:="Specification_1", FileFormat:=wdFormatXMLDocument Set oDoc = Nothing .Collapse wdCollapseEnd .MoveStart wdCharacter, 1 .EndOf Unit:=wdSection, Extend:=wdExtend .MoveEnd wdCharacter, -1 Set oDoc = Documents.Add() .Copy oDoc.Content.Paste oDoc.SaveAs2 FileName:="Specification_2", FileFormat:=wdFormatXMLDocument Set oDoc = Nothing .Collapse wdCollapseEnd .MoveStart wdCharacter, 1 .EndOf Unit:=wdSection, Extend:=wdExtend .MoveEnd wdCharacter, -1 Set oDoc = Documents.Add() .Copy oDoc.Content.Paste oDoc.SaveAs2 FileName:="Specification_3", FileFormat:=wdFormatXMLDocument Set oDoc = Nothing End With lbl_Exit: Exit Sub End Sub |
#6
|
|||
|
|||
Thanks for letting me know I wasn't doing anything right.
Anyway, thank you very much. Your code does work...if I step through it. If I don't step through it, I get an error on the first "oDoc.Content.Paste". The error is a "Run-time error 4605: This command is not available." I see some possible fixes on the Internet for this (see below after the code), but I haven't found one that works. As you can see in the code below, I've tried selecting "something" in newly added document, thinking that perhaps that document was not in focus. I've tried adding a delay (of about 1 second), thinking it was a timing issue. Neither of these worked. But if you put a break on the first "oDoc.Content.Paste" and step through from there, it works perfectly well. Any ideas on what the issue and fix are? Also, why the "lbl_Exit:" at the end? Here's my commented code, for anyone like me who is new to this: Code:
Sub SplitDocumentTest2() ' This macro takes a document formed as follows and splits into three documents, one per Part ' Part 1...ends at Section Break (Next Page) ' Part 2...ends at Section Break (Next Page) ' Part 3...to end of document ' NOTE: you should have a paragraph mark before the Section Break (Next Page), ' otherwise the final line will have an error in formatting ' oRng is used to manipulate the current part of the document Dim oRng As Range ' oDoc is used for the three new documents Dim oDoc As Document Set oRng = ActiveDocument.Range With oRng ' ' Copy Part 1 to first document ' ' Collapse using wdCollapseStart collapses a range or selection to the starting position .Collapse wdCollapseStart ' Go to the next section .EndOf Unit:=wdSection, Extend:=wdExtend ' Move backwards one character .MoveEnd wdCharacter, -1 ' Add a new cocument Set oDoc = Documents.Add() ' Copy the current Range .Copy ' oDoc.Range(0, 0).Select ' Wait (1) ' Paste the new range oDoc.Content.Paste ' Save the document oDoc.SaveAs2 FileName:="Specification_1", FileFormat:=wdFormatXMLDocument ' Reset the document oDoc Set oDoc = Nothing ' ' Copy Part 2 to second document ' ' Move the range to the end .Collapse wdCollapseEnd ' Add one character to the range .MoveStart wdCharacter, 1 ' Go to the next section .EndOf Unit:=wdSection, Extend:=wdExtend ' Move backwards one character .MoveEnd wdCharacter, -1 Set oDoc = Documents.Add() .Copy oDoc.Content.Paste oDoc.SaveAs2 FileName:="Specification_2", FileFormat:=wdFormatXMLDocument Set oDoc = Nothing ' ' Copy Part 3 to third document ' .Collapse wdCollapseEnd .MoveStart wdCharacter, 1 .EndOf Unit:=wdSection, Extend:=wdExtend .MoveEnd wdCharacter, -1 Set oDoc = Documents.Add() .Copy oDoc.Content.Paste oDoc.SaveAs2 FileName:="Specification_3", FileFormat:=wdFormatXMLDocument Set oDoc = Nothing End With lbl_Exit: Exit Sub End Sub One discussion of this error, which describes this as an active window error: vba - Runtime Error 4605. This method or property is not available because a document window is not active - Stack Overflow This person suggests using a second macro that just performs the paste operation, but no code: Redirecting This says "The 4605 error occurs because of a timing problem and can be overcome by using an Or Error - DoEvents -Resume loop", but no code: Redirecting Another web page, which I can't find now, suggested a timer, which I implemented to delay everything by a second, but this did not work. |
#7
|
|||
|
|||
I fixed this error (for at least one running of the macro anyway) by implementing the timer macro discussed here:
https://learn.microsoft.com/en-us/an...table-run-time I add a one second delay and that seems to work. I assume the previous macro I used did not work because I did something wrong. And this seems to be a common error when pasting, especially from the clipboard, which is what I do a lot. I created another macro for a completely different purpose where I had to paste the same text multiple times. Ran great when I stepped through the code. Would get tripped up on one of the four pastes when run normally. Added the delay from the above link (thanks to whoever wrote that!) and it works so far. |
#8
|
|||
|
|||
Not tested, but you might try:
Code:
On Error Resume Next Do Err.Clear oDoc.Content.Paste Loop While Err.Number <> 0 On Error GoTo 0 |
#9
|
|||
|
|||
Thank you! I had seen something similar, but I lost it in the mass of windows I opened then closed. And if you don't remember the search terms you used, you get different results.
I'll implement when I can and report back, as the 1 second delay looks a little weird when it's running -- something gets entered; delay; more stuff happens; delay... (It works though, and saves me so much time.) |
Tags |
resources, select text |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Searching in main body document and text boxes at the same time | laith93 | Word VBA | 0 | 10-31-2021 10:38 AM |
Searching for e-mails in a Shared Mailbox prior to start date. | m5fgn | Outlook | 4 | 08-08-2019 03:43 AM |
Change Drawing Tools to Text Box Tools to insert address in labels | Alecf | Drawing and Graphics | 5 | 09-01-2017 05:27 AM |
Start and stop an executeble from vba | remmyMartin | Outlook | 0 | 01-12-2012 08:09 AM |
Remove text in start up document | Sheila | Word | 1 | 09-30-2010 03:33 PM |