• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Export a cell containing text into a .docx file - keep formatting

OCOR

New Member
Hello! This is my first forum post. I need to export content from each cell in one column to a word document (or .mht file). I am very new excel programming. I would like the macro to take the content of one cell (ie. row 2 column B) and turn it into a .docx - repeating this down all of column B. It would be extra helpful if it could take the name of the content that is located in the cell to the left of the cell being exported (ie. row 2 column A) and use that as the name for the new document created.

I have found out how to take a cell and export it to a .txt file using this code:
Code:
Sub MakeTextFiles()
Dim X As Long, Z As Long, FF As Long, TextOut As String
Const OutputPath As String = "c:\temp\" '<==Note the trailing backslash
Const BaseFileName As String = "Instance_"
Const StartColumn As Long = 2 'Assumed Column B
Const StartRow As Long = 2 'Assumed Row 3
For X = StartColumn + 1 To StartColumn + 1
TextOut = ""
For Z = StartRow To StartRow + 1
TextOut = TextOut & Cells(Z, StartColumn).Value & " " & Cells(Z, X).Value & vbNewLine
Next
FF = FreeFile
Open OutputPath & BaseFileName & (X - StartColumn) & ".txt" For Output As #FF
Print #FF, TextOut
Close #FF
Next
End Sub

The problem is that .txt files remove the formatting. I want the text formatting in each cell to transfer the same into the new .docx file. My small amount of coding knowledge makes me believe that if I could find out how to extract a cell to a .docx, then I could do something like:
For each cell in column B (Create a .docx file).

EXAMPLE:
353679d1414013218-export-a-cell-containing-text-into-a-docx-file-keep-formatting-excel-example.jpg


Referring to the picture above - essentially the code would take row 1 column B export it to a .docx file and name that file 'The letter A.docx'. Then it would do the same for the next row; take row 2 column B export it to a .docx file and name it 'The letter B.docx'.

Any help on the code for this would be WONDERFUL!! Thank you!
 
Last edited by a moderator:
I made something similar lately, although I put specific cells to default letters in word.

I used bookmarks in word to export the cells from excel to the word document.
If your new document has to be empty I suggest opening a word document and make a bookmark (for example name it 'Today') and than save it as a template.
This way you can open the templates as a new .docx put the cells value in the bookmark and than save it using the string from the cell next to it.

Here the model I used (without a loop, I didn't need one), hope it helps.

Michael

Code:
  'Word
  Dim wdApp As Word.Application
  Dim myDoc As Word.Document
  Dim mywdRange As Word.Range

  'Bookmark value
  Dim Today As Excel.Range

Set wdApp = New Word.Application
  With wdApp
  .Visible = True
  .WindowState = wdWindowStateMaximize
  End With

Set myDoc = wdApp.Documents.Add(Template:="C:\Users\Templates\Template1.dotx")

  Set Today = Sheets("Sheet1").Range("B1")

With myDoc.Bookmarks
  .Item("Today").Range.InsertAfter Today
End With

myDoc.SaveAs (“C:\Users\HAF\Desktop\" & Sheets("Sheet1").Range("A1") & ".docx”)

Set wdApp = Nothing
  Set myDoc = Nothing

[\code]
 
Back
Top