Excel line break vs VBA line break? - vba

I'm writing VBA codes to read spreadsheet contents and write to a TXT file.
Some of the cells has multi-line text. They are displayed as multi-line in the spreadsheet.
However, when the VBA code read the cell and write to the TXT file, they appear on the same line. This also happens when I copy the cell and past it into Notepad.
For example in spreadsheet, cell A1 appears as:
Text1
Text2
Text3
But in TXT file, it appears as: Text1Text2Text3.
So I guess CHAR(10) in Excel is not equivalent to vbNewLine in VBA? What do I need to do to get the line breaks into TXT file? Thank you!

Do a Replace(YourCellValue, vbLf, vbCrLf) to get line breaks that works in Windows.

I had the same problem, and resolved it by changing my column to word wrap. Please be gentle if this is a silly solution - this is my first answer on the forum. :-)

Related

How to insert code in Word, export to pdf, and keep formatting?

I've written some Python code for my thesis, and wish to add this to my report in Word. I have to hand it in in pdf format. But when I convert the word document, the code cannot be copy pasted and used again, as the tabs are replaced by a single space.
Is there any solution for this?
found the answer: replace all spaces by _ , and possibly make them invisible (white).
If you want to copy paste and use the code, just replace all _ by spaces again.
Solved!

How to remove hidden quotes

I've Excel sheet that has few text columns. These text columns are Email Messages. The data from this sheet will be used to send mails.
There data looks fine in Excel but when the message is copied to the Email body quotes are appearing in the beginning and end of the message.
I researched online and found out that these are unwanted characters. I tried removing the " using following formula.
=SUBSTITUTE(SUBSTITUTE(CLEAN(K1),CHAR(127),""),CHAR(160),"")
However the problem is that there are multiple columns with this problem so this method is not very feasible option for me. Also another problem is that after this the cell loses the formatting.
Please help me resolve this, I'm looking for a Find and Replace method if possible. Worst case scenario would be a macro.
Thanks in advance.
Cells.Replace What:=Chr(127), Replacement:=vbNullString
Cells.Replace What:=Chr(160), Replacement:=vbNullString
Your cells in your excel sheet contains multiple lines of data within a data, which means all lines in the cell are entered with carriage return. (Enter Key)
If you copy and paste such cells to a txt file, you will get the text within a " ". The " " are not actually quotes, but text with carriage return.
Just use the formula and let me know if it works,
=SUBSTITUTE(A1,CHAR(10)," ")

VBA: Copy whole word document to excel

I´m trying to simply copy whole word document into excel and keep the source formatting (and images).
Assuming both documents are open.
I tried this code:
Sheets("Nabídka").Range("A" & 87) = Documents("K4E pila.docx").Range.Text
but it only copies the text without formatting and images. Is there a similar command which includes the formatting?
Even If I copy/paste from word to excel (ctrl+c) the formatting is ruined --> The image overlaps the text.
This is the first step I need to figure out to proceed in my project. The outcome should be: Copy all word documents into excel if the name of the word document matches some excel cells values.
Thanks in advance for any help!

Vb.net Exporting Richtextbox formated content to Excel Cell

Further to my first question regarding that issue, here are my progress:
First : using the function: sheet.Cells(X, X).Value = RichTextBox1.Rtf, I find this kind of cell content:
"rtf1\ansi\ansicpg1252\deff0\deflang1036\deflangfe1036{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Calibri;}{\f2\froman\fprq2\fcharset0 Times New Roman;}}
{\colortbl ;\red255\green0\blue0;\red0\green0\blue139;}
\viewkind4\uc1\pard\f0\fs22 XXXXXXXXXXX\par
\cf1\strike\f1 XXXXXXXXXXX\par"
I can however manually use the excel function "keep the original formatting" to paste some rtf doc content into a cell.
Could you help me find a way to use this function in the VB.net code if possible?

How can I copy text from a Word 2010 table into a VBA string, maintaining line breaks?

I've got a table in Word with a cell with a few short lines in. I want to copy the contents of that cell into a .txt file, maintining the line breaks.
Currently the line breaks are lost:
Contents of Word table cell:
Alice
Bob
Contents of VBA String:
AliceBob
Code sample to get string:
MyString = ActiveDocument.Tables(a).Cell(1, 1).Range.Text
I solved my problem by using copy & paste into a new word document, then saving the new document as a text file. Not neat or tidy, but effective enough for the situation.