I have an Excel document with a lot of code in it already that does things unrelated to any of the code I'm using in this module. The issue I'm having trouble with is passing an object of type Word.Document
to a subroutine that will eventually do stuff to alter that word document. First, the code I have.
Option Explicit
Public oWordApplication As Word.Application
Public oWordDocument As Word.Document
Sub GenerateWordProposal()
Set oWordApplication = New Word.Application
Set oWordDocument = oWordApplication.Documents.Add
oWordApplication.Visible = True
oWordDocument.Activate
PCL_PageLayout (oWordDocument)
PCL_HeaderFooter (oWordDocument)
End Sub
Sub PCL_PageLayout (theDoc as Word.Document)
'Nothing here yet.
Debug.Print "PageLayout code"
End Sub
Sub PCL_HeaderFooter (theDoc as Word.Document)
'Nothing here yet.
Debug.Print "HeaderFooter code"
End Sub
So when I execute this code, the error I get is Compile Error: Type mismatch
. I'm not even manipulating it in any way yet. I have a slew of things I intend to throw in here as each document that's passed to this subroutine needs to start off looking the same from the onset. There will be other subroutines that will modify the individual documents depending on information from the Excel sheet, however none of that has been coded yet.
I searched here and on Google but I can't seem to find anything that specifically refers to passing Word.Document
objects to subroutines/functions. Tried byRef
and byVal
and they both return the same error. Tried changing the sub's arguments to type Variant
and it throws a Runtime Error 424, Object Required
. I've enabled the Microsoft Word 16.0 Object Library
in references and it is at the bottom of the priority list and the only one added besides the default ones.
The question is, what am I doing wrong? Can Word.Document
or Word.Application
not be passed as arguments? Eventually I will need to execute the code from each of these subroutines on multiple different documents so just coding in the individual values needed isn't really an option that works for this.