VBA opens word, creates new document

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Code:
Private Sub test()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
'==================================================<o:p></o:p>
'Procedure-level declarations<o:p></o:p>
'==================================================<o:p></o:p>
<o:p> </o:p>
Dim newHelp As Word.Application, theDate, thePath As String<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
'==================================================<o:p></o:p>
'Sets the path, the date, and the application object<o:p></o:p>
'==================================================<o:p></o:p>
<o:p> </o:p>
thePath = ThisWorkbook.Path
<o:p> </o:p>
Set newHelp = CreateObject("Word.Application")<o:p></o:p>
<o:p> </o:p>
newHelp.Visible = True<o:p></o:p>
<o:p> </o:p>
'==================================================<o:p></o:p>
'Opens Word, creates a new document, saves the file<o:p></o:p>
'==================================================<o:p></o:p>
<o:p> </o:p>
newHelp.NewDocument.Add (thePath & "Name.doc")<o:p></o:p>
<o:p> </o:p>
End Sub

This code successfully creates a Word window but doesn't create a document.

I've had this problem before when opening a browser window and trying to get a macro to do work in that window. It was necessary to use doevents and readystate and busy properties, but Word doesn't support those properties.

Any suggestions?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try

Code:
set newDoc=newHelp.Documents.Add 'no parameters
newDoc.saveas(thePath & "Name.doc")
 
Upvote 0
Worked perfectly. Scott, can you please explain why my other attempt didn't work? If I don't understand that then I feel like I'm not gaining what I really need.

I used the integrated list of properties and methods that were revealed when I set a reference to the Word 11.0 Object Library, every step of the way. "NewDocuments" and "Add" came straight out of the automatic dropdown.

What went wrong?
 
Upvote 0
Honestly, I have no idea.

I don't use word automation very often. When I do, I find the expected behaviours don't happen very often. The whole thing is a chaotic mess.

Its the Quantum Mechanics of the VB world (or I don't really know what I'm doing either :laugh:).

So I decided that finding, remembering and repeating what does work when I discovered it was the most effective learning strategy. Don't beat yourself up about it!
 
Upvote 0
Another example of this is trying to place new data at the end of the document instead of where ever the cursor happens to be

Where my sub includes these declarations

Code:
Dim WD As Word.Application, wDoc As Word.Document, myRange

I found I had to constantly do the following before adding each snippet of data

Code:
Set myRange = wDoc.Content
myRange.Collapse (wdCollapseEnd) 'wdCollapseStart
myRange.MoveEnd

From what I'd seen on the subject, that shouldn't have been necessary. So either I was misinterpreting or it was behaving oddly.
 
Upvote 0
I notice that I get a lot of error messages that don't really have anything to do with what I'm working on.

I'll get a message that says "For without next" when what's really wrong is that there's an "End If" missing somewhere in the middle of the loop... or I'll get a "Range method failed" message when really there's a syntax error a hundred lines away from the "Range" statement that's being spotlit.

Your comments are much appreciated...
 
Upvote 0
I'll get a message that says "For without next" when what's really wrong is that there's an "End If" missing somewhere in the middle of the loop... or I'll get a "Range method failed" message when really there's a syntax error a hundred lines away from the "Range" statement that's being spotlit.

Those are (somewhat understandable) limitations with the compiler rather than code gremlins. A user error prevents it from correctly determining where the "thread" is ending.

I've got so used to committing the first error myself that it is the first thing I check for when that error is thrown up.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top