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 -
-
' Make an exact duplicate of the currently open document, optionally to a different
-
' template and return reference to the new.
-
-
Public Function DuplicateDocument(ByVal vobjSourceDocument As Word.Document, _
-
ByRef robjNewDocument As Word.Document, _
-
Optional strNewTemplate As String = "") As Boolean
-
-
Dim I As Long
-
Dim strBookmarkName As String
-
Dim strTemplateName As String
-
Dim strTemplateLanguage As String
-
Dim strArrayBookmarkNames() As String
-
Dim strArrayBookmarkValues() As String
-
Dim objWordApplication As Word.Application
-
Dim GGADebug As String
-
-
On Error GoTo Error
-
DuplicateDocument = False
-
Set objWordApplication = vobjSourceDocument.Application
-
-
' Store all the bookmark values in temp array
-
ReDim strArrayBookmarkNames(vobjSourceDocument.Bookmarks.COUNT + 1)
-
ReDim strArrayBookmarkValues(vobjSourceDocument.Bookmarks.COUNT + 1)
-
For I = 1 To vobjSourceDocument.Bookmarks.COUNT
-
strBookmarkName = vobjSourceDocument.Bookmarks.Item(I)
-
strArrayBookmarkNames(I) = strBookmarkName
-
vobjSourceDocument.Bookmarks(I).Select
-
strArrayBookmarkValues(I) = Selection.Text
-
End If
-
Next I
-
-
' copy body text inc. Annexes
-
If strNewTemplate = "" Then
-
' Get the current document's template name
-
strTemplateName = vobjSourceDocument.AttachedTemplate.FullName
-
Else
-
strTemplateName = strNewTemplate
-
End If
-
-
' create a new document based on the template name
-
blnWordFileDuplicate = True
-
'Set robjNewDocument = New Word.Document
-
Set robjNewDocument = objWordApplication.Documents.add(strTemplateName)
-
GGADebug = VarType(robjNewDocument) 'When this = 8 (string) it works, fails on 9 (Automation Object)
-
'How this can sometimes be 8 and sometimes 9 is a mystery
-
'Why it works when it is a string and not an object I don't know
-
-
GGADebug = GGADebug & " " & IsObject(robjNewDocument) ‘IsObject is always true, even when VarType is String
-
blnWordFileDuplicate = False
-
-
' paste existing content into new document - do not copy styles \rt !!!
-
For I = 1 To UBound(strArrayBookmarkNames()) - 1
-
If strArrayBookmarkNames(I) <> cstrWordBookmarkBody Then
-
‘Fails on the next line
-
writeToDocument robjNewDocument, strArrayBookmarkNames(I), _
-
strArrayBookmarkValues(I), ""
-
'With robjNewDocument
-
' .EditGoto strArrayBookmarkNames(I)
-
' .Insert strArrayBookmarkValues(I)
-
'End With
-
End If
-
Next I
-
-
DuplicateDocument = True
-
Exit Function
-
-
Error:
-
-
objWordApplication.ScreenUpdating = True
-
-
MsgBox LoadDBString(myLanguageOffset + 250) & " !" & vbNewLine & vbNewLine & _
-
"Source = " & Err.Source & vbNewLine & vbNewLine & GGADebug & vbNewLine & vbNewLine & _
-
" Description = " & Err.Description & " " & Err.Number, vbOKOnly, "Duplicate Document"
-
-
End Function
6 2844
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.
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.
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
I know where it fails. It fails as soon as I try to use the new document after the SET line:
[color=#b1b100] - [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] - [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
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]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ike |
last post by:
Anyone knows what cuases this under 6.0 ? -Ike
|
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...
|
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...
|
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...
|
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.
...
|
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)...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |