Excel formatting

Excel formatting

chendersonCRKPU
Contributor Contributor
1,951 Views
5 Replies
Message 1 of 6

Excel formatting

chendersonCRKPU
Contributor
Contributor

I'd like to tidy up an excel worksheet using the API.

 

So far I've only used AutoFit on the column widths, but I'd like to center align certain columns, apply a border around the full range of used cells, bold the headings and apply a filter to the first row.

 

Can someone point me to where I can find the API for these things? Or a similar example

 

This is what I've got so far:

 

excelApp = CreateObject("Excel.Application")
excelApp.Visible = True
excelApp.DisplayAlerts = False

wb = excelApp.Workbooks.Open(oPATH + "BOM-PartsOnly.xlsx")
xlws = wb.Worksheets(1)

excelApp.Columns.AutoFit
excelApp = Nothing 

 

0 Likes
Accepted solutions (1)
1,952 Views
5 Replies
Replies (5)
Message 2 of 6

bradeneuropeArthur
Mentor
Mentor
Use excel macros to read what the coding is for your options. I mean record your macro in excel and copy this to your code.

You understand?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 6

MechMachineMan
Advisor
Advisor

Yo check the links in my signature. MSDN is great.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 4 of 6

chendersonCRKPU
Contributor
Contributor

Thanks for your suggestions. I've tried both the record macro and the resources in your links. Both produced something which looks like it should work but I keep receiving this error on the red line below:

 

Unable to set the HorizontalAlignment property of the Range class

 

wb = excelApp.Workbooks.Open(oPATH + "BOM-PartsOnly.xlsx")
xlws = wb.Worksheets(1)

Call ReorderXLBOM()

excelApp.Columns.AutoFit
excelApp.Rows("1:1").Select
excelApp.Selection.Font.Bold = True
excelApp.Selection.AutoFilter

xlws.Columns("A:A").HorizontalAlignment = xlCenter

 

I have also tried this (as per recorded excel macro) instead of that line but get the same error:

excelApp.Columns("A:A").Select
With excelApp.Selection
	.HorizontalAlignment = xlCenter
End With

 

I'm running this from iLogic environment directly after using BOMView.Export

0 Likes
Message 5 of 6

MechMachineMan
Advisor
Advisor

Google: "horizontalalignment enum" 

 

Why? You are setting a property using text - ergo, using an enum.

 

What the results of google returns:

 

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlhalign-enumeration-excel

 

What this means for your code:

 

xlws.Columns("A:A").HorizontalAlignment = xlHAlignCenter

OR

xlws.Columns("A:A").HorizontalAlignment = xlHAlign.xlHAlignCenter

OR

xlws.Columns("A:A").HorizontalAlignment = -4108


If none of those lines work, either:

1. I found the wrong enum

2. Some spelling is wrong in the line

3. One of the members is not valid for the object it's called on, or

4. The Columns range is not a valid object to run the horizontalalignment command on. 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 6 of 6

chendersonCRKPU
Contributor
Contributor
Accepted solution
Imports Microsoft.Office.Interop.Excel.Constants 

 Adding this statement solved it.

 

I only had

Imports Microsoft.Office.Interop.Excel