Click here to Skip to main content
12,886,892 members (34,543 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
How do I wrap and align text in an excel work sheet using
Posted 13-Jul-10 3:14am
Updated 13-Jul-10 3:37am
Johnny J.164.5K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is one i made earlier, for a similiar question asked in the VB forums only a few weeks ago;

    Private Sub doSomething()
        Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
        Dim xlWk As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add()
        Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = xlWk.Worksheets(1)
        xlApp.Visible = True
        xlSheet.Range("A1").Value = "Some Text Value on 1 line"
        xlSheet.Range("A5").Value = "Some text value on" + vbCrLf + "2 lines"
        xlSheet.Range("B10").Value = "Some text aligned to the center"
        xlSheet.Range("B10").ColumnWidth = 120
        xlSheet.Range("B10").CurrentRegion.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter
    End Sub

NOTE: there are other HorizontalAlignment constants available in the object model,e.g. HorizontalAlignment.Left (.Right) (.Center) but these do not work, you must use the one shown above.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

xlBottom = -4107
xlCenter = -4108
xlJustify = -4130
xlLeft = -4131
xlRight = -4152
xlTop = -4160

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 31 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100