PDA

View Full Version : PrivateProfileString Error



Jack Feeman
12-07-2007, 11:29 AM
I have Document_New() code that auto-names a document based on an external text file.
The VBA runs when the template is opened by the using from a website (SharePoint) and assigns a filename of {000-07} and places it in the document at a predefine location (bookmarked as Order). It works great on my machine only and errors on everyone else's. I assume that it is because it is trying to write to each person;s registry even though nothing in the VBA (except possibly PrivateProfileString). Is there anything that can be done to prevent the code from trying to write to the registry? Is there another expression that can be used that doesn't send it to the registry. My current code follows. The part about saving back to the web server using the document name as the filename has been commented out until I can get that to work (actually it works just doesn't append filename with the .doc file extension).
///Code follows:///
Private Sub Document_New()
' Unprotect document to run macro
ActiveDocument.Unprotect
Order = System.PrivateProfileString("\\{server}/{dir}/{sub-dir}/ECN.txt (file://\\{server}/{dir}/{sub-dir}/ECN.txt)", _
"MacroSettings", "Order")
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
System.PrivateProfileString("\\{server}/{dir}/{sub-dir}/ECN.txt (file://%7Bserver%7D/%7Bdir%7D/%7Bsub-dir%7D/ECN.txt)", "MacroSettings", _
"Order") = Order
ActiveDocument.Bookmarks("Order").Range.InsertBefore _
Format(Order, "00#") & "-" & Format(Now, "YY")
'ActiveDocument.SaveAs FileName:=Format(Order, "00#") & "-" & Format(Now, "YY")
' Re-protect document for Forms after running macro
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
' Save ECN with ECN number for file name and in the ECN library
'ActiveDocument.SaveAs FileName:=http://{webserver}/{site}/{sub-site} & _
'Format(Order, "00#") & "-" & Format(Now, "YY")

End Sub
///End Code///

Thanks
Jack

TonyJollans
12-07-2007, 05:17 PM
I know nothing about opening files from SharePoint but I would have thought people could write to their own registry. You say you _assume_ that is the cause - what makes you assume that? (not that I'm saying you're wrong, I just have never seen an error like that and don't know what the message might be)

Jack Feeman
12-08-2007, 11:21 AM
Hi Tony,

Actually someone in the MS Community defined the string "PrivateProfileString" as writing to the registry and even gave the registry keys. Since it worked beautifully on my machine but never on another machine logged on as someone else. So I have no personal kowledge that it does. I should have included the actual error - my error. This is the actual error everyone (except me) receives:
Quote
Run-time error '-2147467259(80004005)':
Method 'PrivateProfileString' of object 'System' failed
Unquote

Does that make any sense to you? I really need to make this work so any help would be greatly appreciated.
Thanks
Jack

TonyJollans
12-08-2007, 12:55 PM
I've had a look at your code now - it always helps :)

PrivateProfileString is really a hangover from long ago and can look either in an INI file or the registry. In your code it appears to be looking for an INI file on a server.

The error message is just a generic message that doesn't provide much useful information. However, from a quick google, it appears that the return code *may* indicate a network error so the question is, perhaps, do other people have access to the same server as you?

Jack Feeman
12-09-2007, 09:41 AM
Thanks Tony, I'll take a look from that perspective.
Jack