Open DOCX File

Information, tips and instructions

How to create a DOCX from VBA for Excel

Microsoft VBA (Visual Basic for Applications) is a high-level language designed for Microsoft Office macros and is based on Visual Basic; This language is used to program Windows applications and is included in several Microsoft applications, especially office automation. VBA allows users and programmers to extend the functionality of programs in the Microsoft Office suite. Visual Basic for Applications is a subset of Visual Basic with practically all the functions of the original language. You can even create interoperable files between programs in the suite, that is, you can create DOCX files from VBA to Excel, and vice versa.

In this case, we are going to see the basic principles to create a DOCX document from VBA for Excel.

To create a file in Word from the VBA editor:

  1. You have to go to Tools, click on Macro and select Visual Basic Editor.
  2. You will see a window called Microsoft Visual Basic open. Select Tools->References from the menu and make sure that "Microsoft Word 16.0 Office Library" is selected.
  3. Write the following code:

Public Sub testword1 ()

Dim objWord As Word.Application

Dim string As String


string = "Text you want to create in a word file"

string = string & "text from a cell in excel that you want to add in word (A1):" & ThisWorkbook.Worksheets ("Sheet1"). Range ("A1"). Value

string = string & "text from another cell you want to add (B1):" & ThisWorkbook.Worksheets ("Sheet1"). Range ("B1"). Value


Set objWord = CreateObject ("Word.Application")


objWord.Documents.Add

objWord.ActiveDocument.Content.FormattedText.Text = string

objWord.ActiveDocument.SaveAs "c:\testword1"

objWord.Quit True

Set objWord = Nothing

End Sub

* name with which you want to save the word file

When we execute the command or click on the save button, the file will be saved in the path that we have indicated in quotation marks.

Development
Conversion
File Extension Info

DOCX Quick Info
  Microsoft Word OOXML Document File
MIME Type
  application/vnd.openxmlformats-officedocument.wordprocessingml. document
Opens with
  Microsoft Word
  Microsoft Office Online
  Apache OpenOffice
  LibreOffice