Search code examples
batch-filejscriptsendkeys

Get "Last Modified by" of Word 2010 file by opening and using sendkeys from batch script


I'm trying to get the person who last modified a Word 2010 document and I thought I'd be really smart by just opening the file and using sendkeys. I've never used sendkeys, so it's probably some really simple fix with my syntax.

I tried using this help topic to fix it, but am having no luck: Press Keyboard keys using a batch file

Here are my problems:

  1. It only works if Word is already open, and then it changes the open window to the file I want. Can we have it so that I don't have to open word first?

  2. It can only type text in word. Can't seem to send the special keys like alt. I was trying "{%}", but it gives me a java runtime error. So, I guess there's something wrong with the Jscript at the bottom?

  3. If I try to use WScript.Sleep 5000 I get a runtime error too.

  4. If I try specifying what window to make active, WshShell.AppActivate, I get a runtime error.

I'm also new to Jscript. I'd be grateful for any help. Thanks, CJ.

My code:

@if (@CodeSection == @Batch) @then

@echo off

rem use the sendkeys 
set SendKeys=CScript //nologo //E:JScript "%~F0"

cd C:\users\cbaker\desktop
start /w test.doc

%SendKeys% "{%}"
%SendKeys% "f"
%SendKeys% "i"
%SendKeys% "{tab 14}"
%Sendkeys% "^c"

rem Open notepad
cd C:\windows
start /w notpad.exe

%SendKeys% "^(V)"

goto :EOF

@end

// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Solution

  • You could have a look at using VBScript, example here.

    Basically, you would save a script like the one below in a file called "GetInfo.vbs" and then run

    CSCRIPT /NOLOGO GetInfo.vbs
    

    at a Command prompt.

    Or you can double-click it in Windows (File) Explorer.

    Note that you will have to change the name in the second line to match your document. You will also maybe need to look at how to pass arguments on the command-line so you can start it with the name of a document that you want to check.

    Set objPropertyReader = CreateObject("DSOleFile.PropertyReader") 
    Set objDocument = objPropertyReader.GetDocumentProperties _ 
        ("C:\Scripts\Test.doc") 
    
    Wscript.Echo "Application name: " & objDocument.AppName 
    Wscript.Echo "Author: " & objDocument.Author 
    Wscript.Echo "Comments: " & objDocument.Comments 
    Wscript.Echo "Company: " & objDocument.Company 
    Wscript.Echo "Date created: " & objDocument.DateCreated 
    Wscript.Echo "Date last saved: " & objDocument.DateLastSaved 
    Wscript.Echo "Last edited by: " & objDocument.LastEditedBy 
    

    Belowis another method... if you look through the code, you will see the Author is extracted. You may have to play around looking for other fields but a little Googling should solve it.

    On Error Resume Next
    
    Set objWord = CreateObject("Word.Application")
    Wscript.Echo "Active Printer:", objWord.ActivePrinter
    
    For Each objAddIn in objWord.AddIns
        Wscript.Echo "AddIn: ", objAddIn
    Next
    
    Wscript.Echo "Application:", objWord.Application
    Wscript.Echo "Assistant:", objWord.Assistant
    
    For Each objCaption in objWord.AutoCaptions
        Wscript.Echo "AutoCaptions:", objCaption
    Next
    Wscript.Echo "Automation Security:", objWord.AutomationSecurity
    Wscript.Echo "Background Printing Status:", objWord.BackgroundPrintingStatus
    Wscript.Echo "Background Saving Status:", objWord.BackgroundSavingStatus
    Wscript.Echo "Browse Extra File Type:", objWord.BrowseExtraFileTypes
    Wscript.Echo "Build:", objWord.Build
    Wscript.Echo "Caps Lock:", objWord.CapsLock
    Wscript.Echo "Caption:", objWord.Caption
    
    For Each objLabel in objWord.CaptionLabels
        Wscript.Echo "Caption Label:", objLabel
    Next
    
    Wscript.Echo "Check Language:", objWord.CheckLanguage
    
    For Each objAddIn in objWord.COMAddIns
        Wscript.Echo "COM AddIn:", objAddIn
    Next
    
    Wscript.Echo "Creator:", objWord.Creator
    
    For Each objDictionary in objWord.CustomDictionaries
        Wscript.Echo "Custom Dictionary:", objDictionary
    Next
    
    Wscript.Echo "Customization Context:", objWord.CustomizationContext
    Wscript.Echo "Default Legal Blackline:", objWord.DefaultLegalBlackline
    Wscript.Echo "Default Save Format:", objWord.DefaultSaveFormat
    Wscript.Echo "Default Table Separator:", objWord.DefaultTableSeparator
    
    For Each objDialog in objWord.Dialogs
        Wscript.Echo "Dialog:", objDialog
    Next
    
    Wscript.Echo "Display Alerts:", objWord.DisplayAlerts
    Wscript.Echo "Display Recent Files:", objWord.DisplayRecentFiles
    Wscript.Echo "Display Screen Tips:", objWord.DisplayScreenTips
    Wscript.Echo "Display Scroll Bars:", objWord.DisplayScrollBars
    
    For Each objDocument in objWord.Documents
        Wscript.Echo "Document:", objDocument
    Next
    
    Wscript.Echo "Email Template:", objWord.EmailTemplate
    Wscript.Echo "Enable Cancel Key:", objWord.EnableCancelKey
    Wscript.Echo "Feature Install:", objWord.FeatureInstall
    
    For Each objConverter in objWord.FileConverters
        Wscript.Echo "File Converter:", objConverter
    Next
    
    Wscript.Echo "Focus In MailHeader:", objWord.FocusInMailHeader
    
    For Each objFont in objWord.FontNames
        Wscript.Echo "Font Name:", objFont
    Next
    
    Wscript.Echo "Height", objWord.Height
    
    For Each objBinding in objWord.KeyBindings
        Wscript.Echo "Key Binding:", objBinding
    Next
    
    For Each objFont in objWord.LandscapeFontNames
        Wscript.Echo "Landscape Font Name:", objFont
    Next
    
    Wscript.Echo "Language", objWord.Language
    
    For Each objLanguage in objWord.Languages
        Wscript.Echo "Language:", objLanguage
    Next
    
    Wscript.Echo "Left", objWord.Left
    Wscript.Echo "Mail System:", objWord.MailSystem
    Wscript.Echo "MAPI Available:", objWord.MAPIAvailable
    Wscript.Echo "Math Coprocessor Available:", objWord.MathCoprocessorAvailable
    Wscript.Echo "Mouse Available:", objWord.MouseAvailable
    Wscript.Echo "Name:", objWord.Name
    Wscript.Echo "Normal Template:", objWord.NormalTemplate
    Wscript.Echo "Num Lock:", objWord.NumLock
    Wscript.Echo "Parent:", objWord.Parent
    Wscript.Echo "Path:", objWord.Path
    Wscript.Echo "Path Separator:", objWord.PathSeparator
    Wscript.Echo "Print Preview:", objWord.PrintPreview
    
    For Each objFile in objWord.RecentFiles
        Wscript.Echo "Recent File:", objFile
    Next
    
    Wscript.Echo "Screen Updating:", objWord.ScreenUpdating
    Wscript.Echo "Show Visual Basic Editor:", objWord.ShowVisualBasicEditor
    Wscript.Echo "Special Mode:", objWord.SpecialMode
    Wscript.Echo "Startup Path:", objWord.StartupPath
    
    For Each objTask in objWord.Tasks
        Wscript.Echo "Task:", objTask
    Next
    
    For Each objTemplate in objWord.Templates
        Wscript.Echo "Template:", objTemplate
    Next
    
    Wscript.Echo "Top:", objWord.Top
    Wscript.Echo "Usable Height:", objWord.UsableHeight
    Wscript.Echo "Usable Width:", objWord.UsableWidth
    Wscript.Echo "User Address:", objWord.UserAddress
    Wscript.Echo "User Control:", objWord.UserControl
    Wscript.Echo "User Initials:", objWord.UserInitials
    Wscript.Echo "User Name:", objWord.UserName
    Wscript.Echo "Version:", objWord.Version
    Wscript.Echo "Visible:", objWord.Visible
    Wscript.Echo "Width:", objWord.Width
    
    For Each objWindow in objWord.Windows
        Wscript.Echo "Window:", objWindow
    Next
    
    Wscript.Echo "Window State:", objWord.WindowState
    objWord.Quit