Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Set the Width of an Excel Column
Message
De
04/09/2003 09:34:20
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
02/09/2003 18:20:58
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Divers
Thread ID:
00825508
Message ID:
00826000
Vues:
12
>My mind has gone blank. How can I set the width of an Excel Column through Automation?

Width is in points and readonly. ColumnWidth is in characters and read/write. If you know the width in chars you could directly set. If you want to do it based on pixels then you need some math. ie:
#Define TWIPSPERINCH  1440
#Define TWIPSPERPOINT 20
#Define POINTSPERINCH (TWIPSPERINCH / TWIPSPERPOINT)
#Define WU_LOGPIXELSX  88
#Define WU_LOGPIXELSY 90

Declare Integer ReleaseDC In Win32Api;
  INTEGER nwnd, Integer hdc
Declare Integer GetDeviceCaps In Win32API;
  INTEGER hdc, Integer nIndex
Declare Integer GetWindowDC In Win32API;
  INTEGER HWnd

oExcel = Createobject('Excel.Application')
With oExcel
  .Visible = .T.
  .Workbooks.Add
  With .ActiveSheet
* Set columns B:E width to 220 pixels
    .Range('B:E').ColumnWidth = GetColumnWidth(oExcel,220)
* Set all rows' height to 100 pixels    
    .Cells.RowHeight = Pixels2Points(100)
  Endwith
Endwith


Function Pixels2Points
Lparameters tnPixels, tlVertical
Return ( tnPixels  / ;
  GetPixelsPerInch(tlVertical) * POINTSPERINCH )

Function Points2Pixels
Lparameters tnPoints, tlVertical
Return ( tnPoints  * ;
  GetPixelsPerInch(tlVertical) / POINTSPERINCH )

Function GetPixelsPerInch
Lparameters tlVertical
Local lnDC, lnPixelsPerInch
lnDC = GetWindowDC(0)
lnPixelsPerInch = GetDeviceCaps(lnDC, ;
  Iif(tlVertical, WU_LOGPIXELSY, WU_LOGPIXELSX))
ReleaseDC(0, lnDC)
Return lnPixelsPerInch

Function GetColumnWidth
Lparameters toExcel, tnPixels
Local lnSign, lcOperator, lnWSet, lnSaveWidth
lnWSet = Pixels2Points(tnPixels)
With toExcel.ActiveWorkbook.ActiveSheet.Range('A1')
  lnSaveWidth = .ColumnWidth
  .ColumnWidth = 100
  .ColumnWidth = .ColumnWidth * m.lnWSet / .Width
  lnSign = Iif(.Width > m.lnWSet, -1, 1)
  lcOperator = Iif(.Width > m.lnWSet, '>', '<')

  Do While .Width &lcOperator lnWSet
    .ColumnWidth = .ColumnWidth + 0.1 * m.lnSign
  Enddo
  lnRetVal = .ColumnWidth
  .ColumnWidth = lnSaveWidth
Endwith
Return lnRetVal
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform