Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

LinkBack Thread Tools Display Modes
Old 12-20-2012, 11:48 AM
marceepoo marceepoo is offline Windows 7 64bit Office 2010 64bit
Join Date: Sep 2012
Posts: 7
marceepoo is on a distinguished road
Default Macro operates on wrong document window

If I have two or more word files open when I run this macro, sometimes the macro will operate on a file other than the one that was topmost on my screen when I hit the button that triggered the macro.

Does anyone know how I can tell vba to run the macro on the document window that was topmost on my screen when I hit the button that triggered the macro?

Thanks for any tips.


Sub subRarBakupDoc() 'Source = NMa
    Dim dtmStart As Date
    Dim dtmEnd As Date
    Dim intTotalTime As Integer
    dtmStart = Now()
    With ActiveDocument
    End With
    With ActiveDocument
        Dim strActiveDocFullName As String
        Dim strActiveDocDir As String
        Dim strVbScriptFullName As String
        Dim strPythonScriptFullName As String
        Dim strCmdLin01 As String
        Dim strRetVal As String
        Dim objWshShell As Object
        Dim strMsgBoxMsg As String
        strActiveDocFullName = Chr(34) & ActiveDocument.FullName & Chr(34)
        strActiveDocDir = Chr(34) & ActiveDocument.Path & Chr(34)
        strPythonScriptFullName = Chr(34) & "C:\Apps\UtilitiesByMarc\" & Chr(34)
        strCmdLin01 = Mac02.fnPythonFullName & " " & strPythonScriptFullName & " " & strActiveDocFullName
        'strCmdLin01 = "C:\Apps\ UtilitiesByMarc\MessageBoxe_SAVE_.vbs" & " " & strActiveDocFullName
        'MsgBox strCmdLin01
        Set objWshShell = CreateObject("WScript.Shell")
        objWshShell.Run strCmdLin01, 1, True '  objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn])
                                            'intWindowStyle = 1 = Activate and display the window.   bWaitOnReturn = True / False
        'strRetVal = Shell(strCmdLin01, vbNormalFocus)
    End With
    dtmEnd = Now()
    intTotalTime = CInt(dtmEnd - dtmStart)
    'MsgBox "intTotalTime = " & intTotalTime
End Sub 'subRarBakupDoc()
Reply With Quote
Old 12-20-2012, 03:12 PM
Astacus Astacus is offline Windows XP Office XP
Join Date: Dec 2012
Posts: 9
Astacus is on a distinguished road

It will always run on your active document. That isn't the same as the one that is topmost on your screen, because the topmost document doesn't necessarily have to be the one that's activated. The easiest solution is to click somewhere in your topmost window to activate it.
Reply With Quote

documents, vba in microsoft word, window

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ensure macro operates in the right doc window marceepoo Word 1 10-31-2012 08:59 PM
need macro for quiz (?) repeat of wrong answers with shuffle braveshark PowerPoint 0 10-02-2012 06:10 AM
trying to open a word 2010 document and opens the file conversion window starfish Word 2 05-24-2012 12:06 AM
IF statement just generates text "MERGEFIELD" in document - what am I doing wrong? Eri Mail Merge 1 03-03-2012 03:03 PM
Document window resizing misbehavior Clueless in Seattle Word 4 01-10-2012 07:29 PM

All times are GMT -7. The time now is 10:09 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc. is not affiliated with Microsoft