View Single Post
 
Old 04-15-2023, 09:20 AM
ctviggen ctviggen is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

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.
Reply With Quote