2
votes

I recently learned how to manipulate Word documents through VBA in Excel. My problem is I want my macro to be able to determine an active word document with a proper filename and select that document and continue the macro. I have this code below

Sub CreateNewWordFile()
  Dim wrd As Word.Application
  Dim Opfile As Object
  Dim AcFile As Object

  Set Opfile = GetObject(, "Word.Application")
  Debug.Print Opfile.Documents.count
  For Each AcFile In Opfile.Documents
    If AcFile = "SampleWord " & Format(Now, "mm-dd-yyyy") & ".docx" Then
      Set wrd = AcFile.Application
      With wrd
       .Activate
       .Selection.TypeParagraph
       .Selection.TypeParagraph
       .Selection.Font.Size = 20
       .Selection.TypeText "success"
       .ActiveDocument.Save
       GoTo Label1
      End With
    End If
  Next
  Set wrd = New Word.Application

 With wrd
   .Visible = True
   .Activate
   .Documents.Add
   With .Selection
       .ParagraphFormat.Alignment = wdAlignParagraphCenter
       .BoldRun
       .Font.Size = 18
       .TypeText "Sample Word File"
       .BoldRun
       .TypeParagraph
       .Font.Size = 12
       .ParagraphFormat.Alignment = wdAlignParagraphLeft
       .TypeParagraph
       .ParagraphFormat.Alignment = wdAlignParagraphCenter
       .Font.Size = 15
       .TypeText "samples"
   End With

  ActiveDocument.SaveAs2 Filename:="Documents\SampleWord " & Format(Now, "mm-dd-yyyy") & ".docx"

  End With

Label1:

  Set Opfile = Nothing
  Set wrd = Nothing

End Sub

Below the For Each loop is my code for creating a Word document in case there is no open Word document with the proper filename. The problem is when the For Each Loop runs the macro it gives the error

ActiveX component can't create Object

But when I turn it into a comment and just run my code that creates a Word document and uncomment it for my second run to test, it works. Also I notice that the Documents.count doesn't count the open Word documents. I tried opening several Word documents but it doesn't count them. I hope someone can help, thank you.

1

1 Answers

4
votes

I'm going to give you a lot of information, but not change your code. You'll learn more if you can implement what I explain. Your main problem comes from not fully grasping what's happening when you use GetObject vs. New Word.Application. Once you get that sorted you should be OK.

If Word is not running at all, then GetObject returns the error message you're seeing. The typical way to handle that is to test the error and start Word, if necessary, for example

On Error Resume Next
Set wrd = GetObject(, "Word.Application")
If err.number = 429 Then
    Set wrd = new Word.Application
End If
On Error GoTo 0

HOWEVER, since you're looking for a specific document, as long as that document has been saved and you know the file path, you could (but don't have to) use

Dim wrdDoc as Object
Set wrdDoc = GetObject("C:\ExamplePath\DocName.docx")
Set wrd = wrdDoc.Application

It's also not necessary to loop the Documents collection to pick up a document with a certain name. You can also do:

Set acFile = wrd.Documents("Filename")

You can test whether that document exists using

If acFile Is Nothing Then
  'put the code to create a new DOCUMENT here
  Set acFile = wrd.Documents.Add
  'Do all the formatting, etc. here
End If

The main problem with Documents.Count comes from using

Set wrd = New Word.Application

every time the code doesn't find the specific document. This creates a new instance of Word every time it executes. Each instance is independent of any others, which is why Documents.Count doesn't return a number equal to all the documents you've generated. It is only running for the current instance of Word.