Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Pass variabel to Excel open in VBA

Dear Expert,

When we open  excel file at command line  and running its macro , we will put the
code in Thisworkbook  sheet of  the excel file to run the macro autmatically
once the file is opend. I would like to know how to put variabe to the marco at
the command line.  Please advise, I try this command but it fail
C:> myexcel.xls  1

Duncan
----'Mymacro in the Thisworksheet

Sub workbook_open( i as integer )
if i= 0 then
call  previousmarco
Else
call udpatemacro
End Sub
0
duncanb7
Asked:
duncanb7
  • 4
  • 3
1 Solution
 
Chris BottomleyCommented:
0
 
duncanb7Author Commented:
Thanks, I get the commnadline string from the following code you suggestion, SO I can extract
the string of the commandline sting into my expect variable and use it for my application. Am i corret ?

The last question is that How could I run the excel file in readonly mode  because I need to re-run the
excel file at command in many timee for different varable chnage ?

'In THisworkbook
Private Sub Workbook_Open()
Dim CmdRaw As Long
Dim CmdLine As String
    
    CmdRaw = GetCommandLine
    CmdLine = CmdToSTr(CmdRaw)
    MsgBox CmdLine
End Sub 


in other macro sheet
=======================================

Option Base 0
Option Explicit

Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)

Function CmdToSTr(Cmd As Long) As String
Dim Buffer() As Byte
Dim StrLen As Long
   
   If Cmd Then
      StrLen = lstrlenW(Cmd) * 2
      If StrLen Then
         ReDim Buffer(0 To (StrLen - 1)) As Byte
         CopyMemory Buffer(0), ByVal Cmd, StrLen
         CmdToSTr = Buffer
      End If
   End If
End Function 

Open in new window

0
 
duncanb7Author Commented:
http://www.robvanderwoude.com/commandlineswitches.php#Excel, for excel readonly mode open at command line
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Chris BottomleyCommented:
Sorry too late in responding ... of course as you said:

/r for reead only on the command line

Chris
0
 
Chris BottomleyCommented:
BTW

I would suggest a B grade is unfair, the original question was about command line parameter passing, and was answered in full by the link, (and since the link is definitive there is nothing inappropriate in that).

Had I failed to respond to specific question thereof then perhaps.  Note the question re read only was additional but even then you should allow time to respond.

No point in chasing the admins to change the award though (even if you agree) i'm simply advising you on the normal etiquette.

Chris
0
 
duncanb7Author Commented:
No problem, I will re-credit it .
0
 
duncanb7Author Commented:
Thanks for quick reply
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now