There is a view that Solomon wrote (and collected) the proverbs when he was young, the Song of Songs before mid-life, and Ecclesiastes when he was old.
The first words in the book, after the byline, are:
1:2 Vanity of vanities, says the Teacher,
vanity of vanities! All is vanity.
That word, vanity, has also been translated as meaningless, futile, transient, profitless, and perplexing. Its precise meaning depends on the context. While Solomon could use one word, English doesn’t have a single equivalent.
The book of Ecclesiastes records the struggle Solomon had with God in finding meaning in all his wisdom, power and wealth.
He tried wisdom, but possibly discovered he knew too much:
1:18 For with much wisdom comes much sorrow; the more knowledge, the more grief.
So he tried ‘pleasure’ and ‘great projects’, but:
2:11 Then I considered all that my hands had done and the toil I had spent in doing it, and again, all was vanity and a chasing after wind, and there was nothing to be gained under the sun.
And even though he decided ‘that wisdom is better than folly’, he also came to believe that:
2:14The wise have eyes in their head, but fools walk in darkness. Yet I perceived that the same fate befalls all of them.
So should we just be content what what we have?
2:24-25 There is nothing better for mortals than to eat and drink, and find enjoyment in their toil. This also, I saw, is from the hand of God; for apart from him who can eat or who can have enjoyment?
But even this is meaningless (or vanity), a chasing after the wind.
It’s a message of despair at this stage. Was it written after Solomon was rebuked by God?
One commentator puts it this way:
[Solomon] examines for us, turning life over and over in his hands so that we see it from every angle. And he forces us to admit that it is vanity, emptiness, futility; yet not in the sense that it is not worth living. … The book is a record of … an endeavor to give meaning to life, …[but] Life has lost the key to itself. If you want the key [to life] you must go to the locksmith who made the lock. From: The Theology of Ecclesiastes