Changing Worksheets in Excel Automation

jversusj
Member Posts: 489
Good day.
I've been reading Excel automation threads most of this AM looking for a solution to my issue. I have a report with 8 data items (7 are nested under 1 parent, some of these are again nested under others). I have been asked to output this to excel with different sheets for most of the different data items.
I have been successful in adding sheets and writing relevant data to each, the problem is that when the next record in the parent data item is retrieved, I need to switch back to the proper sheet that was created on the first iteration of the data item. Thus far, I have been unsuccessful, such that after the first parent record is output to all sheets, all subsequent parent record output is to the most recent sheet used.
I have tried several different syntax - some threw automation errors, others appear to do nothing (no error, but no result). I'm sure someone here has done this and I would greatly appreciate any suggestions. Thanks!
Some of the commands I have attempted:
I've been reading Excel automation threads most of this AM looking for a solution to my issue. I have a report with 8 data items (7 are nested under 1 parent, some of these are again nested under others). I have been asked to output this to excel with different sheets for most of the different data items.
I have been successful in adding sheets and writing relevant data to each, the problem is that when the next record in the parent data item is retrieved, I need to switch back to the proper sheet that was created on the first iteration of the data item. Thus far, I have been unsuccessful, such that after the first parent record is output to all sheets, all subsequent parent record output is to the most recent sheet used.
I have tried several different syntax - some threw automation errors, others appear to do nothing (no error, but no result). I'm sure someone here has done this and I would greatly appreciate any suggestions. Thanks!
Some of the commands I have attempted:
XlApp.ActiveWorkbook.Sheets.Select('Sheet Name'); //no errors, but no change in output XlSheet := XlBook.Worksheets.Item('Sheet Name'); //no errors, but no change in output XlSheet.Activate;
kind of fell into this...
0
Answers
-
I have created a function before to Activate a sheet passing it a text variable for a sheet name and with a boolean return value. Basically if the sheet is not found then I create it. If it is found the sheet name you sent is active:
Found := FALSE; i := 1; EndOfLoop := XlBook.Worksheets.Count; WHILE (i <= EndOfLoop) AND (NOT Found) DO BEGIN XlSheet := XlBook.Worksheets.Item(i); IF Sheetname = XlSheet.Name THEN BEGIN Found := TRUE; END; i += 1; END; EXIT(Found);
Gerry Kistler
KCP Consultores0 -
thanks - i'm not having trouble creating the sheets, just switching between them. I will experiment with using an integer to change sheets instead of a string.kind of fell into this...0
-
The code in the function I passed looks for the sheet and if it is found it becomes active. Now one way you could do this is keep track of the sheet number along with the name as you are creating them, then you will not need to loop through the sheets as my example does, you can just use this one line where i is the number of the sheet you want to go back to:
XlSheet := XlBook.Worksheets.Item(i);
Gerry Kistler
KCP Consultores0 -
gerry - working with your idea I realized i made a rookie mistake and had my code in the wrong trigger on the parent dataitem, so of course it was never working! :oops:
i made some changes and it looks like i'm on the right track, thanks again!kind of fell into this...0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions