1
votes

I'm trying to write a macro that adds subdocuments to the end of a Word document when the Word document is opened. The document in question already has some text in it, so before running the macro I'd like to move the cursor to the end of the document. I can achieve this with the code: Selection.EndKey Unit:=wdStory which works fine when I run the macro after opening the document, but if I run the macro as soon as the document is opened by using the Sub:

Private Sub Document_Open()
    Selection.EndKey Unit:=wdStory
    'Add subdocuments based on user input to a form
    '(See edit below)
End Sub

in the ThisDocument object, the subdocuments are added at the start of the document. This may be because the cursor has not yet appeared so Selection doesn't 'exist' yet.

How do I run my macro when the document opens, but add subdocuments to the end of the document?

I've tried writing a space out first to cause the cursor to spawn but no change...

Any suggestions for alternative methods also welcome.

Edit: This code in ThisDocument:

Private Sub Document_Open()
    CreateWorkbook.Show
End Sub

Calls the form CreateWorkbook, with a button click sub:

Private Sub GenerateButton_Click()
    Dim i As Integer
    Dim rng As Word.Range

    Set rng = ActiveDocument.Content
    rng.Collapse wdCollapseEnd

    'ModulesListBox is a user input box that is a list of paths to the subdocuments
    For i = 0 To ModulesListBox.ListCount - 1
        docpath = ModulesListBox.List(i)
        rng.Subdocuments.AddFromFile docpath
    Next i

End Sub
1
Try adding Selection.Wholestory before that line.dwirony
@dwirony No change unfortunately-thanks for the suggestion though.Siyh

1 Answers

1
votes

Since the Document_Open event is calling a UserForm first thing, Word does need a chance to access the document. The following worked in my test.

'ThisDocument code:
Option Explicit

Private Sub Document_Open()
    Dim f As UserForm1
    Set f = New UserForm1
    Set f.rng = ThisDocument.Content
    f.Show
End Sub

Note how the user form is declared as an object - a UserForm is actually a class (same as ThisDocument), but VBA lets you handle it without specifically coding as a class. Often, this works, but not always. So the object is declared and a new instance of the UserForm class assigned to it.

A Range is declared in the UserForm class as a class-level, public member. It's set to the body of the document in the Open event.

Then the user form is displayed, code below.

At this point, the Range object is accessible and actual work with it can take place. That said, it appears Subdocuments.AddFromFile relies on a Selection, just as it relies being in the Outline View. This is probably because the functionality dates from the old WordBasic days and was never changed to adhere to VBA principles.

'Code in the UserForm
Option Explicit

Public rng As Word.Range

Private Sub CommandButton1_Click()
  Me.rng.Collapse 0
  'rng.Text = "Test text"
  ThisDocument.ActiveWindow.View = wdOutlineView
  Me.rng.Select
  Selection.Range.Subdocuments.AddFromFile ("C:\Test\CCRanges.docx")
  Application.ScreenRefresh
End Sub