473,505 Members | 14,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check if workbook is open and run a macro

3 New Member
I'm Looking for VBscript that will check to see if a specific Excel workbook is open and then run a macro, and in case if the workbook is close it will open the workbook.
i tried bellow code but it open the worbook as read only if the workbook is previously open.
when running the macro it will add 1 to cell F9, when running the macro via vbscript same workbook will open as read only and run the macro.
apreciate your help

Expand|Select|Wrap|Line Numbers
  1. Set xl = CreateObject("Excel.application")
  2.  
  3. xl.Application.Workbooks.open "C:\Users\redree\Desktop\project1.xlsm"
  4. xl.Application.Visible = True
  5. xl.Application.run "'project1.xlsm'!copy"
  6.  
  7. Set xl = Nothing
Nov 1 '13 #1
3 1809
MikeTheBike
639 Recognized Expert Contributor
Hi

pehaps somthing like this
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Function WorkbookIsOpen(ByVal BookName As String) As Boolean
  4.     Dim wkbk As Workbook
  5.     With Application
  6.         For Each wkbk In .Workbooks
  7.             If wkbk.Name = BookName Then
  8.                 WorkbookIsOpen = True
  9.                 Exit Function
  10.             End If
  11.         Next wkbk
  12.     End With
  13.     WorkbookIsOpen = False
  14. End Function
  15.  
  16. Sub Test()
  17.     MsgBox WorkbookIsOpen(ThisWorkbook.Name)
  18. End Sub
In your code change

With Application

to

With xl

assuming xl is a global or modyle level variable.

You will also need to extract the File Name from the Path string

HTH
Nov 1 '13 #2
redree
3 New Member
i'm stuck in 3rd line
my file name is "project" and file path is "C:\Users\redree\Desktop\project1.xlsm"
how can i insert them i your code for testing
thank you for your help as i am very new in scripting.
Nov 1 '13 #3
MikeTheBike
639 Recognized Expert Contributor
Try this
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Function WorkbookIsOpen(ByVal BookName As String) As Boolean
  4.     Dim wkbk As Workbook
  5.     With Application
  6.         For Each wkbk In .Workbooks
  7.             If wkbk.Name = BookName Then
  8.                 WorkbookIsOpen = True
  9.                 Exit Function
  10.             End If
  11.         Next wkbk
  12.     End With
  13.     WorkbookIsOpen = False
  14. End Function
  15.  
  16. Sub Test()
  17.     Dim Path As String
  18.     Dim FileName As String
  19.  
  20.     Path = "C:\Users\redree\Desktop\project1.xlsm"
  21.     FileName = Mid(Path, InStrRev(Path, "\") + 1)
  22.  
  23.     MsgBox WorkbookIsOpen(FileName)
  24. End Sub
??

MTB
Nov 4 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3985
by: Sivaraman.S | last post by:
Issue: When I am opening a word document or a template from an ASP.NET Web page (language used VB.NET Visual Studio Version 2003) using Word.ApplicationClass object (Reference Word 11.0 object...
0
1398
by: s_m_b | last post by:
and other errors too... I'm copying an office automation script from NT4 to w2k, and have as far is can be certain replicated all the settings correct, but at the point where the...
4
6152
by: mvivar | last post by:
Hi everybody: This will be not easy to explain as my mother language is not english, so my apologies in advance if it sounds confusing. We have a database access 97 wich controls time of...
0
1292
by: skumar | last post by:
I am trying to open Word from ASP.net. But when i try to open the word document i am getting "Could not open Macro Storage" Can anybody give me a .NET way of making this work.
0
4898
by: Sivaraman.S | last post by:
Issue : When I am trying to open a Word Document or a Template from a Web Application (ASP.NET Web page - Language Used VB.NET Visual Studio Version 2003) using Word.ApplicationClass (Refering...
1
2109
by: yduani | last post by:
Hi, I'm running 2 different executables: One saving infinitely an excell file, Other Read it infinitely. time duration for Save/Open is randomally. This 2 applications works fine differently,...
0
5081
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
4
4035
toddinator
by: toddinator | last post by:
Here's the situation.... I have a Word doc open and an Excel workbook open. I can easily toggle between the 2 open files by pressing Alt&tab. What I'd like to do is to set up a macro in Word to...
0
1247
by: scrapcode | last post by:
Hi everyone. I'm facing a problem with excel 2010 when using a function that worked fine in 2003. I'm using Environ(username) in the following function in module 1: Public Function...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.