This macro works fine in Word 2007 using Vista on a desktop, but not in Word 2013 running Windows 8.1 on a Surface Pro. Any sugestions?

I am using a simple macro in a Word template I created in Word 2007 to add a unique sequential number at the bottom of a one page document and it works great on my desktop computer running Windows Vista.  When I use the same template with Word 2013 on a Surface Pro running Windows 8.1, I get a VBA error stating: "Method 'PrivateProfileString' of object 'System' failed".  I have attached some images for clarification.  I hope they are legible.  If not, I can email them in a Word document.  The settings.txt file is indeed in the root of C:\.  Any ideas would be appreciated as I am new to both Windows 8.x and Word 2013 and I am not sure which might be giving me the grief.

Thanks.

Lamarr

Answer
Answer

System.PrivateProfileString is often a bit buggy.  Instead, use the SaveSetting Statement and GetSetting Function

From the VBA Help File

Save Setting Statement

Saves or creates an application entry in the application's entry in the
Windows registry or (on the Macintosh) information
in the application's initialization file.

Syntax

SaveSetting appname, section, key, setting

The SaveSetting statement syntax has these named arguments:

Part Description
appname Required. String expression containing the name
of the application or project to which the setting
applies. On the Macintosh, this is the filename of the initialization file in
the Preferences folder in the System folder.
section Required. String expression containing the name of the section where the key
setting is being saved.
key Required. String expression containing the name of the key setting being
saved.
setting Required. Expression containing the value that
key is being set to.

Remarks

An error occurs if the key setting can't be saved for any reason.


Example

The following example first uses the SaveSetting statement
to make entries in the Windows registry (or .ini file on 16-bit Windows
platforms) for the

MyApp
application, and
then uses the DeleteSetting statement to remove them.

' Place some settings in the registry.
SaveSetting appname := "MyApp", section := "Startup", _
            key := "Top", setting := 75 
SaveSetting "MyApp","Startup", "Left", 50 
' Remove section and all its settings from registry.
DeleteSetting "MyApp", "Startup" 
GetSetting Function

Returns a key setting value from an application's entry in the Windows registry or (on the Macintosh) information in the
application’s initialization file.

Syntax

GetSetting(appname, section, key[, default])

The GetSetting function syntax has these named arguments:

Part Description
appname Required. String expression containing the name
of the application or project whose key setting is requested. On the Macintosh,
this is the filename of the initialization file in the Preferences folder in the
System folder.
section Required. String expression containing the name of the section where the key
setting is found.
key Required. String expression containing the name of the key setting to
return.
default Optional. Expression containing the value to
return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string
("").

Remarks

If any of the items named in the GetSetting arguments do
not exist, GetSetting returns the value of default.


Example

This example first uses the SaveSetting statement to make
entries in the Windows registry (or .ini file on 16-bit Windows platforms) for
the application specified as appname, and then uses
the GetSetting function to display one of the settings.
Because the default argument is specified, some value
is guaranteed to be returned. Note that section names
can't be retrieved with GetSetting. Finally, the DeleteSetting statement removes all the application's
entries.

' Variant to hold 2-dimensional array returned by GetSetting.
Dim MySettings As Variant
' Place some settings in the registry.
SaveSetting "MyApp","Startup", "Top", 75
SaveSetting "MyApp","Startup", "Left", 50

Debug.Print GetSetting(appname := "MyApp", section := "Startup", _
                       key := "Left", default := "25")

DeleteSetting "MyApp", "Startup"
Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 164 Applies to: