Writing to an open workbook from a VB script

jtdewhurst

New Member
Joined
Jan 16, 2015
Messages
26
Hi, I've done quite a bit of research, and I haven't found anything.

Is there anyway to write to a workbook that the current user already has open from a VB Script?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Usually if it is opened and locked for editing by another user then it is not possible to edit that workbook either manually or through any Macro.
But in case if you are looking for writing to another Excel from one Excel, then try this link that discuss on different methods to do that.

Different Methods to Import Data From External Excel File | OfficeTricks – Technical Bloggers Space

Basically I'm trying to find a way for a Script to output data to an excel worksheet. the Script is itself called from the open workbook. I was wonder if because the user who has the workbook open is the same one running the script if there was anyway to write data to the workbook without closing it first.
 
Upvote 0
Yes. It is possible using the Workbooks collection object. But the limitation is that you should exactly mention the workbook name inside the code.
Either you can search in this forum for Workbooks Collection or refer point 2 in the link that I provided for a sample code.
 
Upvote 0
Basically I'm trying to find a way for a Script to output data to an excel worksheet. the Script is itself called from the open workbook.
It can be done by having the VBScript refer to the open workbook by its name - the Set Wb line in the following code, which writes the current time to A1 on the first sheet.
Code:
Public Sub Create_and_Run_VBScript()

    Dim VBScriptFile As String
    Dim fileNum As Integer
        
    'Create the VBScript.vbs file
    
    VBScriptFile = Environ("temp") & "\VBScript.vbs"
    fileNum = FreeFile
    Open VBScriptFile For Output As fileNum
    
    Print #fileNum, "Option Explicit"
    Print #fileNum, "Dim Excel, Wb"
    Print #fileNum, "Set Excel = GetObject(, ""Excel.Application"")"
    Print #fileNum, "Set Wb = Excel.Workbooks(""" & ThisWorkbook.Name & """)"
    Print #fileNum, "Wb.Worksheets(1).Range(""A1"").Value = Now"
    Close fileNum
        
    'Debugging aid - open the script in Notepad
    
    Shell "Notepad " & VBScriptFile
                    
    'Run the script
    
    Shell "wscript " & Chr(34) & VBScriptFile & Chr(34), vbNormalFocus
                                                   
End Sub
Simply call Create_and_Run_VBScript from the Workbook_Open routine in the ThisWorkbook module to run the script when the workbook is opened.
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,533
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top