Create new MS Word 365 doc and put text in it with VS 2017

Maslow Raffo
Maslow Raffo used Ask the Experts™
on
Using VS 2017 to open an MS Word 365 doc, but how can I create a new MS Word 365 document and then put text in it?

THIS WORKS TO OPEN EXISTING:
    Private Sub btn_print_Click(sender As Object, e As EventArgs) Handles btn_print.Click
        Dim objWordApp As Microsoft.Office.Interop.Word.Application
        objWordApp = New Microsoft.Office.Interop.Word.Application
        objWordApp.Visible = True
        objWordApp.Documents.Open("C:\NP_EVAL.docx")
    End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Maslow,
 Dim objWordDoc As Microsoft.Office.Interop.Word.Document
...
...
...
    Set objWordDoc = objWordApp.Documents.Open("C:\NP_EVAL.docx")
    objWordDoc.Content.InsertAfter "Experts Exchange"
    objWordDoc .Save
    objWordDoc .Close
    objWordApp.Quit

Open in new window


Also you can simplify the namespace so you don't have to write entire assembly's name to use the Office objects.

Regards,
Chinmay.
Sam JacobsDirector of Technology Development, IPM

Commented:
I believe that line 6 should have the text in parenthesis:
objWordDoc.Content.InsertAfter("Experts Exchange")

Open in new window

Author

Commented:
Thank you, but my question is how do I open a NEW MS word doc and insert that text?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Sorry I saw your last question and got confused.

To create a new document, after you have the App object, simply use objWordApp.Add

Reference : https://excel-macro.tutorialhorizon.com/vba-excel-create-and-save-the-word-document/
Sam JacobsDirector of Technology Development, IPM

Commented:
Change line 5 to:
objWordDoc = objWordApp.Documents.Add

Open in new window

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Yupp. What Sam said. 🙂

Author

Commented:
Hey--it worked beautifully!  Thanks to both of you!

Is there a way to divide the credit for your respective help?
Sam JacobsDirector of Technology Development, IPM

Commented:
Chinmay did most of the work, so mark his post as the solution ... you can then mark mine as helpful, if you so choose.

Author

Commented:
Got it--thanks again

Author

Commented:
Thanks for the quick and helpful responses!
Sam JacobsDirector of Technology Development, IPM

Commented:
You're most welcome.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
I am not being modest here. And I can't blame a sleepless night either. I had a wonderful sleep. I made two critical mistakes - some credit goes to the mobile but again, that's no excuse. And Sam picked up my slack. I think it should be 60/40 or 80/20. Sam's answer is more accurate.

Author

Commented:
I don't see the option to divide by %...Only "This is my solution" and "No, but it is helful." Happy to know if I can be more specific, as I'll be asking more questions. Don't see any way to apply specific percentages. :-\
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Even I don't know. I thought there was a slider which you can drag to assign points. I am sorry but I have never used that feature hence the confusion. Marking the answer as helpful works as well. I did the same. If you refresh the page you will be able to see it.

Author

Commented:
ok, got it. thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial