#1
|
|||
|
|||
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. Marc Code:
Sub subRarBakupDoc() 'Source = NMa ' ' Dim dtmStart As Date Dim dtmEnd As Date Dim intTotalTime As Integer dtmStart = Now() With ActiveDocument End With With ActiveDocument ActiveDocument.ActiveWindow.SetFocus 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\RarCpyBakupSelected_file_nu_.py" & 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 'http://ss64.com/vb/run.html 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() |
#2
|
|||
|
|||
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.
|
Tags |
documents, vba in microsoft word, window |
|
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 |