I have a vbscript that checks to see if MS Project is open. If it's already open it runs a macro if not it should open Project then run the macro. It works fine if Project is already open. If project isn't open the script successfully opens and runs the macro but fails half way through. Basically it fails because the macro that is being called opens files from project server. even with my default account set to the Project server url and 'when starting' set to 'choose my default account' it still fails.
vbscript to open & run macro:
dim pjApp
on error resume next
set pjApp = GetObject(, "MSProject.Application")
if err.Number = 0 then
pjApp.Visible = True
pjApp.macro "testsave"
else
Set pjApp = CreateObject("MSProject.Application")
pjApp.Visible = True
pjApp.macro "testsave"
end if
Set pjApp = Nothing
Is there a way of forcing it to connect to the project server site when Project opens?
Here is the real issue:
Basically it fails because the macro that is being called opens files from project server.
In order to automate MS Project and have it open to a project server, you need to launch winproj.exe using a command-line switch as follows:
VBScript
On Error Resume Next
Dim pjApp
Set pjApp = GetObject(, "MSProject.Application")
If Err.Number <> 0 Then
Dim ProjServer
ProjServer = Chr(34) & "enter project server name here" & Chr(34)
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "winproj /s " & ProjServer, 1, True
Set objShell = Nothing
WScript.Sleep 5000
Set pjApp = GetObject(, "MSProject.Application")
End If
pjApp.Macro "testsave"
The code first checks to see if MS Project is already open and if so, uses that instance. Otherwise it uses the shell command to open to a specific project server.
Note: Update sleep value as necessary to give MS Project enough time to open before trying to get a reference to it.
VBA version
On Error Resume Next
Dim pjApp As MSProject.Application
Set pjApp = GetObject(, "MSProject.Application")
If Err.Number <> 0 Then
Dim ProjServer As String
ProjServer = Chr$(34) & "enter project server name here" & Chr$(34)
Shell "C:\Program Files (x86)\Microsoft Office\Office14\Winproj.exe /s " & ProjServer, vbNormalFocus
Do While pjApp Is Nothing
DoEvents
Set pjApp = GetObject(, "MSProject.Application")
Loop
End If
pjApp.Macro "testsave"
Note: Update the path to Winproj.exe as necessary.
Documentation for command-line switches seems to have been removed from Microsoft's site. Here's a page that still provides the documentation:
Using Command-line switches for Project. Briefly:
/s "URL"
/u "username"
/p "password"
filename
-ProjectProfiles
In place of pjApp.macro try:
pjApp.Appllication.Run "testsave"
I know that it is weird that the .macro version works for the code above it; however, it is still worth a shot. This code should also work for both places where you are using the .macro method.
Related
How can I run a CMD or .bat file in silent mode? I'm looking to prevent the CMD interface from being shown to the user.
Include the phrase:
#echo off
right at the top of your bat script.
I have proposed in StackOverflow question a way to run a batch file in the background (no DOS windows displayed)
That should answer your question.
Here it is:
From your first script, call your second script with the following line:
wscript.exe invis.vbs run.bat %*
Actually, you are calling a vbs script with:
the [path]\name of your script
all the other arguments needed by your script (%*)
Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:
intWindowStyle : 0 means "invisible windows"
bWaitOnReturn : false means your first script does not need to wait for your second script to finish
See the question for the full invis.vbs script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False
^
means "invisible window" ---|
Update after Tammen's feedback:
If you are in a DOS session and you want to launch another script "in the background", a simple /b (as detailed in the same aforementioned question) can be enough:
You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window.
I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps popping up, here is your solution.
Use a VBS Script to call the batch file ...
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0
Set WshShell = Nothing
Copy the lines above to an editor and save the file with .VBS extension. Edit the .BAT file name and path accordingly.
Use Advanced BAT to EXE Converter from http://www.battoexeconverter.com
This will allow you to embed any additional binaries with your batch file in to one stand alone completely silent EXE and its freeware
Use Bat To Exe Converter to do this
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html (Choose Direct Download Link)
1 - Open Bat to Exe Converter, select your Bat file.
2 - In Option menu select "Invisible Application", then press compile button.
Done!
Try SilentCMD. This is a small freeware program that executes a batch file without displaying the command prompt window.
If i want to run command promt in silent mode, then there is a simple vbs command:
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM iexplore.exe"
if i wanted to open an url in cmd silently, then here is a code:
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("iexplore.exe http://otaxi.ge/log/index.php", 0)
'wait 10 seconds
WScript.sleep 10000
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM iexplore.exe"
I'm pretty confident I like this method the best. Copy and paste the code below into a .vbs file. From there you'll call the batch file... so make sure you edit the last line to specify the path and name of the batch file (which should contain the file you'd like to launch or perform the actions you need performed)
Const HIDDEN_WINDOW = 12
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("C:\PathOfFile\name.bat", null, objConfig, intProcessID)
It definitely worked for me. Comments are welcomed :)
Another way of doing it, without 3rd party programs nor converters ("batch to exe" programs actually just put your batch file in the tmp folder and then run it silently so anyone can just fetch it from there an get your code) no vbs files (because nobody knows vbs) just one line at the beginning of the batch file.
#echo off > NUL
The below silent .bat file code prevents the need to have two bat files (using "goto" and ":").
It does it all in the same .bat file. Tested and confirmed working in Windows 10
Make sure you replace "C:\pathToFile\ThisBatFile.bat " with the path to this same .bat file! Keep the space after ".bat".
#echo off
if [%1]==[] (
goto PreSilentCall
) else (
goto SilentCall
)
:PreSilentCall
REM Insert code here you want to have happen BEFORE this same .bat file is called silently
REM such as setting paths like the below two lines
set WorkingDirWithSlash=%~dp0
set WorkingDirectory=%WorkingDirWithSlash:~0,-1%
REM below code will run this same file silently, but will go to the SilentCall section
cd C:\Windows\System32
if exist C:\Windows\Temp\invis.vbs ( del C:\Windows\Temp\invis.vbs /f /q )
echo CreateObject("Wscript.Shell").Run "C:\pathToFile\ThisBatFile.bat " ^& WScript.Arguments(0), 0, False > C:\Windows\Temp\invis.vbs
wscript.exe C:\Windows\Temp\invis.vbs Initialized
if %ERRORLEVEL%==0 (
echo Successfully started SilentCall code. This command prompt can now be exited.
goto Exit
)
:SilentCall
cd %WorkingDirectory%
REM Insert code you want to be done silently.
REM Make sure this section has no errors as you won't be able to tell if there are any,
REM since it will be running silently. You can add a greater than symbol at the end of
REM your commands in this section to output the results to a .txt file for the purpose
REM of debugging this section of code.
:Exit
If your .bat file needs more than just the "Initialized" argument (which tells the bat file to go to :SilentCall section), add "^& WScript.Arguments(1)," , "^& WScript.Arguments(2)," ,etc. depending on the number of arguments, then edit the line where wscript.exe is called:
"wscript.exe C:\Windows\Temp\invis.vbs Initialized BatFileArgOne BatFileArgTwo"
I'm created RunApp to do such a job and also using it in my production env, hope it's helps.
The config like below:
file: config.arg
:style:hidden
MyBatchFile.bat
arg1
arg2
And launch runapp.exe instead.
You know , I can save the following code as .js file, and multiple webpage will be opened in different Tab in IE when I double click the js file.
How can I do the same thing in FireFox? Thanks!
var navOpenInBackgroundTab = 0x1000;
var oIE = new ActiveXObject("InternetExplorer.Application");
oIE.Navigate2("http://www.google.com");
oIE.Navigate2("http://www.msn.com/", navOpenInBackgroundTab);
oIE.Visible = true;
I"m not sure if this is the solution you're looking for but you can do this with a batch file. The *.bat file can be placed anywhere on your computer and when you click it, it will open Firefox with the appropriate browsers:
#echo off
Set URL="www.stackoverflow.com www.yahoo.com www.google.com"
set NewTab=-new-tab
set NewWindow=-new-window
For %%a in (%URL%) Do (Start /d "%programfiles%\Mozilla Firefox" Firefox.exe %Newtab% "%%a")
You could try something like this instead, that would open up in Firefox in a private window (I'm not sure if this is exactly what you're looking for):
#echo off
SET BROWSER=firefox.exe -private-window
SET WAIT TIME=2
#ping 127.0.0.1 -n %WAIT TIME% -w 1000 > nul
START %BROWSER% -new-tab "www.stackoverflow.com"
START %BROWSER% -new-tab "www.yahoo.com"
START %BROWSER% -new-tab "www.google.com"
pause
In my old system (XP) I created many shortcuts in a folder on my desktop to open programs like eclipse, notepad++, etc. and some script files. I assigned shortcuts like
Cnrl+Alt+E for Eclipse, Cnrl+Alt+N for notepad, etc.
I have backed up and restored the shortcuts folder on the desktop and batch/script file folder to my new system in the same path.
Is there any script to register all the shortcut to registry in one go?
The hotkey is a function of the file.
XP looks in 4 places to discover hotkeys:
%UserProfile%\desktop
%AllUsersProfile%\desktop
%UserProfile%\Start Menu
%AllUsersProfile%\Start Menu
and in these three file types:
*.lnk
*.pif
*.url
as long as the files are in those locations, the hotkeys should still work
This vbs script will show you the hotkey assigned to a file:
on error resume next
set WshShell = WScript.CreateObject("WScript.Shell" )
Set Arg=Wscript.Arguments
set lnk = WshShell.CreateShortcut(Arg)
If lnk.hotkey <> "" then
msgbox Arg & vbcrlf & lnk.hotkey
End If
use it with the filename passed to the script, e.g. thescript.vbs %UserProfile%\desktop\myshortcut.lnk
I've written a program in Access vba that goes to a fixed directory, zips all files in that directory (and subdirectories), and places the zip archive in a (typically) new directory for archival storage. It's the backup routine for my Access Front End and associated files. I've been running it from my client machine every day, using my personal credentials. Now I want to run it from my small SQL Server machine since it is more robust, and accountable. I'd prefer to do it all in T-SQL rather than "calling" the Access routines.
I've done some research, but can't find anything definitive that will help me with this. Can someone point me to some help? Here is the Access VBA code:
Function Zip_All_Files_in_Folder()
Dim FileNameZip, FolderName
Dim strDate As String, TargetPath As String
Dim oApp As Object
TargetPath = "H:\xxx\secure\Construction\Access\All Database Backup\" & Format(Date, "YYYY-MMM") & "_Backup\"
If Len(Dir(TargetPath)) = 0 Then
MkDir (TargetPath)
End If
FolderName = "H:\xxx\secure\Construction\Access\CPAS\"
strDate = Format(Date, "YY-MM-DD")
FileNameZip = TargetPath & strDate & ".zip"
'Create empty Zip File
NewZip (FileNameZip)
'Copy the files to the compressed folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items
'Keep script waiting until Compressing is done
On Error Resume Next
Dim time As Integer
Do Until (oApp.Namespace(FileNameZip).items.Count = _
oApp.Namespace(FolderName).items.Count) Or time > 180
Sleep (1000)
time = time + 1
Loop
On Error GoTo 0
'Send a message about the way the script ended
If time < 180 Then
SendEmail "xxxxxx#yyyyyy.com", "Looks like the zip backup worked." & vbCrLf & TargetPath
Else
SendEmail "xxxxxxx#yyyyy.com", "Better double check the Zip backup: " & time & " seconds" & vbCrLf & TargetPath
End If
DoCmd.Quit
End Function
Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
File operations or other shell operations from the shell are difficult from TSQL because there is no built-in support for them and permissions are a common problem (the SQL Server service accounts may not have permission to access the filesystem).
The easiest solution is simply to write a standalone script in your preferred language; not an Access-based script, because Microsoft recommends against server automation using Office. Then schedule it as a SQL Server job or using the Windows scheduler. Unfortunately you haven't mentioned either your version or edition of SQL Server, so it isn't clear what capabilities your server has (e.g. Express Edition does not include SQL Agent, so no scheduled jobs).
The following script is invoking the excel batch file in the remote machine. The batch file will open the excel workbook.
D:>psexec.exe \Host_name D:\Excel.bat
For the above case excel is opened in the background (process) but the workbook is not opened
Is there any way to open the excel book in the remote machine?
Schedule task has been created in remote PC to invoke the desired batch file
Batch file has been created to run the scheduled task (schtasks /run /tn taskname)
Run the batch file using psexec.exe \host_name
Running a GUI (excel) remotely to your machine is not that easy. The easier way, is to code vbscript in your Excel.bat to "open" the excel file programmatically and display the cell values to you on the command line. Of course, charts and such would not be available to you then. The other way, get the excel file to your local machine and open it locally
http://motevich.blogspot.com/2007/11/execute-program-on-remote-computer.html
strComputer = "."
strCommand = "notepad.exe"
Const INTERVAL = "n"
Const MINUTES = 1
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob")
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
If errReturn = 0 Then
Wscript.Echo "notepad.exe was started with a process ID: " & intJobID
Else
Wscript.Echo "notepad.exe could not be started due to error: " & errReturn
End If
String strComputer = "." means "local computer",
On remote computer strComputer = "servername"