0

From a macro in Excel, I open a .docm Word doc then search for "<>" (the only text on its line) and replace it with a page break. After the page break is inserted, it leaves an empty line at the end of the previous page where "<>" was. So I'd like to use the SendKeys function to simulate pressing the up arrow key and then backspace to go up to this empty line and then delete it. SendKeys works in the active window, so I'm trying to switch the currently active window (Excel) to the Word doc. But everything I have tried hasn't worked. I can't figure out why VBA won't activate the Word Doc! Please help! Thanks in advance! :)

(Below I labeled what I tried that didn't work)

'EARLY BINDING
Dim WordApp As Word.Application
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True

Dim WordDoc As Word.Document
Set WordDoc = WordApp.Documents.Open(Filename:=word_template_path, ReadOnly:=False)

With WordDoc.Application.Selection
        .Find.Text = "<<new page>>"
        .Find.Execute
        .InsertBreak

'        AppActivate "Microsoft Word" - didn't work

'        Dim wn As Window
'        For Each wn In Application.Windows
'        Debug.Print wn.Caption
'        Next wn
'^ This loop doesn't even show the word file either! It only iterates once, showing the Excel file the macro is saved in!

'        WordDoc.Activate - didn't work
'        WordApp.Activate - didn't work
'        WordDoc.Windows(1).Activate - didn't work

        SendKeys "{UP}"
        .TypeBackspace
        .EndOf
End With
7
  • Not the answer you're looking for, but I'd look into replacing the line break at the same time as your new page marker. So replace <<new page>>[newline], that way you won't have to do SendKeys. Looks like Word is a bit peculiar on how to do that, this might help superuser.com/questions/124759/… Commented Feb 16, 2023 at 20:21
  • I tested code with Office 2021, WordApp.Activate does the job. The empty line is suppressed. Commented Feb 16, 2023 at 20:57
  • @jacouh What might be the issue then? Is it some kind of setting? Or the way the module is saved or whether the sub is public? Commented Feb 16, 2023 at 22:20
  • I tested the code in a Worksheet public sub. Commented Feb 16, 2023 at 22:33
  • @jacouh My Sub is also public. What else might cause the issue? Commented Feb 16, 2023 at 23:11

2 Answers 2

0

Use the Document.Activate method which activates the specified document so that it becomes the active document.

WordDoc.Activate 
Sign up to request clarification or add additional context in comments.

Comments

0

When the code is launched from the Excel document, your original code would effectively not activate Word.Application. But AppActivate WordApp.Caption worked from a button click on an Excel Worksheet body:

As WordApp.Caption = "Word" rather than "Microsoft Word" as you used in the OP.


Sub so5477097ActivateWord()
  'EARLY BINDING
  'Dim WordApp As word.Application
  Dim WordApp As Object
  Dim word_template_path As String

  Set WordApp = CreateObject("Word.Application")
  WordApp.Visible = True


  Dim WordDoc As Object

  word_template_path = "mypath\template.docx"

  Set WordDoc = WordApp.Documents.Open(fileName:=word_template_path, ReadOnly:=False)

        'WordDoc.Activate
        'WordApp.Activate

  With WordDoc.Application.Selection
        .Find.Text = "<<new page>>"
        .Find.Execute
        .InsertBreak
'        AppActivate "Microsoft Word" - didn't work

'        Dim wn As Window
'        For Each wn In Application.Windows
'        Debug.Print wn.Caption
'        Next wn
'^ This loop doesn't even show the word file either! It only iterates once, showing the Excel file the macro is saved in!

'        WordDoc.Activate - didn't work
'        WordApp.Activate - didn't work
'        WordDoc.Windows(1).Activate - didn't work

   AppActivate WordApp.Caption
   
        SendKeys "{UP}"
        .TypeBackspace
        .EndOf
  End With
End Sub

Some images:

enter image description here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.