How can the language code entered via an input box be set as defau

D

David Turner

I have a macro (based on code fragments largely lifted from this forum) to
set the language in all story ranges via the wd languageId code entered in an
input box. Is there any way to arrange for this code to become the default
the next time the macro is run so that the user does not have to change it
each time (from 1033 to 1036 say)? In general, a user is likely to work in
only one language. The code could be hard-wired but then you would need a
different version for each language.
Thanks.
David


Option Explicit

Sub LanguageIDInputBox()

'See http://msdn.microsoft.com/en-us/library/bb213877.aspx for languageIDs

Dim rngStory As Range
Dim Message, Title, Default
Dim MyLanguage
Dim lngJunk As Long
Dim oShp As Shape

' Define the message.
Message = "Enter WdLanguageID code i.e. wdEnglishUK (2057) wdFrench
(1036)"
Title = "Language InputBox" ' Defines the Title.
Default = "1033" ' Defines default value.
' Displays the message, the title and the default value
MyLanguage = InputBox(Message, Title, Default)
If MyLanguage = "" Then
MsgBox ("Nothing entered. Exiting routine.")
Exit Sub
End If

'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document

For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
If rngStory.Words.Count = 1 Then GoTo SetnextRange

Do
rngStory.LanguageID = MyLanguage

On Error Resume Next
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
rngStory.LanguageID = MyLanguage
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0

DoEvents

SetnextRange:
'Get next linked story (if any)

Set rngStory = rngStory.NextStoryRange

StatusBar = "Resetting language"

Loop Until rngStory Is Nothing

Next

StatusBar = "Language reset"
'Clean up
Set rngStory = Nothing

End Sub
 
P

Pesach Shelnitz

Hi David,

If you have your users create an environment variable on their machines, you
could use its value to set the default.
For example, you could have your users set an environment variable called,
say, WdLanguageID, equal to the applicable number and make the following
change in your code.

Change:

Default = "1033" ' Defines default value.

To:

Default = Environ$("WdLanguageID")
If Default = "" Then
Default = "1033"
End If

If you like this general approach, we could continue discussing the possible
ways to set the environment variable on your users' machines.
 
T

Tony Jollans

Nothing to do with language at all, but look at the VBA SaveSetting and
GetSetting Functions. These will save values in, and restore values from,
the User's registry, and can do what you want.
 
D

David Turner

Hello Pesach,

Thanks for your reply. I was wondering about the possibility of an
environment variable but couldn't find much information about it. I guess it
would be something like:

ActiveDocument.Variables.Add Name:="WdLanguageID", Value:="1036"
'MsgBox ActiveDocument.Variables("WdLanguageID").Value

but I'm not sure how users would set this on their machines. A macro in the
distributed template?Please enlighten me!

Thanks.

David




Pesach
 
P

Pesach Shelnitz

Hi David,

I would have preferred to set and read environment variables, but it seems
that VBA can't write them in the registry, so I've opted to setting a
registry value in the HKCU branch. Here is your code after my modifications
for storing the value for reuse in the registry.

Const Error_ValueNotFound = &H80070002
Dim WshShell As Object
Dim Message, Title, Default As String
Dim MyLanguage, MyKey As String

Set WshShell = CreateObject("WScript.Shell")
MyKey = "HKCU\Software\Microsoft\Office\Word\"
' Define the message.
Message = "Enter WdLanguageID code, i.e., " & _
"wdEnglishUS (1033), wdEnglishUK (2057), or wdFrench (1036)"
Title = "Language InputBox" ' Defines the Title.
Default = ""
On Error Resume Next
Default = WshShell.RegRead(MyKey & "WdLanguageID")
If Err.Number = Error_ValueNotFound Then
Default = "1033"
End If
On Error GoTo 0
' Displays the message, the title and the default value
MyLanguage = InputBox(Message, Title, Default)
If MyLanguage = "" Then
MsgBox ("Nothing entered. Exiting routine.")
Exit Sub
End If
WshShell.RegWrite MyKey & "WdLanguageID", MyLanguage, "REG_SZ"

I hope that the others don't yell at me for showing you how to monkey around
with the registry.
 
T

Tony Jollans

I doubt anyone will yell at you for showing the registry code (although it's
not wise to put your own values in a Microsoft key), but I might yell at you
for not reading my reply before posting :)

You don't need to use Shell; VBA has all you need built-in:

Dim Lang As WdLanguageID
Lang = GetSetting("MyApp", "Defaults", "LanguageId", wdEnglishUS)
Lang = InputBox("Please enter Language Id", "Language Id", Lang)
SaveSetting "MyApp", "Defaults", "LanguageId", Lang

"MyApp", "Defaults" and "LanguageId" are arbitrary literals; use your own
values.
 
D

David Turner

Wow, thanks Pesach. Feels like I'm getting a bit out of my depth but I'll
give it a try tomorrow!
David
 
P

Pesach Shelnitz

Hi,

Thank you Tony for your very constructive criticism. I actually tried to
follow your original suggestion, but I only succeeded after I studied your
example. I completely agree that your way is far better. I revised the code
in my previous post to use GetSetting and SaveSetting and also added code to
validate of the user input.

Dim Message, Title As String
Dim MyLanguage As String
Dim Lang, Default As WdLanguageID

' Define the message.
Message = "Enter WdLanguageID code, i.e., " & _
"wdEnglishUS (1033), wdEnglishUK (2057), or wdFrench (1036)"
Title = "Language InputBox" ' Defines the Title.
Default = GetSetting("MyApp", "Defaults", "LanguageId", wdEnglishUS)
MyLanguage = InputBox(Message, Title, Default)
' Validate the user input.
Select Case MyLanguage
Case Is = "wdEnglishUS", 1033
Lang = wdEnglishUS
Case Is = "wdEnglishUK", 2057
Lang = wdEnglishUK
Case Is = "wdFrench", 1036
Lang = wdFrench
Case Else
MsgBox "The value entered is not valid. Exiting routine."
Exit Sub
End Select
If Lang <> Default Then
SaveSetting "MyApp", "Defaults", "LanguageId", Lang
End If

Now the registry value is stored under the HKCU\Software\VB and VBA Program
Settings\MyApp\Defaults key. "MyApp" should be changed in the code to a more
meaningful name.

I hope that other readers will learn as much from this thread as I did.
 
D

David Turner

Thank you very much Tony.

Tony Jollans said:
I doubt anyone will yell at you for showing the registry code (although it's
not wise to put your own values in a Microsoft key), but I might yell at you
for not reading my reply before posting :)

You don't need to use Shell; VBA has all you need built-in:

Dim Lang As WdLanguageID
Lang = GetSetting("MyApp", "Defaults", "LanguageId", wdEnglishUS)
Lang = InputBox("Please enter Language Id", "Language Id", Lang)
SaveSetting "MyApp", "Defaults", "LanguageId", Lang

"MyApp", "Defaults" and "LanguageId" are arbitrary literals; use your own
values.

--
Enjoy,
Tony

www.WordArticles.com
 
D

David Turner

Many thanks for your help Pesach.

Pesach Shelnitz said:
Hi,

Thank you Tony for your very constructive criticism. I actually tried to
follow your original suggestion, but I only succeeded after I studied your
example. I completely agree that your way is far better. I revised the code
in my previous post to use GetSetting and SaveSetting and also added code to
validate of the user input.

Dim Message, Title As String
Dim MyLanguage As String
Dim Lang, Default As WdLanguageID

' Define the message.
Message = "Enter WdLanguageID code, i.e., " & _
"wdEnglishUS (1033), wdEnglishUK (2057), or wdFrench (1036)"
Title = "Language InputBox" ' Defines the Title.
Default = GetSetting("MyApp", "Defaults", "LanguageId", wdEnglishUS)
MyLanguage = InputBox(Message, Title, Default)
' Validate the user input.
Select Case MyLanguage
Case Is = "wdEnglishUS", 1033
Lang = wdEnglishUS
Case Is = "wdEnglishUK", 2057
Lang = wdEnglishUK
Case Is = "wdFrench", 1036
Lang = wdFrench
Case Else
MsgBox "The value entered is not valid. Exiting routine."
Exit Sub
End Select
If Lang <> Default Then
SaveSetting "MyApp", "Defaults", "LanguageId", Lang
End If

Now the registry value is stored under the HKCU\Software\VB and VBA Program
Settings\MyApp\Defaults key. "MyApp" should be changed in the code to a more
meaningful name.

I hope that other readers will learn as much from this thread as I did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top