• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Create New Word Document

Hello,

I am doing a small simple project where I am creating a Word Document (i'll call it Doc1) which will hold all the VBA/Macro code to create a new Word Document (i'll call it Doc2) partly based on information they have typed into User Form within Doc1; for example the User Form in Doc 1 will ask the user for a Document title. The user will enter the title of the document and click a create button which will create a new Word Document (Doc2) and add into it the title the user has entered into the User Form from Doc 1.

This appears very simple. However can anyone please point me in the right direction of the following; I am ok with creating and capturing all the information in the text fields on the User Form (Doc1) its just the creation of the document that I need help with.

1. How can I create a new Word Document and name it Doc2.docx and save it to my desktop?
2. Once the document has been created; add the title as entered into my User Form from Doc1?

Thanks in advance,

GISVPN

0
gisvpn
Asked:
gisvpn
  • 2
2 Solutions
 
GrahamSkanCommented:
Note that VBA projects are usually in templates, rather than documents.

To name a document, you must save it.

Sub NewDoc()
    Dim Doc As Document
    
    Set Doc = Documents.Add '(TemplatePath) if not Normal
    Doc.SaveAs "C:\MyFolder\" & TextBox1.Text & ".Docx"
End Sub

Open in new window

0
 
OP_ZaharinCommented:
- i would assume you have a module and a form called UserForm1, a textbox and a command button to click on for creation of a new document:

-- this is on Modules. call to display UserForm1 :
Sub NewDoc()
     UserForm1.Show
End Sub

Open in new window



-- this is on UserForm1 which contain a textbox called Textbox1 and command button called CommandButton1.:

Private Sub CommandButton1_Click()
  Set objWord = CreateObject("Word.Application")
  objWord.Caption = "This is Word Caption"
  objWord.Visible = True

  Set objDoc = objWord.Documents.Add()
  Set objSelection = objWord.Selection

  objSelection.Font.Name = "Arial"
  objSelection.Font.Size = "18"
   'this will display the title on the document
  objSelection.TypeText "Title: " & UserForm1.TextBox1.Text   

  'change the <profilename> below to your windows login profile id. 
  'test to open the desktop folder by typing the whole folder name on windows explorer 
  objDoc.SaveAs ("C:\Documents and Settings\profilename\Desktop\doc2.docx") 

End Sub

Open in new window

0
 
GrahamSkanCommented:
Yes, the use of the word 'Title' is a little ambiguous in Word.

There is a built-in Title property. It could mean the first text, probably with a Heading 1 style, or it could be the stem of the document's file name (or even all three).
0
 
gisvpnAuthor Commented:
Perfect thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now