controling excel from vba access and using sub procedures

Hi All,
I'm having trouble getting my code to work using sub procedures in my vba
code, this only happens when i am interacting with excel. I've tried to copy
all the dims and defs from the calling procedure but to no avail at this time.
sample code below.

Dim strPath As String
Dim rst As DAO.Recordset
Dim Excel_Application As Excel.Application
Dim Excel_Workbook As Excel.Workbook
Dim Current_Worksheet As Excel.Worksheet
Dim Data_Range
Dim Worksheet_Name
Dim db As Database
Dim rs As Recordset
D_now = Format(Date, "dd-mm-yy") ' Formated to use as part of file name
gg = "C:\Employee Time Report " & D_now & ".xls"
Is_File= Len(Dir(gg))
If Is_File= 0 Then
Else
    T_Msg = MsgBox("File already exists, Delete file and continue ?.",
vbYesNo, "")
    If T_Msg = vbYes Then
        Kill gg
    Else
        Exit Sub
    End If
End If
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Report Output",
gg, True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Comments", gg,
True
Set Excel_Workbook = GetObject(gg)
Set Excel_Application = Excel_Workbook.Parent
Excel_Application.WindowState = xlMinimized
Excel_Application.Visible = True
Excel_Workbook.Windows(1).Visible = True
Excel_Workbook.Worksheets(1).Name = "Report Output"
Set Current_Worksheet = Excel_Workbook.Worksheets("Report Output")
Excel_Workbook.Worksheets("Report Output").Select

THE code here works if left here but not in it's own sub procedure.- 'CAll
Add_Comments'

    Current_Worksheet.Range("B11").AddComment
    Current_Worksheet.Range("B11").Comment.Visible = False
    Current_Worksheet.Range("B11").Comment.Text Text:="trevorc:" & Chr(10) &
"test"

I was hoping to then add code to this sub procedure.

-- 
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/access-modules/201002/1

0
trevorC
2/1/2010 9:05:49 AM
access.modulesdaovba 1670 articles. 0 followers. Follow

3 Replies
934 Views

Similar Articles

[PageSpeed] 14

hi Trevor,

On 01.02.2010 10:05, trevorC via AccessMonster.com wrote:
> THE code here works if left here but not in it's own sub procedure.- 'CAll
> Add_Comments'
>
>      Current_Worksheet.Range("B11").AddComment
>      Current_Worksheet.Range("B11").Comment.Visible = False
>      Current_Worksheet.Range("B11").Comment.Text Text:="trevorc:"&  Chr(10)&
> "test"
>
> I was hoping to then add code to this sub procedure.
It works if you define it correctly:

Public Sub AddComments(AWorkSheet As Excel.Worksheet)

   AWorkSheet.Range("B11").AddComment
   AWorkSheet.Range("B11").Comment.Visible = False
   AWorkSheet.Range("B11").Comment.Text _
     Text:="trevorc:" & Chr(10) & "test"

End Sub

Use it as

   AddComments Current_Worksheet

or

Public Sub AddComments(ARange As Excel.Range)

   ARange.AddComment
   ARange.Comment.Visible = False
   ARange.Comment.Text _
     Text:="trevorc:" & Chr(10) & "test"

End Sub

Use it as

   AddComments Current_Worksheet.Range("B11")


I would assume the second method is what you need.


mfG
--> stefan <--
0
Stefan
2/1/2010 9:21:39 AM
It is quite easy to perform operations in Excel, and control the entire 
process from Access. 

Make sure you set a reference to Excel, and then run this code in an Access 
module:

Option Compare Database
Option Explicit ' Use this to make sure your variables are defined
' One way to be able to use these objects throughout the Module is to 
Declare them here, and not in a Sub

Private objExcel As Excel.Application
Private xlWB As Excel.Workbook
Private xlWS As Excel.Worksheet
 
Sub Rep()
Dim strFile As String
strFile = "C:\MyExcelWorkbook.xls"

' Of course, this is just an example; put the actual path to your actual 
file here…
' Opens Excel and makes it Visible
Set objExcel = New Excel.Application
objExcel.Visible = True

' Opens up a Workbook
Set xlWB = objExcel.Workbooks.Open(strFile)

' Sets the Workseet to the last active sheet - Better to use the commented 
version and use the name of the sheet.
Set xlWS = xlWB.ActiveSheet

' Set xlWS = xlWB("Sheet1")
With xlWS ' You are now working with the Named file and the named worksheet
' Your Excel code begins here…you can record a macro and make the process 
super easy!!
End With

' Close and Cleanup
xlWB.SaveAs xlSaveFile
xlWB.Close
xlapp.Quit
Set xlapp = Nothing

End Sub

-- 
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Stefan Hoffmann" wrote:

> hi Trevor,
> 
> On 01.02.2010 10:05, trevorC via AccessMonster.com wrote:
> > THE code here works if left here but not in it's own sub procedure.- 'CAll
> > Add_Comments'
> >
> >      Current_Worksheet.Range("B11").AddComment
> >      Current_Worksheet.Range("B11").Comment.Visible = False
> >      Current_Worksheet.Range("B11").Comment.Text Text:="trevorc:"&  Chr(10)&
> > "test"
> >
> > I was hoping to then add code to this sub procedure.
> It works if you define it correctly:
> 
> Public Sub AddComments(AWorkSheet As Excel.Worksheet)
> 
>    AWorkSheet.Range("B11").AddComment
>    AWorkSheet.Range("B11").Comment.Visible = False
>    AWorkSheet.Range("B11").Comment.Text _
>      Text:="trevorc:" & Chr(10) & "test"
> 
> End Sub
> 
> Use it as
> 
>    AddComments Current_Worksheet
> 
> or
> 
> Public Sub AddComments(ARange As Excel.Range)
> 
>    ARange.AddComment
>    ARange.Comment.Visible = False
>    ARange.Comment.Text _
>      Text:="trevorc:" & Chr(10) & "test"
> 
> End Sub
> 
> Use it as
> 
>    AddComments Current_Worksheet.Range("B11")
> 
> 
> I would assume the second method is what you need.
> 
> 
> mfG
> --> stefan <--
> .
> 
0
Utf
2/1/2010 5:15:02 PM
Hi,
I don't have a problem with interfacing with excel, theres only 2 things I
can't do as yet (turn off print text box(I can create it but not turn it off
when printing), create code and save it to a macro in excel, I can create the
macro but not assign the code to it.),but thats a different day.

What  I am trying to do is re-use common code that i need in a loop, I don't
want to have 50 copies of this in my app, so I thought to use a sub procedure
and call it were required. This works fine in access for other subs I have
created but not if I am also interacting with excel at the same time. I have
tried adding the references to excel that are in the original calling
procedure as well.
All help is greatfully accepted.
regards
Trevor.

-- 
Message posted via http://www.accessmonster.com

0
trevorC
2/4/2010 10:22:40 PM
Reply:

Similar Artilces:

excel database 25,000 plus names #2
I am working with a database i set up in excel. It has over 25,000 names, total size of file is 274mb. If i transfer all my data from an excel database to access, will i work faster. Right now it takes forever to open and save. I am told it is not my computer. any tips??? -- mikedipaol ----------------------------------------------------------------------- mikedipaola's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=3568 View this thread: http://www.excelforum.com/showthread.php?threadid=55465 ...

New to excel
Hi All, I'm new to Excel ( and to this forum :) ) and so I hope somebody may b able to help me. I've got 2 questions.... QUESTION 1 I've got a spreadsheet which takes data from one worksheet and uses i to calculate data in a second worksheet using the following code formula: =IF('4th November 2005'!B19="","nothing here dude",IF(B19<'4th Novembe 2005'!B19,"UP",IF(B19='4th November 2005'!B19,"Same",IF(B19>'4t November 2005'!B19,"DOWN")))) The problem is, when I create a new worksheet I have...

Reporting from Project Server
I dont know if i need to ask this question here or in the Access section. I have an ODBC connection to the Project Server database so I can make reports through Access. Access' limit of 255 fields per table is causing me some trouble. for example, the MSP_VIEW_PROJ_PROJECTS_ENT table has well over 255 fields. Access only shows me the first 255 fields. how can I change that so I can see all the fields in that table? thanks, Hadi Hadi, I have not tried this yet it may be a viable option. Have your DBA create a view that pulls the key fields to this table and the specifi...

Looking for Excel Help
I'm a very novice Excel user and am looking for a little help with creating a formula for a spreadsheet I'm creating for my personal use. I would appreciate some assistance if possible. Thanks in advance. Dan --- Message posted from http://www.ExcelForum.com/ Hi Dan! Post a sample of what you want to do. Your question is just a tad open ended <g> -- Regards Norman Harker MVP (Excel) Sydney, Australia njharker@optusnet.com.au Excel and Word Function Lists (Classifications, Syntax and Arguments) available free to good homes. "DanB4105" <DanB4105.ywtpa@excelfor...

Using Relative path for XML data file?
Is there a way to specify a relative path to an XML data file imported into Excel 2003? I am writing a web app that generates report data as XML for the user to download to their local machine. This data is to be consumed by an Excel reporting spreadsheet, which contains display-formatted tables and charts that are mapped to various data fields in an XML Map, which is in turn linked to the xml data file they will download. The idea is the user only needs to download the data for updates, not the whole spreadsheet. However, since I cannot predict the path where the user will store their...

Excel button problem
Hi All I have a macro that copies a worksheet in the active workbook and puts it into a new workbook - then formats it and deletes any buttons on the worksheet. On the first click on the button the macro works ok. On the second click, it fails because the all assigned macros on all buttons in the active workbook changed from "mba" to "book1!mba". Book 1 doesn't exists (wasn't opened, wasn't saved, doesn't have the macros). I've never experienced this problem before?? Can anyone help to solve this problem? FYI The macro to do this is c...

Excel 97 #9
Please can anyone help??? I have two columns in Excel 97. The first contains a list of statu values eg. pending, or granted or withdrawn. The second contains date eg.01/12/1997, 05/06/2003. I woudl like to know how to get all th granted apps before 31/12/2003. Can anyone help please -- Message posted from http://www.ExcelForum.com theres many ways, but an easiest way would be to do a sort. Highlight the 2 columns, click on data, then sort, then sort by status, then by date. this should group them all together. hope this helps...toe >-----Original Message----- >Please can anyo...

Formula without using numbers after decimal in the answer
I have a formula that derives the answer from a figure with a decimal. I don't want to use the figures after the decimal. Is there a way to just use the whole number and omit the numbers after the decimal without having to manually key in all these numbers manually? Thanks, Mustang You can use the INT function. This 'rounds down' any number to th nearest integer, e.g. if A1=2.567, a formula in B2 of =INT(A1) return 2 HTH Bruc -- swatsp0 ----------------------------------------------------------------------- swatsp0p's Profile: http://www.excelforum.com/member.php?...

how to find an item in a list control?
hi all, In a list Control how to find an item. If it is found then the item should be highlighted. how to do this? i wrote the following code but its not working what's wrong in the code? LVFINDINFO info; int nIndex; info.flags = LVFI_PARTIAL|LVFI_STRING; info.psz = (LPCTSTR) str; //fd.m_strEditCtrl; sprintf(st,"%d",m_ListCtrl.GetItemCount()); MessageBox(st,"Count"); for(int i=0;i < m_ListCtrl.GetItemCount();i++) { if((nIndex = m_ListCtrl.FindItem(&info,-1)) == -1 ) { m_ListCtrl.SetItemState(nIndex,LVIS_SELECTED,LVIS_SELECTED); } } thanks and r...

mouse highlighting webbrowser control
Hi, I'm using the webbrowser control. Mouse highligting isn't working (ie. pressing and holding the left mouse button, moving over the items/text to highlight and releasing). Is this something I have to implement manually or is there another reason why this isn't working? Thanks, Craig ...

How do I Remove a Split from my Comments in Excel 2003? #2
I have set my current workbook to split/freeze the first column and first 2 rows. Now, when I add a comment to the second row (in any column) my comments are cut off if I should scroll down. I don't ever remember the behavior before. And I don't know what I've done to enable it but it's really annoying. How do turn this off ? ...

learning Excel #3
Hi, I was considering learning Excel as an additional tool for my data analysis work. Is it better to use data sets that I have previously used with SPSS and apply the same analysis tools as in SPSS? For applying the appropriate tools, I was considering using excel's online help. The second option I have is to use some excel book for data analysis and apply the techniques to data sets provided with the book. Any suggestions????? regards Metal ...

unable to paste Excel 2003 chart into Outlook 2003
(This was posted on "excel.charting" group.) I have a user who's unable to paste an Excel 2003 chart into Outlook 2003 email message. In Outlook options, the checkbox is selected for "Use Microsoft Office Word 2003 to edit e-mail messages". When I tested this on my own computer running the same version of Office, if the box is check, I have no problem pasting; if this box is cleared, I cannot paste. But on his computer, it doesn't work regardless. Thanks and regards, TL ...

Disable Secure Sockets Layer on exchange server when using RPC over HTTP
Hi im trying to enable RPC over HTTP to enable users to establish contact to my Excahger server 2003 over the internet. Now, I dont want to use SSL (security not that important) and i am told by this article that i can disable SSL in windows registry. Quote: Note While RPC over HTTP does not require Secure Sockets Layer, you must modify the registry to enable RPC over HTTP if you do not want to use Secure Sockets Layer. Microsoft recommends that you enable and require Secure Sockets Layer for your RPC over HTTP communications. At this address: http://support.microsoft.com/?id=833401 But i ...

using the journal on outlook
Once I link an email to the journal, can I still find that email in my mail box? I seem to be able to get to it only via the journal. If this is the way it is supposed to be, how do I remove it from the journal and get it back into my mail box? Am I just missing something? -- thanks, Independent Are you linking to the item or putting a copy into the journal item? Also, has the item been archived or not? "Independent" <Independent@discussions.microsoft.com> wrote in message news:868279F2-53C8-403A-97F5-604CEECD873C@microsoft.com... > Once I link an email to the journ...

Excel corrupts when asking to update vlookups
We are experiencing weird behavior with some Office 2K3 Excel spreadsheets that contain lots of calculations, but no macros. On some pc’s Excel acts normally, on others you get the error. I have a couple of screen shots available. Any help is appreciated. If desired, send your file to my address below. I will only look if: 1. You send a copy of this message on an inserted sheet 2. You give me the newsgroup and the subject line 3. You send a clear explanation of what you want 4. You send before/after examples and expected results. -- Don Gu...

Uninstall of mappoint has caused errors with excel
Hi, I am running Office 2003 on the terminal server (windows 2003) and had a copy of mappoint as well. This is a mapping program. We ininstalled mappoint which has caused an error message with Excel and other office products. The error says "Cd:\documents and settings\administrator.ocrdc1\application data\microsoft\addins c:\Program files\common files\microsoft shared\geography\mpoai9.dll is not a valid add-in." I then click OK and excel opens up and everything is fine. The problem is that we are using other programs as well such as Quickbooks that export to excel and t...

microsoft.public.access.conversion
...

Excel VBA
When you select the autofilter, the drop down list is created from the unique data in that column. Is there any way to customize this list, so the changes are permanent? For instance, I might have in one(1) column the following 4 row entries: red, blue, green (all are single cell entries) red, blue red blue, green I want my autofilter drop down list to read: red instead of red, blue, green blue red, blue green red blue, green (like normal) If I chose red from the drop down list...

how do I add error bars to a 3D chart in excel?
The help states you can only add error bars to data series in 2D area. Is there a way to add them to a 3D chart? Hi, I would not have thought so. Obviously as it is not a built-in option the only way would be a work around perhaps using dummy series. Unfortunately you can create 3d combination charts. Stick with the 2d view. Cheers Andy elahe wrote: > The help states you can only add error bars to data series in 2D area. Is > there a way to add them to a 3D chart? -- Andy Pope, Microsoft MVP - Excel http://www.andypope.info I checked, and error bars are not offered for 3D ch...

q Emailing Excel Sheet With Outlook
I am trying to send a table from Excel within the body of an Outlook (lastest versions) to be sent as a fax. Am able to do this, BUT when the document prints out on the fax machine the formating is off (too big for the sheet). I am flexible on changing the method I send it to the fax machine, however it must be sent to the fax via macro. Below is the code I am using. Any help would be greatly appreciated. David Public Sub DoIt() 'On Error GoTo Handler Dim EmailAddress(0 To 2) As String Dim Count As Integer Dim N As Integer Dim sRec1(0) As String Dim sRec2(0 To 1) As...

Problem with named range for a large spreadsheet in Excel 2007
Hello, I am trying to generate a large Excel spreadsheet apprx. 10000 rows and 40 columns. I am generating defined name section in the Workbook.xml part of the XLSM package. Here is a sample entry from that section <definedName name="_._44802_._0_._0_._0_._top_line" localSheetId="0" hidden="1">Sheet1!$B$2</definedName> Although the generation goes fine, I can not open the spreadsheet as the Excel throws an error message saying the package is corrupt. But this is not the case if the spreadsheet is small say, 200 rows by 10 columns. ...

Access to User Calendar
I have a user called small conference room that is used to schedule meetings on its calendar. I would like to link the calendar from our intranet site to the calendar with a UNC path. I am calling outlook: and I can get to my local mailbox and public folders but I am unable to connect to another users calendar. I am running Exchange 2003 and Outlook 2003. Is there some security modifications that need to be done? Any help is appreciated. Thanks, Steve I believe that you will need full mailbox rights. -- Ed Crowley MVP - Exchange "Protecting the world from PSTs and brick backups!&...

I need to print a word in PDF from excel.
Hi, In my excel Macro, I update several values and then open a word linked with that excel. Finally what I need to do is to print that word to PDF. I already have a PDF Printer, so I just need the code to do it. Can anybody help me? Thanks in advance. Gast=F3n. Habilita la impresora predeterminada como la PDF y ya lo puedes imprimir a al formato deseado Gracias Francisco,=20 Tienes alg=FAn codigo para ello? gracias Repitiendo: Desde el panel de impresoras asigno primero la que me interesa y despues ejecuto mi macro desde el archivo excel asignando la instruccion sig: midefaultes = a...

Load image in a unbound control from a attachment field in recor
I have a unbound (single not continuious) form with 16 differant records from the same recordset. No problem loading the this data from recordset in VBA. PROBLEM I need to know how to load the unbound controls with Image's from an attachment field in another recordset The normal method of control = Rs!field does not work Please advise -- Thanks Tom dans l'article 78DC5502-3A76-4562-AA20-736446AB1448@microsoft.com, Tom � Tom@discussions.microsoft.com a �crit le 21/01/08 20:28�: > I have a unbound (single not continuious) form with 16 differant records from > the same record...