Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word, Custom Document Properties, Invalid procedure call 1

Status
Not open for further replies.

michaelbr55

Programmer
Oct 8, 2002
70
I have writen a macro to add custom properties to a document, this worked well in the first instance, but now, after changing some other parts of the whole program, this macros generates a "Invalid procedure call or argument (Error 5)"
every time it is run. If I debug it, when I hold the cursor over the "Name" in "Add Name" it returns "Microsoft Word", I don't know if this has anything to do with the problem or not. below is the part of the macro that is giving me the error.

refno = 0
refno = ActiveDocument.CustomDocumentProperties("No of References").Value
refno = refno + 1
ActiveDocument.CustomDocumentProperties.Add _
Name:="References" & refno, LinkToContent:=False, Value:=TextEntry(X), _
Type:=msoPropertyTypeString

If refno > 0 Then
ActiveDocument.CustomDocumentProperties("No of References").Delete
ActiveDocument.CustomDocumentProperties.Add _
Name:="No of References", LinkToContent:=False, Value:=refno, _
Type:=msoPropertyTypeString
End If

Can anyone help Please!!!!!!!!!!
 
Michael,

My guess is that you've somehow broken the line continuation sequence between

Code:
ActiveDocument.CustomDocumentProperties.Add _

and

Code:
Name:="References" & refno, LinkToContent:=False, Value:=TextEntry(X), _

with the result that Name is being interpreted as Application.Name (the default if no object is specified with Name). Hence, the value "Microsoft Word". This is a read-only property so your code causes an error.

HTH
Mike
 
Thanks for the speedy reply Mike, I tried your suggestion by writing the code all on one line, unfortunately with the same result. I also copied the code, as is, into another document and it worked fine, got any other suggestions, this thing is driving me batty!!!!
 
Michael,

Which line is highlighted when the sub throws an error? I ask because when I mocked this up in a clean document, I received Error 5 at

Code:
refno = ActiveDocument.CustomDocumentProperties("No of References").Value

This is because the Property "No of References" does not exist. This is easily compensated for. Let me know where your error is occurring.

Mike
 
Mike, I also get the error message at this line, but I expected that if this property didn't exist, the "Resume Next" compensates for this. But the "add name" lines shouldn't generate the same error, and don't in a clean new document.
 
Michael,

If you are using
Code:
On Error Resume Next
then you should not see any trappable run-time errors. So, I'm not sure how you have this set up in your code. That being said, if I understand what you are trying to do with the code segment you posted, I think this can be simplified. It appears that you are using the custom property "No of References" as a means to store the number of "ReferenceN" properties created (where N = incremented integer). If so, the "No of References" property is not needed. I suggest replacing what you posted with:

Code:
  refno = ActiveDocument.CustomDocumentProperties.Count
  refno = refno + 1

  ActiveDocument.CustomDocumentProperties.Add _
  Name:="References" & refno, LinkToContent:=False, Value:=TextEntry(X), _
  Type:=msoPropertyTypeString

This should work properly whether it is being run for the first time on a given document (i.e.
Code:
CustomDocumentProperties.Count
is 0) or one or more "ReferenceN" properties already exist. Let me know what you think.


Regards,
Mike
 
Mike,
To see why this code was not working I disabled (' rem) the "On Error Resume Next" line.
I tried your suggestion and the "Count" property worked fine, but every other line with CustomDocumentProperties in it still generates the same error.

I just re-wrote the code and put it in a separate sub, but it still doesn't work. Here is the complete sub

Sub WriteCustomProperties()
On Error GoTo errorhandler
'mydoc = ActiveDocument

refno = 0
refno = ActiveDocument.CustomDocumentProperties.Count
refno = refno + 1

ActiveDocument.CustomDocumentProperties.Add Name:="References" & refno, _
LinkToContent:=False, Value:=TextEntry(X), _
Type:=msoPropertyTypeString


If refno > 0 Then
ActiveDocument.CustomDocumentProperties("No of References").Delete
ActiveDocument.CustomDocumentProperties.Add Name:="No of References", LinkToContent:=False, Type:=msoPropertyTypenumber, Value:=refno
End If

Exit Sub

errorhandler:

result = MsgBox("Error " & Err & " Occured in line " & Erl, vbCritical, "What The!!!")
Resume Next

End Sub

Thanks for all of your help, so far, Mike.
If you have any other suggestions I'm all ears!
 
Michael,

I have to admit, I'm stumped. I am unable to duplicate the error condition.

As an aside, my version does not require

Code:
    If refno > 0 Then
        ActiveDocument.CustomDocumentProperties("No of References").Delete
        ActiveDocument.CustomDocumentProperties.Add Name:="No of References", LinkToContent:=False, Type:=msoPropertyTypenumber, Value:=refno
    End If

since it no longer uses the "No of References" property to hold the "ReferenceN" property count.

Regards,
Mike
 
Hi Mike,
My doc does contain proprietary info, and I didn't think you would really want 840k and 200 pages of code anyway.
It still has me stumped as well but I have found a work-around, the problem is that in my doc, it cannot translate this "msoPropertyTypeString" into its number (when I change the upper case letters in this to lower case they don't change back either) so just subsituteing the number "4" in this case and all is well.
I have also used your much simpler code using the count property and removing the "No of References" property.

Thank for all your help Mike [pipe]
 
I don't think it has anything to do with the problem. I have the following code in a working macro:
Documents("NewTemplate.doc").CustomDocumentProperties.Add _
Name:="FooterBlurb", LinkToContent:=False, Value:="Additional footer text here", _
Type:=msoPropertyTypeString
The macro has many similar lines, and when I hold the cursor over Name in "Add Name" I too get Name = "Microsoft Word". This happens before the line has been executed, while the line is highlighted, and after the line has been executed, but the line does execute. When I checked the NewTemplate.doc, the FooterBlurb property had been added as usual.

When I tried running your code snippet, it ran just fine for me as long as I added a Custom Document Property named "No of References" to my active doc and put quotes around TextEntry(X). So I would guess that your problem is either that your active doc doesn't have a No of References property or the rest of your code isn't generating TextEntry(X) as before.
 
Hi Margot?
Thanks for your input but I think I have a handle on the problem now.
I've removed the "No of References" property as Mike suggested and just use the Count property to find out how many References their are, and the textentry(x) is a array variable which gets it's value from another part of the whole program.
It's just that Word could not translate the msoPropertyTypeString into a number for some reason in my code, if I copy this snippet into a new doc it works fine, anyway I just substutite the number 4 for the msoPropertyTypeString and all is fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top