473,569 Members | 2,737 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 1817
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\redre e\Desktop\proje ct1.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
3995
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 Library), I am getting the following error: could not open macro storage (System.Runtime.comExeception) I am not getting any errors while opening...
0
1402
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 word.application object tries to create a new document using a specific template, I get either this error or one about incorrect file paths. I've been through...
4
6157
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 people in their job in order to finally know how many money costs us to run some projects. People always estimates the time when reporting and we do...
0
1294
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
4903
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 Word 11.0 Library), I am getting the following Errror: Could not open Macro storage. (System.Runtime.ComExeception) When I am Opening the word...
1
2113
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, but together they cause a colligion. (Can't save file. probably while other application reading it at the same time) My questions are: 1. Do I...
0
5084
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
4039
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 copy some text , then toggle to my open excel workbook and paste it after I do a "cntl+F". Eventually there will be quite a bit more to this macro,...
0
1251
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 Username() Username = Environ("UserName") End Function
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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 we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.