If you are doing this in the SW VBA macro editor, one thing you might want to check is to make sure execution is beginning with your sub main. If you have your cursor elsewhere when you start running/debugging through your code, sometimes the execution starts with whatever procedure/module your cursor is in. This can leave some of your global or module-level objects uninstantiated.
This error can also crop up when the compiler just gets "lost". If you forget to close your if/then blocks with an endif, if you leave a for/next loop open, etc. it may interpret something in the wrong context and throw you this error. So if plan A above doesn't yield results, you may want to just do some more proof-reading of your code.
You are (most likely) using a "SET" type command, and the 'SET' is failing. If this is the case, you have to check your object to see if it is 'NOTHING'.
something likle this:
Dim MyModel as ModelDoc2
Dim DocTitle as string
SET MyModel=swapp.Activedoc ' Sets active SW doc into "MyModel"
DocTitle = MyModel.GetTitle ' gets name of doc in titlebar
... Now, if there were no files open in SW, the "MyModel" object would be "NOTHING", but there would be no error...
UNTIL you tried to get the Title of the document, in which case you would THEN get the "Object variable or With block variable not set" error.
Go backwards in the code from that line, looking for "SET" commands. In the line following a SET command, put in a line of code to check to see if the object is nothing...
SET MyModel=swapp.Activedoc
' ---- insert check for nothing here ----
if MyModel Is Nothing then msgbox "Cant find Object!"