|
Create an Excel object in VB
A few weeks ago, a tip was sent out discussing how to create a
Word object in Visual Basic. Many questions were sent to me asking
how to do this same procedure with Excel.
First, assign a new reference to your VB app, "Microsoft Excel 8.0
Object Library" - Excel8.olb
Dim objExcel As New Excel.Application
'-- Display Excel Application
objExcel.Visible = True
'-- Add new Workbook
objExcel.Workbooks.Add
'-- Set Text
objExcel.ActiveCell(1, 1) = "Row 1 Col 1"
objExcel.ActiveCell(1, 3) = "Row 1 Col 3"
'-- Release the object variable
Set objExcel = Nothing
Be sure to check out the Object Browser in VB for more properties/
methods of the Excel object
返回
变量定义代表字符
在定义一些VB变量时,可以使用代表字符来表示,例如
Dim astr as String
可以表示为:
Dim astr$
下面是完整的变量定义代表字符列表:
String ($), Integer (%), Long (&), Single (!), Double (#), and Currency
(@)
返回
Let DateDiff()
determine if two dates are in the same month
To determine if two dates are in the same month, your first instinct
may be to simply use the Month() function on each date, then compare
the two resulting numbers. However, under these circumstances, the
comparison would equate 1/1/2000 with 1/1/1999. Instead, the
DateDiff() function provides one quick way to make this
determination, like so:
DateDiff("m", Date1, Date2)
In this expression, the DateDiff() function finds the difference in
calendar months between the two dates. If the expression returns a
zero, then the two dates are in the same calendar month. To include
this feature in an conditional expression, you could use the
following in a query:
If DateDiff("m", Date1, Date2) Then
'Month's are different
Else
'Month's are same
End If
返回
Get the Address
of Variables in Visual Basic
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:kbVBp kbVBp500 kbVBp600 kbGrpVB
======================================================================
-------------------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, versions 5.0, 6.0
-------------------------------------------------------------------------------
SUMMARY
=======
It is uncommon for a Visual Basic programmer to need to obtain low level
information on a variable, such as its memory address. However, there
are some
API functions that require such information. This article describes the
following Visual Basic functions that may help a Visual Basic programmer
obtain
this information:
VarPtr - Returns the address of a variable.
VarPtrArray - Returns the address of an array.
StrPtr - Returns the address of the UNICODE string buffer.
VarPtrStringArray - Returns the address of an array of strings.
ObjPtr - Returns the pointer to the interface referenced by an object
variable.
MORE INFORMATION
================
WARNING: One or more of the following functions are discussed in this
article;
VarPtr, VarPtrArray, VarPtrStringArray, StrPtr, ObjPtr. These functions
are not
supported by Microsoft Technical Support. They are not documented in the
Visual
Basic documentation and are provided in this Knowledge Base article "as
is."
Microsoft does not guarantee that they will be available in future releases
of
Visual Basic.
VARPTR
This function can be used to get the address of a variable or an array
element.
It takes the variable name or the array element as the parameter and returns
the
address. However, you should be aware that unlocked Dynamic Arrays may
be
reallocated by Visual Basic, so you must be very careful when you use
VarPtr to
get the address of an array element.
The following example gets the address of a variable:
Dim lngVariableAddress as Long
Dim dblMyVariable as Double
lngVariableAddress = VarPtr(dblMyVariable)
This example gets the address of the fourth element of an array:
Dim lngElementAddress as Long
Dim lngArrayOfLongs(9) as Long
' following will get address of 4th element
lngElementAddress = VarPtr(lngArrayOfLongs(3))
Limitations: The VarPtr function cannot be used to get the address of
an array.
For more information, see the VarPtrArray function.
VARPTRARRAY
Arrays in Visual Basic are stored as SAFEARRAYs. To get the address of
the
SAFEARRAY structure, you need to use the VarPtrArray function. The following
are
the Visual Basic 5.0 and Visual Basic 6.0 declarations respectively:
Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr"
_
(Var() as Any) As Long
Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr"
_
(Var() as Any) As Long
To get the address of a SAFEARRAY, pass the SAFEARRAY name (including
the
parenthesis) to the VarPtrArray function:
Dim lngSafeArrayAddress as Long
Dim lngArrayOfLongs(9) as Long
lngSafeArrayAddress = VarPtrArray(lngArrayOfLongs())
Limitations: The VarPtrArray function cannot be used to get the address
of an
array of Strings, because Visual Basic does UNICODE/ANSI conversion for
Strings.
If you use VarPtrArray on an array of Strings, you will get the address
of a
temporary ANSI copy of the array. For more information, see the
VarPtrStringArray function.
STRPTR
Strings in Visual Basic are stored as BSTR's. If you use the VarPtr on
a variable
of type String, you will get the address of the BSTR, which is a pointer
to a
pointer of the string. To get the address of the string buffer itself,
you need
to use the StrPtr function. This function returns the address of the first
character of the string. Take into account that Strings are stored as
UNICODE in
Visual Basic.
To get the address of the first character of a String, pass the String
variable
to the StrPtr function.
Example:
Dim lngCharAddress as Long
Dim strMyVariable as String
strMyVariable = "Some String"
lngCharAddress = StrPtr(strMyVariable)
You can use this function when you need to pass a pointer to a UNIOCODE
string to
an API call.
VARPTRSTRINGARRAY
VarPtrStringArray gets the address of an array of Strings. To avoid the
intrinsic
UNICODE/ANSI conversion performed by Visual Basic, the declaration has
to be
defined in a type library.
Alternatively, you could use the MIDL compiler to compile your own type
library
from the following .odl files.
For Visual Basic 6.0, create a text file named VB6ptrlib.odl with the
content
below:
#define RTCALL _stdcall
[
uuid(C6799410-4431-11d2-A7F1-00A0C91110C3),
lcid (0), version(6.0), helpstring("VarPtrStringArray Support for
VB6")
]
library PtrLib
{
importlib ("stdole2.tlb");
[dllname("msvbvm60.dll")]
module ArrayPtr
{
[entry("VarPtr")]
long RTCALL VarPtrStringArray([in] SAFEARRAY (BSTR) *Ptr);
}
}
For Visual Basic 5.0, create a text file named VB5ptrlib.odl with the
content
below:
#define RTCALL _stdcall
[
uuid(6E814F00-7439-11D2-98D2-00C04FAD90E7),
lcid (0), version(5.0), helpstring("VarPtrStringArray Support for
VB5")
]
library PtrLib
{
importlib ("stdole2.tlb");
[dllname("msvbvm50.dll")]
module ArrayPtr
{
[entry("VarPtr")]
long RTCALL VarPtrStringArray([in] SAFEARRAY (BSTR) *Ptr);
}
}
Use the following command lines to compile the preceding .odl files with
the MIDL
compiler to create a Visual Basic 6.0 or Visual Basic 5.0 type library
(.tlb)
file respectively:
MIDL /t VB6ptrlib.odl MIDL /t VB5ptrlib.odl
To use the VarPtrStringArray function in your project, you need to create
a
reference to the type library you just created.
Example:
Dim MyArrayOfStrings(2) As String
Dim AddressOfArray As Long
MyArrayOfStrings(0)="AAA"
MyArrayOfStrings(1)="BBB"
AddressOfArray = VarPtrStringArray ( MyArrayOfStrings() )
OBJPTR
ObjPtr takes an object variable name as a parameter and obtains the address
of
the interface referenced by this object variable.
One scenario of using this function is when you need to do a collection
of
objects. By indexing the object using its address as the key, you can
get faster
access to the object than walking the collection and using the Is operator.
In
many cases, the address of an object is the only reliable thing to use
as a
key.
Example:
objCollection.Add MyObj1, CStr(ObjPtr(MyObj1))
...
objCollection.Remove CStr(ObjPtr(MyObj1))
返回
Compile VB
Programs with Debug Symbols Embedded
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Enterprise Edition for Windows,
versions 5.0, 6.0
---------------------------------------------------------------------
SUMMARY
=======
The "Compile to Native Code" option introduced in Visual Basic
5.0 not only
improves performance of Visual Basic programs and components, but also
makes it possible to debug them with the Visual C++ debugger.
This article shows you how to compile a Visual Basic program or component
with embedded debug symbols. By doing so, you simplify the debugging
process and avoid the problems associated with mismatched and improperly
placed symbols.
MORE INFORMATION
================
All it takes to compile a Visual Basic program or component with embedded
debug symbols is a properly set environment variable, LINK. The key is
to
set it in such a way that Visual Basic inherits the setting when it is
launched. To do this, follow the steps below:
There are several ways to set the LINK environment variable, each of these
methods is outlined below:
Windows NT or Windows 95/98
---------------------------
1. Open an MS-DOS Prompt.
2. Navigate to the folder containing Visual Basic.
3. Execute the following command, "set link=/pdb:none" without
quotes.
4. Start Visual Basic.
Windows NT Only
---------------
1. From the Control Panel, select the System icon.
2. Select the Environment tab.
3. In the Variable entry, enter "link" without quotes.
4. In the Value entry, enter the following:
/pdb:none
5. Press Set, then Apply.
6. Start Visual Basic.
Windows 95/98 Only
------------------
1. Make a backup copy of the AutoExec.Bat file.
2. Open the Autoexec.bat file with Notepad.Exe or any text editor.
3. Add the following entry to the AutoExec.Bat file:
set link=/pdb:none
4. Save the AutoExec.Bat file.
5. Execute the AutoExec.Bat file or reboot the machine.
6. Start Visual Basic.
Once the LINK environment variable is set, a Visual Basic project can
be
compiled with embedded debug symbols. The following steps describe how
to create a test program and compile it with embedded debug symbols:
Step-by-Step Example
--------------------
1. Create a new Standard EXE project in Visual Basic. Form1 is created
by
default.
2. Add a CommandButton (Command1) to Form1.
3. Add the following code to Form1:
Private Sub Command1_Click()
Dim s As String
s = App.EXEName
Print s
End Sub
4. Select Project1 Properties from the Project menu.
5. Select the Compile tab.
6. Select Compile to Native Code, check Create Symbolic Debug Info, and
select No Optimization.
7. Save the project and create Project1.EXE. Note that the EXE is created
with embedded debug symbols.
返回
Install
Crystal Reports for Use in Visual Basic 6.0
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:6.0
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, version 6.0
- Microsoft Visual Studio 6.0
---------------------------------------------------------------------
SUMMARY
=======
Crystal Reports is not part of the Visual Basic or Visual Studio setup.
However, you can install it from the Visual Basic and Visual Studio CDs.
NOTE: Starting with Visual Basic 6.0, Microsoft has included a new report
generator called the Microsoft Data Report Designer. You may use either
the
new Report Designer or Crystal Reports. For more information about the
new
Report Designer, please search the MSDN Library included within Visual
Studio 6.0 on the phrase "Microsoft Data Report Designer."
MORE INFORMATION
================
To install Crystal Reports, run Crystl32.exe. The location of Crystl32.exe
depends on which version of Visual Basic or Visual Studio you own. In
all
cases, it is in the following relative path:
\Common\Tools\VB\CrysRept\Crystl32.exe
Use the following table to determine which CD to use:
Product Disk
-----------------------------------
Visual Basic Professional 1
Visual Basic Enterprise 1
Visual Studio Professional 2
Visual Studio Enterprise 3
NOTES:
- The Learning Edition does not include Crystal Reports.
- The version of Crystal Reports is 4.6.1.0, which is the same version
that comes with Visual Basic 5.0.
- Where the Help file (Crw.hlp) refers to Visual Basic 5.0, you can assume
the same applies to Visual Basic 6.0.
- The setup program (Crystl32.exe) does not add a shortcut to the Programs
menu in Windows. To start Crystal Reports, run Crw.exe in the installed
folder or click Report Designer on the Add-Ins menu in Visual Basic.
- Microsoft supports installation of the version of Crystal Reports that
ships with Visual Basic. For usage issues, please contact Seagate
Software. For additional information, please see the following article
in the Microsoft Knowledge Base:
返回
Build a Setup
Program Creating Multiple Groups and Icons
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, version 5.0
---------------------------------------------------------------------
SUMMARY
=======
By default, the Microsoft Visual Basic 5.0 Setup Wizard creates one program
group using the project name, and one icon within the program group for
the
executable file generated by the project. This article shows how to create
additional program groups and more than one icon within a group for any
additional executable files you want to ship by customizing the Visual
Basic Setup Toolkit's Setup1.vbp project.
NOTE: Microsoft Technical Support does not support the modification of
the
setup process or any of the setup files. Support is provided for the Setup
Wizard on an "as is" basis only.
MORE INFORMATION
================
To allow for multiple group and icon creation, the Setup Toolkit uses
CreateShellGroup and CreateOSLink procedures. These procedures can be
used
to create custom groups and icons.
NOTE: Before making any modifications to the Setup1.vbp, backup the
contents of the Setup1 directory. This directory can be located in the
following path:
c:\program files\devstudio\vb\setupkit\setup1\setup1.vbp
(Modify path information accordingly.)
Step-By-Step
------------
1. Start Microsoft Visual Basic and open the Setup1.vbp project located
in
the following directory by default:
c:\program files\devstudio\vb\setupkit\setup1\setup1.vbp
(Modify path information accordingly.)
2. In the Project Explorer window, click to expand the Forms collection.
3. Right-click on the form "frmSetup1" and click View Code.
4. In the Form_Load event, locate the following line of code:
CreateOSLink frmSetup1, strGroupName, gsDest.strAppDir & "My
Exe 1.exe",
"My Exe 1 command-line arguments", "My Exe 1"
NOTE: The line above represents one line of code.
5. The lines of code below demonstrate how an additional group and icon
can
be created:
'Allows for creation of additional icons
fAdditionalIcons = True
'Creates a group entitles "My Group" on the Programs menu
fCreateShellGroup "My Group", False, True
'Creates a shortcut in the "My Group" folder, where the target
'application is test.exe and is entitled "My test exe file"
CreateOSLink frmSetup1, "My Group", gsDest.strAppDir & "test.exe",
_
"", "My test exe file"
6. On the File menu, click Make Setup1.exe. If desired, save the Setup
Toolkit project and exit Visual Basic.
7. Use the Setup Wizard as normal to create the application distribution
set.
返回
Back to top
|