AutoHotkey Community

It is currently May 27th, 2012, 5:06 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 296 posts ]  Go to page Previous  1 ... 13, 14, 15, 16, 17, 18, 19, 20  Next

Should this be continued?
Poll ended at June 18th, 2008, 11:34 pm
Yes 93%  93%  [ 14 ]
No 7%  7%  [ 1 ]
Total votes : 15
Author Message
 Post subject:
PostPosted: April 2nd, 2010, 4:09 am 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
Ok, so I did some more testing which resulted in new and even more confusing errors. I will try to be as complete as I can. I tried two different methods of calling the save function. The first was the method I have already posted, this is the complete code I was using during testing (hotkey was random, for testing):

Code:
^r::
   COM_Init()
   oExcel := COM_GetActiveObject("Excel.Application")
   COM_Invoke(oExcel, "Save")
   COM_Release(oExcel)
   COM_Term()
   return


This code was working earlier, though I don't seem to be able to recreate those conditions. Now every time I run it I get this error (Copied directly from the msgbox):
Code:
---------------------------
COM Error Notification
---------------------------
Function Name:   "Save"
ERROR:      (0x800A03EC)
PROG:   Microsoft Office Excel
DESC:   Unable to get the Save property of the Application class
HELP:   C:\Program Files\Microsoft Office\Office12\1033\XLMAIN11.CHM,0

Will Continue?
---------------------------
Yes   No   
---------------------------


After fighting with that for a while, I took the code you posted to get the pwb, and tried using that.

Code:
^t::
   COM_Init()
   oExcel := GetExcel()
   oWorkbook := COM_Invoke(oExcel, "ActiveWorkbook")   
   COM_Invoke(oExcel, "Save")
   COM_Release(oExcel)
   COM_Term()
   return

GetExcel(hWnd = 0)
{
   DetectHiddenWindows, On
   If Not   hWnd
   WinGet, hWnd, ID, ahk_class XLMAIN
   ControlGet, hWnd, hWnd,, EXCEL71, ahk_id %hWnd%
   If   hWnd
   Return   GetNativeOM(hWnd)
}

GetNativeOM(hWnd)
{
   Static   pfn,iid
   If Not   pfn
      pfn := DllCall("GetProcAddress", "Uint", DllCall("LoadLibrary", "str", "oleacc"), "str", "AccessibleObjectFromWindow"), COM_GUID4String(iid, "{00020400-0000-0000-C000-000000000046}")
   If   DllCall(pfn,"Uint",hWnd,"Uint",-16,"Uint",&iid,"UintP",pwin)=0
      papp := COM_Invoke(pwin,"Application"), COM_Release(pwin)
   Return   papp
   }


The first time I run this, it saves the spreadsheet as well creates a new file, named RESUME.XLW, in my documents that appears to be a copy of the current spreadsheet. If this file is not deleted before running the code again, I get the error I posted last time. I don't really understand why.

You said to compare the previous and the current call. I noticed that the line I first posted (when responding to tank) uses pxlb, since I copied it from a block of code on page 1, where the pointer was created from this:

Code:
pweb := COM_AtlAxCreateControl(WinExist(), A_Temp . "\test.xls")
pxlb := COM_Invoke(pweb, "Document")


I take this to mean that the save method I am trying to call will only work properly when using the COM_AtlAxCreateControl instance, not either of the methods I was testing with. If thats the case, is there a way to call the save method using a pointer collected from either of the methods I used above?

I know this post is long, but I wanted give you what I had done. I know I am asking what probably seem like basic questions from someone who just wants a quick solution, but I really am trying to learn this, and I am grateful to all the help I've gotten on these forums (especially from the work you and tank have done).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2010, 4:50 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Why aren't you using your created pointer?

Code:
^t::
   COM_Init()
   oExcel := GetExcel()
   oWorkbook := COM_Invoke(oExcel, "ActiveWorkbook")   
   COM_Invoke(oWorkbook, "Save")
   COM_Release(oWorkbook), COM_Release(oExcel)
   COM_Term()
   return

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2010, 7:42 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
I'm always puzzled how/why users are missing the obvious resources to learn from. As you seem to use Office 2007, the definite place you have to visit is 2007 Microsoft Office System.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2010, 3:40 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
in fairness Sean you must admit it is only recently that the MSDN has been so much more friendly. I remember a time (not but a few years ago) that much of it was like jibberish to all but the more skilled developer.

the very latest version of the msdn tho is very user friednly

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2010, 8:39 pm 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
sinkfaze wrote:
Why aren't you using your created pointer?


I thought I had tried that earlier with the same result, but going back it seems to be working now.Thats the pointer that the example I was borrowing from uses, too, I don't know how I skipped it. I certainly feel foolish for spending so much time with the answer right in front of me. I guess that makes sense though, the workbook being the one with the save method and not the application. Its the workbook thats being saved.

@Sean, thank you for the link, I will definitely be spending time there this week.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2010, 9:06 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Tyrsius wrote:
I certainly feel foolish for spending so much time with the answer right in front of me.


Welcome to most of our lives when we started using COM. :wink:

By the way, there's also a CHM reference for Excel 2003 that you could download and read in addition to the website Sean referenced above. The core of what is required to access Excel hasn't changed much between the two versions so it won't hurt.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 7th, 2010, 3:03 am 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
In the Excel.ahk code posted on the front page, the author has Excel_SetText accept Column references as letters. I was trying to use it in a loop, and instead of trying to get the next letter, I found it easier to modify the function to just accepts numbers for the column reference (in addition to letters).

Code:
; **************************************************************************
; Author:   NKRUZAN
; Language:   AutoHotkey v1.0.47.06
; Creation Date:   05/19/2008   16:47
; Function Name:   Excel_SetText()
;
; Syntax:
;    Excel_SetText(ColumnLetter, RowNumber, Text)
; Parameters:
; 1)    ColumnID   =    The column letter/number to put the text in
; 2) RowNumber   =    The row Number to put the text in
; 3) Text       =    The text to put in the cell
; Return:
;    Success = nothing
;    Failure = nothing
; **************************************************************************
Excel_SetText(ColumnID, RowNumber, Text){
   if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
      MsgBox Could not find Excel Instance.
      Return
      }
   ;Get the cell range
   if !RegExMatch(ColumnID, "\d+") ;if not a number, get the Index from the letter - Tyrsius
      ColumnID := Excel_GetColumnIndex(ColumnID)
   if !oCells := COM_Invoke(oExcel,"Cells",RowNumber,ColumnID) {
      MsgBox Could not get cell range.
      Return
      }
   COM_Invoke(oCells,"Formula=",Text) ;"
   COM_Release(oCells)
   COM_Release(oExcel)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2010, 5:54 pm 
Offline

Joined: July 9th, 2009, 1:13 am
Posts: 140
Sean, thank you so much for that link. I have been using it quite a lot, and I've learned how to translate from at least a dozen methods into COM for ahk.

That is one of the single largest time savers ever. :D

Code:
COM_Invoke(oWorkbook, "ActiveSheet.PivotTables['Unaccepted Issues'].PivotCache.Refresh")

For the curious, this was the hardest to translate, as the MSDN lists the method as "RefreshTable" which does not work. I took this command from Excel's macro recorder, which did work.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2010, 5:46 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
Tyrsius wrote:
For the curious, this was the hardest to translate, as the MSDN lists the method as "RefreshTable" which does not work. I took this command from Excel's macro recorder, which did work.
RefreshTable is a function of the interface PivotTable, not that of PivotCache which you're using.

BTW, AutoHotkey_COM has a built-in function to show the type of the object:
Code:
obj := ComObjActive("Excel.Application").ActiveWorkbook.ActiveSheet.PivotTables("Unaccepted Issues").PivotCache
MsgBox % ComObjType(obj, "name")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2010, 3:25 pm 
Offline

Joined: May 11th, 2009, 6:13 am
Posts: 28
so this post means i don't need to learn MS VBA for excel?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2010, 3:29 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
if anything you need more to learn it since it will carry over and Sean or any one else for that matter isnt likely to document an objects methods properties and child objects

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2010, 9:42 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Here's a function for the Excel collection, it will save and close an Excel document. You can optionally specify the title of the document, if no title is specified the "active" instance will be saved/closed.

AHK:

Code:
Excel_Close(title="") {

   f :=   "is)Microsoft Excel - ", t :=   "is)\.xlsx?.*$"   
   if   !xl :=   COM_GetActiveObject("Excel.Application") {
      MsgBox, 48, Excel_Close, Could not obtain a reference to Excel.
      return   False
   }
   title :=   !title ? COM_Invoke(xl,"ActiveWorkbook.Name")
    : RegExReplace(RegExReplace(title,t),f)
   Loop %   COM_Invoke(xl,"Workbooks.Count")
      if   InStr(COM_Invoke(xl,"Workbooks[" A_Index "].Name"),title) {
         COM_Invoke(xl,"Workbooks[" A_Index "].Save")
         COM_Invoke(xl,"Workbooks[" A_Index "].Close")
         break
      }
   COM_Release(xl)
   return   True
}


AHK_L:

Code:
Excel_Close(title="") {

   f :=   "is)Microsoft Excel - ", t :=   "is)\.xlsx?.*$"   
   if   !xl :=   COM_GetActiveObject("Excel.Application") {
      MsgBox, 48, Excel_Close, Could not obtain a reference to Excel.
      return   False
   }
   title :=   !title ? xl.ActiveWorkbook.Name
    : RegExReplace(RegExReplace(title,t),f)
   Loop %   xl.Workbooks.Count
      if   InStr(xl.Workbooks[A_Index].Name,title) {
         xl.Workbooks[A_Index].Save, xl.Workbooks[A_Index].Close
         break
      }
   xl :=   ""
   return   True
}

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 5th, 2010, 11:06 am 
Offline

Joined: May 11th, 2009, 6:13 am
Posts: 28
i think it should be more functions for excel as operations on chart. after all i can use formula of excel to get/calculate data.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 5th, 2010, 3:11 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
zhuluobin wrote:
i think it should be more functions for excel as operations on chart. after all i can use formula of excel to get/calculate data.


If you have such functions you are more than welcome to contribute them. The people who have been and are contributing to this thread are doing so because the functions here are the ones they find the most use for. I rarely have a need to create charts so writing functions for something I would barely use doesn't make much sense.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 5th, 2010, 3:21 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
Dare I say ( especially with AHKL & COM_L ) that it's almost as easy to learn some basic VBScript/COM & use the Excel Object Model as it is to learn how to use a bunch of user created functions?

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 296 posts ]  Go to page Previous  1 ... 13, 14, 15, 16, 17, 18, 19, 20  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Stigg and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group