1

I'm writing a macro in VBA that creates a word document using

Sub WriteDocument()
wordapp = CreateObject("word.Application")
letter = wordapp.Documents.Add

TypeText()

wordapp.Visible = True
End Sub

Originally I'd tested this to satisfaction by running the macro and then closing the word document without saving it. Now that I'm actually saving the documents, the macro is taking longer and longer to run each time. I think the issue has to do with WINWORD.EXE and how it behaves if the document is saved or not.

I tried using

If wordapp Is Nothing Then
Set wordapp = CreateObject("word.Application")
End If

But now I'm getting errors if I run the macro repeatedly and without saving. Can someone explain the issue to me and help me resolve it?

Our company recently switched from Office 07 - Office 10. Would this affect anything? I noticed that new documents/spreadsheets always open in existing instances of Word/Excel instead of a new one.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
user2437713
  • 53
  • 3
  • 9

1 Answers1

1

Are you writing in Word's own VBA editor? Then you don't need to create an explicit reference to a Word Application instance. Just use the default application instance, i.e. the one you're currently writing code in:

Dim doc As Document
Set doc = Documents.Add ' don't need explicit reference to wordapp

Or are you calling Word from some other Office application e.g. Excel? Then ok, you do need an explicit reference to an instance of Word. However you don't need to create a new instance, and in fact you may not want to do that, because eventually you could have many running instances piling up. Instead, you can try getting an existing instance first:

Dim doc As Object
Dim wordapp As Object

On Error Resume Next
Set wordapp = GetObject(, "Word.Application")
If wordapp Is Nothing Then Set wordapp = CreateObject("Word.Application")
On Error GoTo 0    
Set doc = wordapp.Documents.Add
'...

As you see, a little error handling is required to guard against the case where there is no existing instance; in this case we just create a new one.

Finally, note that it is necessary to use the keyword Set when setting references to objects such as a Word application or document. You can't just use an assignment, i.e. say Set wordapp = ... not just wordapp = ... like you do a couple of places in your question.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • thanks for your comprehensive response. I am indeed calling Word fromExcel and you're right about the sets. I have one more issue and this should be resolved. If I close the document I created using the Close Button in Word, it won't close WinWord.Exe. How can I get around this? I thought about having an event handler in my Excel application (Workbook_BeforeClose) but I don't want to shut down Word if the end user is using it for something else. – user2437713 Nov 05 '13 at 17:13
  • Too long to answer in a comment -- please post as a new question – Jean-François Corbett Nov 05 '13 at 19:16
  • ok, I submitted one. http://stackoverflow.com/questions/19913664/winword-exe-wont-close – user2437713 Nov 11 '13 at 18:52