How can I display an operating-system environment variable from within Excel? - windows

I have an application written in Excel plus a bunch of C++ / Python addins.
The location of the various config files used by the addins is determined at startup time by a number of environment variables. I'd like to debug a problem related to these environment variables by the most direct possible means: Can I simply type in an Excel formula which will cause an environment variable to display in the worksheet?
Let me give you an example:
I have an environment variable called "MYADDIN_XML_CONFIG" which contains the path to an XML file used by the MyAddin component. If this environment variable is set incorrectly then MyAddin will fail to run. I'd like to have a single simple function which takes the string "MYADDIN_XML_CONFIG" as an argument and returns the value of the env-var if it is set. If the environment variable is not set it should return a NONE or some kind of error code.
Can this be done?
FYI, MS Excel 2003 on Windows XP.

I don't know any built-in Excel formula that does this, but you can create a public function in a VBA module:
Public Function Env(Value As Variant) As String
Env = Environ(Value)
End Function
then use this as a user-defined formula in a worksheet, e.g.
=Env("COMPUTERNAME")

Use the VBA function Environ("MYADDIN_XML_CONFIG").

Unfortunately the accepted solution won't work if your environment variable changes its value (even if you reopen the Excel workbook, the value of the cell won't change). If you want to have it updated more often you should update cell's value on some event,
e.g. Worksheet_Activate (code should be placed in VBAProject->Microsoft Excel Objects->Sheet which should be observed):
Private Sub Worksheet_Activate()
Range("a1") = Environ("SOME_ENV_VARIABLE")
End Sub
or Workbook_Open (code should be placed in VBAProject->Microsoft Excel Objects->ThisWorkbook):
Private Sub Workbook_Open()
Range("a1") = Environ("SOME_ENV_VARIABLE")
End Sub

Related

VBScript passing value by reference (VarPtr) to ActiveX function

I'm trying this:
Dim oApp
Dim iReturnedResult
Set oApp = CreateObject("Some.Application")
Set F_Ord = oApp.Documents.Open("Window 1", VarPtr(iReturnedResult))
The ActiveX control expects the second parameter to be a Long by reference.
This works perfectly well inside Excel VBA.
I can run this step by step, and see the result is returned like it should.
But, when I move this code to a VBS file and run it from the command line (CScript.exe), I get an error 800A000D, meaning it's the wrong type.
I have also tried creating an array instead, and tested with these commands, without any luck:
Set F_Ord = oApp.Documents.Open("Window 1", VarPtr(iReturnedResult(0)))
Set F_Ord = oApp.Documents.Open("Window 1", iReturnedResult(0))
Does anyone know how to pass a long variable by reference to an ActiveX control from VBScript?
The simple answer is VarPtr() is not supported by VBScript.
To my knowledge, there is no equivalent that allows you to pass a pointer to a variables memory address.
Useful Links
Visual Basic for Applications Features Not In VBScript

Scripting Word from vbs

I'm trying to get Word to fill in cells in a table. The script works when run as a macro from within Word, but fails when saved as a .vbs file and double-clicked, or run with wscript. This is a part of it.
set obj = GetObject(,"Word.Application)
With obj
With .Selection
MsgBox .text
If (.Information(wdWithInTable) = True) Then
.Collapse Direction:=wdCollapseStart
tCols = .Tables(1).Columns.Count
tRow = .Information(wdStartOfRangeRowNumber)
tCol = .Information(wdStartOfRangeColumnNumber)
For I = 2 To 5
.Tables(1).Cell(tRow, I).Range.Text = "fred" & Str(I)
Next
` now make new row
For I = 1 To tCols - tCol + 1
.MoveRight unit:=wdCell
Next
End If
End With
End With
I have three problems. First, it won't compile unless I comment out the .Collapse and .MoveRight lines. Second, although the MsgBox .text displays the selected text, I get "out of range" errors if I try to access any .Information property.
I'm sure I'm missing something very simple: I usually write software for Macs, and I'd do this using AppleScript. This is my first attempt at getting anything done under Windows.
VBScript and VBA are different languages.
They are a bit similar, but not very. Moreover, VBScript is not like AppleScript; it doesn't let you easily interface with running programs.
The interfaces you'll get from VBScript can behave subtly differently in VBA and VBScript. However, I think you've got two problems here:
:= is invalid syntax in VBScript; you'll need to find an alternative way of calling the function. Try just using positional arguments.
You've no guarantee that this will open the expected file; there could be another instance of Word that it's interacting with instead.
Since your code is not running within the Word environment it would require a reference to the Word object library in order to use enumeration constants (those things that start with wd).
VBScript, however, cannot work with references, which means the only possibility is to use the long value equivalents of the enumerations. You'll find these in the Word Language References. Simplest to use is probably the Object Browser in Word's VBA Editor. (In Word: Alt+F11 to open the VBA Editor; F2 to start the Object Browser; type in the term in the "Search" box, click on the term, then look in the bottom bar.)
The code in the question uses, for example:
wdWithInTable
wdCollapseStart
wdStartOfRangeRowNumber
wdStartOfRangeColumnNumber
wdCell
The reason you get various kinds of errors depends on where these are used.
Also, VBScript can't used named parameters such as Unit:=. Any parameters must be passed in comma-delimited format, if there's more than one, in the order specified by the method or property. If there are optional parameters you don't want to use these should be left "blank":
MethodName parameter, parameter, , , parameter

Convert VBA code to Vbscript

I am using the below code in macro to delete blank rows in excel. Can you please help me in converting the same into Vbscript?
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
waiting for your valuable response.
VBScript doesn't provide implicit parent objects like the VBA runtime environment does, so you need to make everything explicit:
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Add
Set ws = wb.Sheets(1)
ws.Columns("A:A").Select
...
Also, VBScript doesn't recognize VBA named constants, so you need to either use the numeric value:
...
xl.Selection.SpecialCells(4).Select
...
or define the constant in your script:
Const xlCellTypeBlanks = 4
...
xl.Selection.SpecialCells(xlCellTypeBlanks).Select
...
See here for more information about translating VBA to VBScript.

Embed EXE file in the Excel file

I use:
retVal = Shell("program.EXE " & filename, vbNormalFocus)
To execute a program need for my excel spreadsheet.
Is it possible to embed the EXE file in the excel file itself?
And how would I execute it then?
Ideias:
1 - Some kind of a bin2str function to turn binary to string (so I can store it in the program as a variable and a str2bin (the oposite)
2 - I read something about OLE Control (that you can embed it there), but I really don't know where to start on this one
Here's an outline solution that avoids OLE:
Create a hidden worksheet.
Use a base 64 encoded to convert the exe to text.
Store that text in worksheet cells on the hidden worksheet. Since there is a limit on the number of characters in a cell (32,767) you will need to break the string into chunks.
Obviously you'll need to reverse this procedure when you want to save and execute the exe file.
You can do this by using: Insert > Object and then selecting 'Create from File'.
To add it to your sheet using VBA:
Dim o As OLEObject
Set o = ActiveSheet.OLEObjects.Add(Filename:="C:\program.exe")
Then this is the command to execute program.exe:
o.Verb Verb:=xlPrimary
Not sure how to pass arguments to it, however (e.g. your filename).
Note: Untrusted applications prompt a warning when you run them.

How to reference a variable in VB6?

Is it possible in Visual Basic 6 to make some variable to reference to another variable, so when one changes, so does the other?
I know it is possible to use Set operator for objects. But how to make this work for integer type variables? The only way I am aware of is to wrap the variable inside an object.
Not through the language itself. You could use a class as you mentioned, the other way is to use the Win32 API.
Specifically
HeapAlloc to allocate memory. You will store the returned address in a Long variable.
Then use RTLMoveMemory renamed as CopyMemory to transfer data in and out of the allocated memory.
Public Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)
This website has a more complete example of using pointers in VB6.
I wrote a custom Reference Object class which sounds like it would do exactly what you are looking for. You can read up on it and download it here: http://battaglia.homedns.org/vbguyny/development/visualbasic6/visualbasic6_20070218.htm
try putting variable A in a textbox then make an on change event on the text box., then put the value the text to variable B.
textbox1.text = A
onchgange textbox1
B= textbox1.text
its wat im using., the most easiest way for me
The method of assigning a variable to a text box to set a reference is wrong. It does not do what is stated. Assigning a variable to a text box or assigning a text box to a variable COPIES the content of the text box to the variable. It is not setting a reference to it!

Resources