CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Output to Excel

  1. #1
    Join Date
    Mar 2005
    Posts
    141

    Output to Excel

    I am trying to send groups of strings to Excel, and I am having a tough time with the formatting. For some reason, when I attempt to assign color to any of the cells, the user gets prompted to save changes to the book. Why does this happen? How do I make changes to entire columns (horizontal alignment etc)? How do I set the 'category' for individual cells?

    Thanks for the help.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Output to Excel

    To prevent the Message Asking if you want to save to display, Save your worksheet first, then close
    Code:
    XlBook.save
    XlBook.close(false)
    Just remember as well to set all the object references to nothing!!

    For example:
    Code:
    xlsheet=nothing
    XlBook=nothing
    Xl.quit
    Xl=nothing
    What do you mean by Category ¿

  3. #3
    Join Date
    Nov 2004
    Location
    LA. California Raiders #1 AKA: Gangsta Yoda™
    Posts
    616

    Re: Output to Excel

    If you make any changes to your workbook such as formatting, you will be prompted to save your workbook.

    Category, do you mean a Named Range?
    VB/Office Guru™ (AKA: Gangsta Yoda™)
    VB Forums - Super Moderator 2001-Present

    Microsoft MVP 2006-2011

    Please use [code]your code goes in here[/code] tags when posting code.

    Senior Software Engineer MCP, BSEE, CET
    VS 2012 Premium, VS 6.0 Enterprise SP6, VSTO, Office Ultimate 2010, Windows 7 Ultimate
    Star Wars Gangsta Rap SE Reputations & Rating Posts Office Primary Interop AssembliesAdvanced VB/Office Guru™ Word SpellChecker™.NETAdvanced VB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address ListVB6/Crystal Report Ex.VB6/CR Print Setup Dialog Ex.

  4. #4
    Join Date
    Mar 2005
    Posts
    141

    Re: Output to Excel

    By Category, I was referring to the type of field that was assigned to the cell (general, number, currency etc). I was thinking that this would be an easy way to format individual cells.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Output to Excel

    For the 'Category' have a look at this:

    http://www.codeguru.com/forum/showpo...34&postcount=2

    Note the NumberFormat property!

  6. #6
    Join Date
    Mar 2005
    Posts
    141

    Re: Output to Excel

    Do all formatting changes force you to save the workbook? Even changes made specfically by referencing the sheet, and not the book? When I do make formatting changes, and then save the book when prompted to, none of the data appears in the book. When I remove the formatting changes, everything works properly. Do I somehow have to link the sheet to the book, aside from:
    Code:
    xlBook = xlApp.Workbooks.Add
    xlSheet = xlBook.Worksheets.Item("Sheet1")
    Is the code to work with excel in VB .NET the same as regular vba code?

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Output to Excel

    Quote Originally Posted by Broodmdh
    Do all formatting changes force you to save the workbook? Even changes made specfically by referencing the sheet, and not the book?
    Yes, becuase the Sheet is part of the book.

    Quote Originally Posted by Broodmdh
    When I do make formatting changes, and then save the book when prompted to, none of the data appears in the book. When I remove the formatting changes, everything works properly.
    Can you include your full code ¿

    Quote Originally Posted by Broodmdh
    Do I somehow have to link the sheet to the book, aside from:
    Code:
    xlBook = xlApp.Workbooks.Add
    xlSheet = xlBook.Worksheets.Item("Sheet1")
    No, as I've said the Sheet is already part of the workbook

  8. #8
    Join Date
    Dec 2002
    Posts
    305

    Re: Output to Excel

    I have been working with Excel outputs from .Net for last 2-3 years and never had the problem of being prompted to save the workbook when I am changing formats within a sheet. My suggestion is first check if this happens when you are working in Excel directly. If it does, it is some default setting within the Excel loaded on your PC that is causing it. If it does not, I have no clue. Good luck.

  9. #9
    Join Date
    Dec 2002
    Posts
    305

    Re: Output to Excel

    Originally Posted by Broodmdh
    When I do make formatting changes, and then save the book when prompted to, none of the data appears in the book. When I remove the formatting changes, everything works properly.
    Add XlApp.visible=true so that you can view the sheet while execution is on.

    Remove the XlBook.save or any other save statements. When your Excel formatting is complete, save it directly as the book is open and visible to you.

    This way you can track what is happening. Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured