-SOLVED- Create object in function

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
921
Office Version
  1. 365
Platform
  1. Windows
I'm making a program that creates some charts, pastes them to Word and loops that around a certain number of times, for example 40 times. I got it already to work, but then I realized that it's pretty slow to create the Word application and document each time from scratch, so I decided to do that once, delete everything from the document, paste the charts to Word save the document and loop that process instead.

So now I got to create that function. Here's the important part I have:


Code:
Sub ChartsMain()
    Dim DocWD As Word.Document

    Set DocWD = CreateChartDoc
End Sub

Code:
Function CreateChartDoc() As Word.Document
    Dim appWD As Word.Application 'Word application
    Dim DocWD As Word.Document 'Word document
    
    'Open a hidden Word instance
    Set appWD = CreateObject("Word.Application")
    appWD.Visible = False

    'Create a new document
    Set DocWD = appWD.Documents.Add
    
    'Return a Word document
    CreateChartDoc = DocWD
    
End Function

And yes, I do have the references set, because it works without this function (ie. when I create a new Word application each time)

I clearly have some syntax problem here about returning the object, since it gives me "invalid use of syntax". What is it and how do I fix it?

SOLUTION: The last linee misses a "SET" ie. it should be
Set CreateChartDoc = DocWD
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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