Solved

Removing references to Excel - use of xlLeft etc

Posted on 2010-11-27
5
789 Views
Last Modified: 2012-06-22
I am trying to not have references for excel, word etc on my access project - recommended when dealing with multiple version of excel 12, 14 etc.

I have changed around most of the code so it generically references objects rather than excel and this seems to work fine except one small issue with horizontal alignment.

Simple example  below - horizontal alignment OK if excel reference added but get error if not. Is there a way to set alignment without having the reference to excel?

Dim xlObj As Object
Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add ' xlFile, , True
Set Sheet = xlObj.ActiveWorkbook.Sheets(1)
Sheet.Range("A2") = 12
Sheet.columns(1).HorizontalAlignment = xlLeft
xlObj.Visible = True
Set Sheet = Nothing
Set xlObj = Nothing
0
Comment
Question by:donhannam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:hnasr
ID: 34223952
This is an automation of excel from access.
You issue excel commands and set excel object properties from access, hence you need to reference appropriate excel library in access to allow for that.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 34225749
The simplest way is to include definitions for those constants in your Access project. For example, if the xlLeft constant has a value of 0, then you'd need to build a new Constant in your Access project that defines that.

In a Standard Module (not a Form's module), add this in the General Declarations section (the very top of that modules' Code window):

Public Const xlLeft = 0

The drawback to this is that if Microsoft changes the "value" of that constat in future versions, your code could break or produce invalid results. However, the chances of that happening (i.e. MS changing the value of the constant) are very, very remote.
0
 

Author Closing Comment

by:donhannam
ID: 34226198
thanks for this - works perfect

I found the value for xlLeft by trial and error - it is 2.
xlLeft = 2
xlCentre = 3
xlRight = 4
Did not show these in help
0
 
LVL 85
ID: 34229474
Note you can use the Object Browser in the VBA Editor to find the values of these sorts of things. You'll need to have a reference set to the library (which is a good idea during development, since you can then use Intellisense - just be sure to remove it afterward), then do this:

1) Open the Object Browser (F2, or click View - Object Browser, or use the toolbar item)
2) In the Search Box (the second dropdown at the top left of the Object Browser) enter your search term: eg "xlLeft" (without the quotes)
3) Click the binoculars button, or tab out of the field to search.

VBA will show you what it finds, and the value of your constant will be shown at the bottom of the screen.

Note you can also filter the libraries you wish to search by using the top dropdown.

0
 

Author Comment

by:donhannam
ID: 34233132
LSMConsulting: - Thanks - Good pointer - I rarely use the object browser - will use it more.

So I got the values wrong before - 2 is actually left to right? - should be -4131 for xlLeft


0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

631 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