Basement

_Excel_RangeWrite with line break

8 posts in this topic

Hi,

 

im writing from an array to an excel sheet and want to use line breaks.

 

The following piece of code does not do this job (instead of the line break the "[...]chr(10)[...]" is displayed in the cell:

 

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

; Create application object and create a new workbook
Local $oAppl = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookNew($oAppl)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example", "Error creating the new workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oAppl)
    Exit
EndIf

; *****************************************************************************
; Write a 1D array to the active sheet in the active workbook
; *****************************************************************************
Local $aArray1D[3] = ['"Zeile 1 " & vbLf & " Zeile 2"', 'B', 'CC']
_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aArray1D, "A3")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 2", "Error writing to worksheet." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 2", "1D array successfully written.")

What am i doing wrong?


Any help is appreciated.

 

Best regards

 

Daniel

Share this post


Link to post
Share on other sites



The help file explains how to do ;) 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
1 hour ago, water said:

 

Hi,

sorry, i asked to quick ;-)

When i use  @CRLF in my previous example the line break works fine.

But my Array-Input comes from a csv file.

In the CSV-File i want to define the line breaks.

After reading it to an array (and processing it) the line breaks in the output Excel file should be overtaken.

But this is what i get (Test.csv=Input file, Mappe8.csv= output file)

Array.jpg

Now unfortunately the help file is not helping me ;-(

Do you have an idea?

 

Best regards

 

Daniel

Share this post


Link to post
Share on other sites

If I understand you correctly you get a line like "Line 1 @CRLF & Line 2" from the CSV.
To have it displayed in two lines in Excel I would use the following to translate the string into a "real" @CRLF:

$sRealString = Execute("Line1 & @CRLF & Line 2")

Then write $sRealString to Excel.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi.

 

i have tried your example with the following 4 versions:

Version 1: Line 1 & @CRLF & Line 2
Version 2: "Line 1" & @CRLF & "Line 2"
Version 3: Line 1' & @CRLF & 'Line 2'
Version 4: "Line 1 & @CRLF & Line 2"

Version 1 and 3 result in an empty string ;-(

 

Here's my code:

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>


; Create application object and create a new workbook
Local $oAppl = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookNew($oAppl)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example", "Error creating the new workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oAppl)
    Exit
EndIf

; *****************************************************************************
; Write a 1D array to the active sheet in the active workbook
; *****************************************************************************
Local $aArray
_FileReadToArray("Test.csv", $aArray);
_ArrayDisplay($aArray);
For $y=1 to UBound($aArray)-1 Step +1
   MsgBox(0, "Line prior to conversion", $aArray[$y])
    $sRealString= Execute($aArray[$y])
    MsgBox(0, "Line after conversion", $sRealString)
   _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $sRealString, "A"&$y)
Next
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 2", "Error writing to worksheet." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 2", "1D array successfully written.")

Seems that i'm too blind to find the solution this day...maybe tommorrow will be better ;-)

Share this post


Link to post
Share on other sites

Or try:

$sRealString = StringReplace("Line1 & @CRLF & Line 2", " & @CRLF & ", @CRLF)

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanx!!!

This works perfectly for me.

You made my day ;-)

 

Best wishes

Daniel

Share this post


Link to post
Share on other sites

:) 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • water
      By water
      My computer has been upgraded from Office 2010 to Office 2016.
      Are there any features of Office 2013 or Office 2016 which you now want to see in the Excel, Word or Outlook UDF?
    • FrancescoDiMuro
      By FrancescoDiMuro
      Hi guys!
      First of all, I want to apologize with @Melba23, @JLogan3o13, and @Jos ( I don't know why the tag sometimes works and sometimes doesn't ), for my bad attitude I have had with
      them. I want to explain why I was so angry, but that's not an excuse to my bad attitude...
      I have health problem, and lastly, I am ever more angry because I'm fighting with these problems for 5 years and I feel worse than 5 years ago, even If I had a lot of investigations about with a lot of Doctors, and there's no cure for my pathology. I don't want to touch anyone with my "story", but I wanted to explain why, sometimes, you can feel my anger in my words.
      I promise I'll try to be as polite as my mom taught me, and to explain all I'd like to develop, posting all you need ( sample codes... ).

      Now, let's talk about my project.
      Let's start from 0! 
      I'd like to implement a database with AutoIt, for the warehouse management.
      First of all, I'd like to set some "goals" which I want to respect, developing my application:
      Efficiency: I'd like to develop an application that is fast as possible; Easy to make changes: I'd like to develop an application that is easy to modify, so, without too much complicated arguments; Noob-proof: As a noob as I am, my AutoIt knowledge starts from June 2016, so, I'm not a blade in this programming language. I studied C++, C, JavaScript, CSS, HTML and PHP, but, all of these programming language has a different approach comparing with AutoIt. I could be wrong in what I just estabilished! I want to tell you, again, that I'm here to listen to your suggestions, so, everything you say, it's something that I could learn, and I want to learn AutoIt, so, don't be scared to share your opinions, or your knowledge! I'm here to learn "How to AutoIt!"  Ok, so, after having set some goals I'd like to hit, I want to ask you some information:
      What's the best way to develop a database with AutoIt? With a .txt file? With an Excel workbook? 
      I was working on Excel workbook, but, I don't know, I could always be wrong in terms of efficiency and "easy to make changes"; I have to create a GUI, in order to let the user do things ( I think it was quite obvious ), so, I have been always used GUI functions like GUICreate and so on; anything about _GDI_Plus or something else. Most, I used _GUI* functions. What's the best way to manage different GUIs? OnEvent Mode or Message Loop "mode"? Now, I'd like to make a list of what I'd like my application would do:
      Reading stocks: I manage mainly 2 manufacturers, so, If I will work with an Excel workbook, I'd prefer to divide first manufacturer with the second; Add product: Simply, If a product does exist, the application prompt to the user an input box, with a sort of summary ( Product name, quantity ) of the product he's adding; ELSE, if the product does not exist, an "Add New Product" GUI should appers; Withdraw product: As a warehouse, I need to add and to withdraw product... This "function" should performs the simply task of reserach the product based on his ID and, prompt an InputBox to the user, with a little summary of the product he's withdrawing and, the quantity of the product after the withdraw; Search product: I'd like to develop a "product reserach", based on the ID of the product(s), or based on the Description of the product(s)... It'd be very nice!  Create DDT: Create a DDT with a pre-formatted Excel sheet. Just insert some information, and then all the products that have been withdrawn from the database ( maximum 14 per DDT ); I have already done some of work I described here, and you can find all files you need attached to this post. 
      I don't want you to do this. Just, don't post your code that I have been looking for, but let me try, with your help, to get an appropriate and efficient solution to what I'm asking.
      The last, but not the less important, the recognition I can give to you is publish your name ( or alias ) on the "developer section", more than a donation for everyone who has take part to this project.

      Please, if you want to know further information ( I think I've been exhaustive enough ), write here or on a PM. 

      Another time, a big thank you to this fantastic community and, a bigger one, to the developers of this amazing and powerful programming language.

      Enjoy your night.

      Francesco 

      PS: I will post the code tomorrow ( the most updated code ), so you can see where I have been arrived and, nothing Warehouse_Management.xlsx
      Gestione_Magazzino_SYS.au3
    • Faedien
      By Faedien
      Hello all, 
      I've lurked around here for a while and learned a ton from everyone, but I can't see this issue anywhere.  I've been running macros through Autoit for a while in a number of programs, but I'm having some issues with this one.
      Func RunMacro() Local $oExcel = _Excel_Open(False, False, False, False) ;Excel Program Local $oWorkbook = _Excel_BookOpen($oExcel, "C:/Equity Helper/Scripts/calendarmacros.xlsm", Default, Default, True) $oExcel.Run("calendarmacros.xlsm!Calendar") EndFunc When I run this bit of code everything works fine as long as the macro gets to finish.  But the actual VBA calls two "Application.UserInput" fields, and if I cancel out of either the macro closes, and then AutoIt throws an error at--
      $oExcel.(<---error throws here)Run("calendarmacros.xlsm!Calendar") I don't want to use autoit for the user inputs if possible, it's just easier to get the data I need for the inputs from within the VBA environment, but I'm afraid that's what's causing the problem.
    • jonson1986
      By jonson1986
      I'm trying to write data from array into .text file with line break and i did it successfully.
      but now I want to write from the same array into already empty excel with with line breaks means each value of array should be written in next cell automatically as it's happening in text file using "Filewrite"
      Looking for guidance.
      Here is my code;
      $file = fileopen(@scriptdir & "\source.txt", 10) $IE = _IECreate("http://www.example.com") $source = _IEDocReadHTML($IE) FileWrite($file, $source) $target_source1 = _StringBetween($source, '<span>', '</span>') If Not @error Then For $i=0 To UBound($target_source1) -1 FileWrite (@scriptdir & "\save.txt", $target_source2[$i] & @crlf) ; I want to write above array onto excel file Next EndIf  
    • cookiemonster
      By cookiemonster
      Im trying to edit a file, I want to find a string which has a line break in it, and replace it with a string that has multiple line breaks in it.
      editfile.txt looks like this:
      dog cat mouse chicken my au3 script looks like this but is not working, i suspect because of how I am trying to do the line breaks?
      Func EditFile($CurrentFile) $szFile = "$CurrentFile" $szText = FileRead($szFile,FileGetSize($szFile)) $szText = StringReplace($szText, "Cat" & @CRLF & "Dog", "Hippo" & @CRLF "Lion" & @CRLF & "Tiger") FileDelete($szFile) FileWrite($szFile,$szText) EndFunc ;--EditFile-- But once ran the file should look like this:
      Hippo Lion Tiger cat mouse chicken I cannot replace by line number as the animals are not in the same line in each copy of the file i want to run against.
       
      Can anyone help?