Solved

How to unWrap when exporting in VBA Excel

Posted on 2016-08-27
16
55 Views
Last Modified: 2016-08-28
We have a VBA routine that exports email but it keeps exporting it with "wrap" setup.  How can we include a code when exporting or "writing" to an excel file (current code lines used below):
0
Comment
Question by:rayluvs
  • 9
  • 5
  • 2
16 Comments
 

Author Comment

by:rayluvs
ID: 41773303
Sorry about that, here's the code section we use to write out the data:

For Each obj In Selection
    Set olItem = obj
    strColB = olItem.SenderEmailAddress
    strColC = olItem.To
    strColD = olItem.Subject
    strColE = olItem.ReceivedTime
    strColF = olItem.Body
    xlSheet.Range("B" & rCount) = strColB
    xlSheet.Range("c" & rCount) = strColC
    xlSheet.Range("d" & rCount) = strColD
    xlSheet.Range("e" & rCount) = strColE
    xlSheet.Range("f" & rCount) = strColF
    rCount = rCount + 1
    Next

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41773305
What do you mean by  "wrap" setup?
0
 

Author Comment

by:rayluvs
ID: 41773308
Hi!

We mean by "wrap" is when a cell content is longer than the cell it would "wrap" the content thus making the cell open up and display its entire content.

The problem we have is that when running the routine it always "wrap" all columns and we want to to be written "unwrap".
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41773314
You will have to set the width manually.
You can do so by adding this code after you dump your data into Excel:
Range("B1:F1").ColumnWidth = Range("XFD1").ColumnWidth

Open in new window

0
 

Author Comment

by:rayluvs
ID: 41773316
gave me an error...

olvberr.JPG
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41773317
Try:
xlSheet.Range("B1:F1").ColumnWidth = Range("XFD1").ColumnWidth

Open in new window

0
 

Author Comment

by:rayluvs
ID: 41773322
nope
0
 

Author Comment

by:rayluvs
ID: 41773323
same err
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:MacroShadow
ID: 41773324
Can you show me the entire code?
Which version of Excel are you using?
0
 

Author Comment

by:rayluvs
ID: 41773326
excel 2010
0
 

Author Comment

by:rayluvs
ID: 41773327
here it is":
0
 

Author Comment

by:rayluvs
ID: 41773328
code:

Sub aaaCopyToExcel()
  Dim xlApp As Object
  Dim xlWB As Object
  Dim xlSheet As Object
  Dim rCount As Long
  Dim bXStarted As Boolean
  Dim enviro As String
  Dim strPath As String
  Dim currentExplorer As Explorer
  Dim Selection As Selection
  Dim olItem As Outlook.MailItem
  Dim obj As Object
  Dim strColB, strColC, strColD, strColE, strColF As String
 'Start
  xVar = 0
 'Get Excel set up
  enviro = CStr(Environ("USERPROFILE"))
 'the path of the workbook
  strPath = enviro & "\Documents\test.xlsx"
  On Error Resume Next
  Set xlApp = GetObject(, "Excel.Application")
  If Err <> 0 Then
     Application.StatusBar = "Please wait while Excel source is opened ... "
     Set xlApp = CreateObject("Excel.Application")
     bXStarted = True
     End If
  On Error GoTo 0
 
 'Open the workbook to input the data
  Set xlWB = xlApp.Workbooks.Open(strPath)
  Set xlSheet = xlWB.Sheets("Sheet1")
 
 'Process the message record
  On Error Resume Next
 
 'Find the next empty line of the worksheet
  rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
 'needed for Exchange 2016. Remove if causing blank lines.
  rCount = rCount + 1

 'Get the values from outlook
  Set currentExplorer = Application.ActiveExplorer
  Set Selection = currentExplorer.Selection
  For Each obj In Selection
      Set olItem = obj
     'Get Outlook fields
      strColB = olItem.SenderEmailAddress
      strColC = olItem.To
      strColD = olItem.Subject
      strColE = olItem.ReceivedTime
      strColF = olItem.Body
     'Write Outlook field to excel
      xlSheet.Range("B" & rCount) = strColB 'SenderEmailAddress
      xlSheet.Range("c" & rCount) = strColC 'To
      xlSheet.Range("d" & rCount) = strColD 'Subject
      xlSheet.Range("e" & rCount) = strColE 'ReceivedTime
      xlSheet.Range("f" & rCount) = strColF 'Body
      xVar = xVar + 1
      Next
  
  xlWB.Close 1
  If bXStarted Then
     xlApp.Quit
     End If
  
  Set olItem = Nothing
  Set obj = Nothing
  Set currentExplorer = Nothing
  Set xlApp = Nothing
  Set xlWB = Nothing
  Set xlSheet = Nothing
 End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41773330
Here:
Sub aaaCopyToExcel()
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlSheet As Object
    Dim rCount As Long
    Dim bXStarted As Boolean
    Dim enviro As String
    Dim strPath As String
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    Dim olItem As Outlook.MailItem
    Dim obj As Object
    Dim strColB, strColC, strColD, strColE, strColF As String
    'Start
    xVar = 0
    'Get Excel set up
    enviro = CStr(Environ("USERPROFILE"))
    'the path of the workbook
    strPath = enviro & "\Documents\test.xlsx"
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err <> 0 Then
        Application.StatusBar = "Please wait while Excel source is opened ... "
        Set xlApp = CreateObject("Excel.Application")
        bXStarted = True
    End If
    On Error GoTo 0

    'Open the workbook to input the data
    Set xlWB = xlApp.Workbooks.Open(strPath)
    Set xlSheet = xlWB.Sheets("Sheet1")

    'Process the message record
    On Error Resume Next

    'Find the next empty line of the worksheet
    rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
    'needed for Exchange 2016. Remove if causing blank lines.
    rCount = rCount + 1

    'Get the values from outlook
    Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection
    For Each obj In Selection
        Set olItem = obj
        'Get Outlook fields
        strColB = olItem.SenderEmailAddress
        strColC = olItem.To
        strColD = olItem.Subject
        strColE = olItem.ReceivedTime
        strColF = olItem.Body
        'Write Outlook field to excel
        xlSheet.Range("B" & rCount) = strColB    'SenderEmailAddress
        xlSheet.Range("c" & rCount) = strColC    'To
        xlSheet.Range("d" & rCount) = strColD    'Subject
        xlSheet.Range("e" & rCount) = strColE    'ReceivedTime
        xlSheet.Range("f" & rCount) = strColF    'Body
        xVar = xVar + 1
    Next

    xlSheet.Range("B1:F1").ColumnWidth = Range("ABC1").ColumnWidth

    xlWB.Close 1
    If bXStarted Then
        xlApp.Quit
    End If

    Set olItem = Nothing
    Set obj = Nothing
    Set currentExplorer = Nothing
    Set xlApp = Nothing
    Set xlWB = Nothing
    Set xlSheet = Nothing
End Sub

Open in new window

0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41773453
Didn't go through all the posts but after reading the question, won't the following line only do the job?
xlSheet.Cells.WrapText = False
xlSheet.Columns.AutoFit

Open in new window

0
 

Author Comment

by:rayluvs
ID: 41773608
Hi Macro, kept getting the error...

Subodh, worked perfectly!!!!  Thanx!!!
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41773611
You're welcome. Glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now