Solved

Pass variabel to Excel open in VBA

Posted on 2010-11-09
8
302 Views
Last Modified: 2012-05-10
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
Comment
Question by:duncanb7
  • 4
  • 3
8 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34091071
0
 
LVL 13

Author Comment

by:duncanb7
ID: 34091197
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
 
LVL 13

Author Comment

by:duncanb7
ID: 34091221
http://www.robvanderwoude.com/commandlineswitches.php#Excel, for excel readonly mode open at command line
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34091297
Sorry too late in responding ... of course as you said:

/r for reead only on the command line

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34091406
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
 
LVL 13

Author Comment

by:duncanb7
ID: 34091425
No problem, I will re-credit it .
0
 
LVL 13

Author Closing Comment

by:duncanb7
ID: 34092900
Thanks for quick reply
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now