Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-20-2012, 11:48 AM
marceepoo marceepoo is offline Macro operates on wrong document window Windows 7 64bit Macro operates on wrong document window Office 2010 64bit
Novice
Macro operates on wrong document window
 
Join Date: Sep 2012
Posts: 22
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.

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()
Reply With Quote
  #2  
Old 12-20-2012, 03:12 PM
Astacus Astacus is offline Macro operates on wrong document window Windows XP Macro operates on wrong document window Office XP
Novice
 
Join Date: Dec 2012
Posts: 9
Astacus is on a distinguished road
Default

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
Reply

Tags
documents, vba in microsoft word, window



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro operates on wrong document window 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
Macro operates on wrong document window 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft