473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VB6 - Error 91 Object or Block Variable Not Set

My application creates Word documents. The routine below takes an existing, open document (vobjSourceDocument) and makes a new document (robjNewDocument) similar to it. The routine works 999+ times out of 1000. I cannot find any pattern regarding document type (template) or qualities of the source document, but once I have a source document that fails it will fail consistenly on any workstation.

In the code sample below it is trying to write to the new document, but it will fail on any object method I use. When the routine works, the VarType = 8 (string) but when it fails the VarType = 9 (automation object). In either case, IsObject always = True.

Thank you in advance,

Glen

Expand|Select|Wrap|Line Numbers
  1.  
  2. ' Make an exact duplicate of the currently open document, optionally to a different
  3. ' template and return reference to the new.
  4.  
  5. Public Function DuplicateDocument(ByVal vobjSourceDocument As Word.Document, _
  6.                                    ByRef robjNewDocument As Word.Document, _
  7.                                    Optional strNewTemplate As String = "") As Boolean
  8.  
  9. Dim I As Long
  10. Dim strBookmarkName As String
  11. Dim strTemplateName As String
  12. Dim strTemplateLanguage As String
  13. Dim strArrayBookmarkNames() As String
  14. Dim strArrayBookmarkValues() As String
  15. Dim objWordApplication As Word.Application
  16. Dim GGADebug As String
  17.  
  18. On Error GoTo Error
  19.     DuplicateDocument = False
  20.     Set objWordApplication = vobjSourceDocument.Application
  21.  
  22.     ' Store all the bookmark values in temp array
  23.     ReDim strArrayBookmarkNames(vobjSourceDocument.Bookmarks.COUNT + 1)
  24.     ReDim strArrayBookmarkValues(vobjSourceDocument.Bookmarks.COUNT + 1)
  25.     For I = 1 To vobjSourceDocument.Bookmarks.COUNT
  26.         strBookmarkName = vobjSourceDocument.Bookmarks.Item(I)
  27.         strArrayBookmarkNames(I) = strBookmarkName
  28.         vobjSourceDocument.Bookmarks(I).Select
  29.           strArrayBookmarkValues(I) = Selection.Text
  30.         End If
  31.     Next I
  32.  
  33.     ' copy body text inc. Annexes
  34.     If strNewTemplate = "" Then
  35.         ' Get the current document's template name
  36.         strTemplateName = vobjSourceDocument.AttachedTemplate.FullName
  37.     Else
  38.         strTemplateName = strNewTemplate
  39.     End If
  40.  
  41.     ' create a new document based on the template name
  42.     blnWordFileDuplicate = True
  43.     'Set robjNewDocument = New Word.Document
  44.     Set robjNewDocument = objWordApplication.Documents.add(strTemplateName)
  45.     GGADebug = VarType(robjNewDocument)   'When this = 8 (string) it works, fails on 9 (Automation Object)
  46.                                             'How this can sometimes be 8 and sometimes 9 is a mystery
  47.                                             'Why it works when it is a string and not an object I don't know
  48.  
  49.     GGADebug = GGADebug & " " & IsObject(robjNewDocument) ‘IsObject is always true, even when VarType is String
  50.     blnWordFileDuplicate = False
  51.  
  52.     ' paste existing content into new document - do not copy styles \rt !!!
  53.     For I = 1 To UBound(strArrayBookmarkNames()) - 1
  54.         If strArrayBookmarkNames(I) <> cstrWordBookmarkBody Then
  55. ‘Fails on the next line
  56.             writeToDocument robjNewDocument, strArrayBookmarkNames(I), _
  57.                                     strArrayBookmarkValues(I), ""
  58.             'With robjNewDocument
  59.              '   .EditGoto strArrayBookmarkNames(I)
  60.               '  .Insert strArrayBookmarkValues(I)
  61.             'End With
  62.         End If
  63.     Next I
  64.  
  65.     DuplicateDocument = True
  66.     Exit Function
  67.  
  68. Error:
  69.  
  70.     objWordApplication.ScreenUpdating = True
  71.  
  72.     MsgBox LoadDBString(myLanguageOffset + 250) & " !" & vbNewLine & vbNewLine & _
  73.            "Source = " & Err.Source & vbNewLine & vbNewLine & GGADebug & vbNewLine & vbNewLine & _
  74.            " Description = " & Err.Description & " " & Err.Number, vbOKOnly, "Duplicate Document"
  75.  
  76. End Function
Mar 6 '08 #1
6 2844
VBWheaties
145 100+
It looks like you are passing in (byVal) a Word.Document, then you are using that to obtain an instance of its Word.Application.
This will work as long as you have at least one instance of Word running in memory. If you dont, you'll get your error and that explains why it errors sometimes.
Solution may be to make sure your Word.Application is running before using it.

if that doesnt do it, let us know what line the code errors on. Set your Debug to Break On All Errors.
Mar 6 '08 #2
Thanks for a quick response!

The routine is actually called from the open document. The application is a dll that gets attached to the documents via the templates. The line of code that fails is:

‘Fails on the next line

writeToDocument robjNewDocument, strArrayBookmarkNames(I), _

However, even asking VB to return the documents name causes the error. I don't know how to set the debug but will ask/look around to see how to do this. I haven't programmed in decades.

It looks like you are passing in (byVal) a Word.Document, then you are using that to obtain an instance of its Word.Application.
This will work as long as you have at least one instance of Word running in memory. If you dont, you'll get your error and that explains why it errors sometimes.
Solution may be to make sure your Word.Application is running before using it.

if that doesnt do it, let us know what line the code errors on. Set your Debug to Break On All Errors.
Mar 6 '08 #3
kadghar
1,295 Expert 1GB
Thanks for a quick response!
...
‘Fails on the next line

writeToDocument robjNewDocument, strArrayBookmarkNames(I), _
Im not familiarized with the WriteToDocument Sub, so i Googled it and found something quite similar to the one you have. Why dont you use this one, with another name, so you can debug it and see where the error ocurs:
(change its name to MyWriteToDocument, copy-paste it anywhere, and use that one on your code, so when the error is shown, it'll be in the Sub's line, and you can have a better idea of what's wrong).

HTH
Mar 6 '08 #4
Killer42
8,435 Expert 8TB
Subscribing .
Mar 7 '08 #5
I know where it fails. It fails as soon as I try to use the new document after the SET line:
[color=#b1b100]
Expand|Select|Wrap|Line Numbers
  1. [color=#b1b100]Set[/color] robjNewDocument = objWordApplication.[color=#66cc66]Documents[/color].[color=#66cc66]add[/color][color=#66cc66]([/color]strTemplateName[color=#66cc66])[/color]
[/color]
[color=#b1b100][/color]
[color=#b1b100]
[color=#b1b100]For example, if I place the statement...[/color]
[/color]
[color=#b1b100][/color]
[color=#b1b100]
Expand|Select|Wrap|Line Numbers
  1. [color=#b1b100]GGADebug = robjNewDocument.FullName[/color]
[/color]
[color=#b1b100][/color]
[color=#b1b100]
[color=#b1b100]...after the SET statement it will fail with the same error (91).[/color]
[/color]


Im not familiarized with the WriteToDocument Sub, so i Googled it and found something quite similar to the one you have. Why dont you use this one, with another name, so you can debug it and see where the error ocurs:
(change its name to MyWriteToDocument, copy-paste it anywhere, and use that one on your code, so when the error is shown, it'll be in the Sub's line, and you can have a better idea of what's wrong).

HTH
Mar 7 '08 #6
QUOTE]
Thank you all for you help. Debugging is a humbling experience.

The cause of the error is an incorrect path to the template I was opening the new document with. When I split the creation of the document into 2 steps -- creating it and then associating it with the template VB was quick to point out that it could not attach the template because it couldn't find it.

If the original SET command had failed for this reason then it would have been obvious. Instead the SET command worked but returned a VARType of 9 instead of 8.

If you are curious about how the program came up with an incorrect path name then read on, else thanks again for your time and interest.

We keep the template presented to the users in subfolders of the workgroup location. The subfolders correspond to templates in different languages--a letter is a letter, but it may be in English, Russian, etc. Because we share documents across networks the attached template paths have different physical names from one network to another. We "work around" this potential problem (word not being able to find a documents attached template) by keeping hidden copies of all templates in the root of the workgroup locations folder. These 1 out of a thousand documents have this hidden template as their attached template rather than the same template in the subfolder. The routine that builds the string to use for the new document's template didn't work properly when the target template was in a different language subfolder and the attached template was in the root. This is now fixed.

Cheers,

Glen
[/quote]
Mar 7 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ike | last post by:
Anyone knows what cuases this under 6.0 ? -Ike
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
8
by: Lauren Quantrell | last post by:
I get the following error: "Object variable or Width block variable not set error" trying to run this in my Access2000 .ADP database: CurrentDb.Properties.Append...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
0
by: RJN | last post by:
Hi Sorry for posting again. I'm calling a shared method in the class. The following error is found in production though I'm not able to reproduce. ...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
4
by: majo | last post by:
Hi, I had posted this once earlier, but couldnt get my problem solved. When i run an asp.net application in windows 2003 server with IIS 6.0, it "SOMETIMES" gives me the below given error....
3
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
5
by: Al G | last post by:
Hi, I'm converting a bit of POP3 VB6 code to VB2005, and have run into this error with the following code. Can someone help me find out what I'm missing/doing wrong? 'holds the attachments...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.