Hi people, I am doing some unpaid work for a friend on an Access/VB application they have. Everything worked for 5 years and then suddenly one day they got the following error:

Run-time error '91':

Object varialbe or With block variable not set.

When I click on debug it shows this code as the issue. The asterix line is the issue:

With objWord

    **objWord.ActiveDocument.SaveAs FileName:=strPath & strDocName, _
            FileFormat:=wdFormat**

'CLOSE FILE
'.Documents.Close (strPath & strDocName & ".docx")

objWord.ActiveDocument.Close
'REOPEN FILE
objWord.Documents.Open (strPath & strDocName)

'SET VIEW TO PRINT LAYOUT

objWord.ActiveWindow.View.Type = wdPrintView


End With

There is far more code than this, but this is the section that errors.

Also objWord has been set earlier in the code as per below:

' CREATE WORD DOCUMENT USING SMEC TEMPLATE (TEMPLATE 1)
Private Sub CmdCreateTemplate1_Click()

   On Error GoTo MergeButton_Err

   Dim objWord As Word.Application

   Set objWord = CreateObject("Word.Application")
   With objWord
      ' Make the application visible.
      .Visible = True
      ' Open the document.

Two questions. Obviously the first is HELP! can someone tell me how to fix it? but secondly how did it happen? I mean no one touches this code, and one day it decides to have a bug? Would it be an update etc?

I have a bit of VB and Access background, but nothing off the top of my head comes to mind. Appreciate anything you can do.

Thanks in advance.

Andrew.

Recommended Answers

All 7 Replies

You have set your object in a subso the scope only works in the sub. Do the same in the othe sub or declare the object on top of yxour code.

I will try that, but I still ask, even if that fixes it, why did it happen, when for five + years the code worked as written?

Well you asked the question yourself "What did happen?". Something must have changed. Some one fiddling with the code?, data has changed?, the feature has not been used before? If you google arror 91 and vb6 you will find many reasons - the main one as your error described.

I have google lots about this issue and agree there seems to be many fixes for this, and I am sure we'll get it done. However, the code has NOT been touched. No-one in the office knows anything about it and doesn't have access to it. The guy who origianlly wrote it had a quick look and believes the code it ok, but doesn't have the time to look into it. It worked perfectly as is until some point when some guys believe microsoft updates went through, and it didn't work the next day. I'll keep trying and will let you know how it goes. Cheers.

Isn't it because you're trying to access the object's methods directly using with? Try deleting objWord in lines 3, 9, 11 and 15.

scudzilla, thanks for the thought, but it didn't work. We're looking into the possiblity that a Word or VB plugin has stuffed it up. Will keep trying and let you know.

Ok...I am still trying to work it out. But have found out the following. The code does not work on the machine I am on (desktop). It does not work for the guy who wrote the code and he is on a laptop. BUT it DOES work on his bosses laptop. So the unchanged code does work, but not on all machines. Does this sound like an update issue? Eg It was written on Office 2007 and they have migrated to 2010. Are there libraries it may not be looking at? If so, why our machines but not the boss? Could there be some registries that are wrong or no longer needed, that the code refers too? AHHHHHHH!!!! This is about to do my head in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.