![]() |
KnowBrainer Speech Recognition | ![]() |
Topic Title: Excel command to auto-fit columns not working - invalid flags Topic Summary: Created On: 02/11/2022 11:06 AM Status: Post and Reply |
|
![]() |
![]() |
- wristofdoom | - 02/11/2022 11:06 AM |
![]() |
![]() |
- Alan Cantor | - 02/11/2022 11:45 AM |
![]() |
|
I created a command for Microsoft Excel (most recent version, Microsoft 365) based on a macro recording. The command is select all cells (clicking the triangle in the upper left corner), and then apply "auto fit column" ( Home > Format > auto fit column width ) In Excel, the macro generated by performing these actions with the mouse is:
Seems straightforward enough. I put this into a command called "auto fit column":
I added the Excel document object library by pressing ALT+ENTER. But when I execute the command, I get the following error message from Dragon: "The macro contains the error:
Any ideas?
------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
Try this:
Sub Main Dim objExcel As Excel.Application Set objExcel = GetObject(, "Excel.Application") objExcel.Selection.Columns.AutoFit Set objExcel = Nothing End Sub Its main limitation is that that the width of the column is determined by the width of the focused cell (or the the range of selected cells). A quick way to deal with this limitation is to select the entire column that you want auto-fitted. Alternatively, you could send keystrokes to auto-fit a column. No need to set a Reference: SendKeys "{F10}", True SendKeys "h", True SendKeys "o", True SendKeys "i", True The following version selects the entire column, changes the width of the column, and then (tries) to restore the cursor to the starting location: Sub Main SendSystemKeys "{Ctrl+space}" SendKeys "{F10}", True SendKeys "h", True SendKeys "o", True SendKeys "i", True SendSystemKeys "{Up}{Down}" End Sub |
|
|
FuseTalk Standard Edition v4.0 - © 1999-2023 FuseTalk™ Inc. All rights reserved.