DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How to Write To A Word File Using Macros (VBA) in Excel Office 365?

In this article, you will learn the simple steps to write to a Word File using Macros (VBA) in Excel Office 365. Let’s get them below!! Get an official version of MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

The Process Of Writing To A Word File Using Excel VBA

  • Firstly, in the Excel Worksheet, you have to go to the Developer Tab.
  • Then, you need to select the Visual Basic option under the Code section.

Select Visual Basic

  • Now, you have to copy and paste the code given below.
 Sub WriteToWord()

   ''Using Early Binding

    'Dim wordApp As Word.Application

    'Dim mydoc As Word.Document

    'set wordApp=new word.Application 

    'Using late binding to create word application object

    'Declaring variables as objects

    Dim wordApp As Object

    Dim mydoc As Object

    'Initializing the object using CreateOBject Function

    Set wordApp = CreateObject("Word.Application")

    'Making word App Visible

    wordApp.Visible = True

    'Creating a new document

    Set mydoc = wordApp.Documents.Add()

    'Getting input from user

    myString = Application.InputBox("Write Your Text")

    'Writing to word

    wordApp.Selection.TypeText Text:=myString

    'inserting a new paragraph

    wordApp.Selection.TypeParagraph

End Sub 
Enter fullscreen mode Exit fullscreen mode
  • After that, you need to save the code by selecting it and then close the window.

Save the Code

  • Again, you have to go to the Excel Spreadsheet , and click on the Developer Tab.
  • You need to choose the Macros option in the Code section.

Choose Macro option

  • Then, you have to make sure that your macro name is selected and click the Run button.

Click on the Run option

  • Now, you will receive an input dialog box, and then you need to type any text in the text box.

Type input text

  • Finally, you will receive your input text in the Word document.

Final Input text in Word

Check this too:

Verdict

In the above article, you can learn the simple steps to write to a Word File using Macros (VBA) in Excel Office 365. Kindly, share your feedback in the below comment section. Thanks for visiting Geek Excel. Keep Learning!

Read Also:

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay