0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rudiez
  • 11
  • 3
  • Try removing this parenthesis `PCL_PageLayout (oWordDocument)` (and the other line too) - they are only needed in functions, and you are using them as subroutines. – braX Aug 10 '20 at 19:43
  • If you are going to use arguments to pass the document there is no need to declare oWordDocument as public. Instead declare those variables inside GenerateWordProposal. – Timothy Rylatt Aug 10 '20 at 19:48
  • 2
    https://stackoverflow.com/questions/5413765/what-are-the-rules-governing-usage-of-brackets-in-vba-function-calls/5413977#5413977 – Tim Williams Aug 10 '20 at 19:58
  • 1
    So it turns out I've been banging my head against the wall for the most obvious little mistake ever. Thank you braX for showing me that thinking too hard about something that should have been simple is a waste of time and money. You sir are a gentleman and a scholar. I'm pretty new to this so it might take me a minute to figure out how to put your comment as the solution and 'upvote' you. – Rudiez Aug 10 '20 at 20:01
  • Also, keep in mind if you're declaring this word document at the module level (public/private) you don't need to pass itas arguments: If it is public, you can access it in any module, and if it is private you can access it in any subroutine/function in the same module – Super Symmetry Aug 10 '20 at 20:04
  • The public declarations were part of me not understanding why they weren't passing as arguments. I will definitely fix this. Thank you. – Rudiez Aug 10 '20 at 20:08

1 Answers1

0

As answered by braX: Remove the parentheses

    PCL_PageLayout oWordDocument
Rudiez
  • 11
  • 3