Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:42:10+00:00 2026-05-16T01:42:10+00:00

I’ve trying to simplify a script and it’s routines and I’m not quite sure

  • 0

I’ve trying to simplify a script and it’s routines and I’m not quite sure what’s the best way of doing it. I want to repeat code as little as possible. So below is my current code.

This is the Filename function that basically checks if an ini-file exists.
It should probably just run once. The way it’s now, it’s run everytime the string “FileName” is used, which is a lot.

Function FileName()
    FileName = "C:\Apps\Templates\fields.ini"
    ''# Does ini-file exist?
    If Len(Dir$(FileName)) = 0 Then
        MsgBox ("Can't find the file " & FileName & ".")
    End If
End Function

Then I have this regPath and regString function which basically just picks up the path in the registry that the script should read from.

Public Function regPath()
    regPath = ReadIni(FileName, "Registry", "Path")
End Function

Public Function regString()
    regString = ReadIni(FileName, "Registry", "String")
End Function

Then there’s the first function that reads the actual registry for its value.

 Public Function regFirstname()
        regStrFistname = ReadIni(FileName, "Fields", "Firstname")
        Set objShell = CreateObject("Wscript.Shell")
        Dim value As String
        value = objShell.RegRead(regPath & "\" & regStrfirstname)
        regFornavn = value
    End Function

I have loads of this last one, just that it reads other parts of the registry. All this code is contained in a module in a Word 2007 template.
And from my Document_New() sub I just basically want to pick up the value from the registry, without having so much code in the Document_New() sub routine. This way I can use the values in forms and other areas as well.

How should I structure this? Any reply would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:42:11+00:00Added an answer on May 16, 2026 at 1:42 am

    You can probably change the Filename function to a sub:

    Sub getFileName()
        FileName = "C:\Apps\Templates\fields.ini"
        ''Does ini-file exist?
        If Len(Dir$(FileName)) = 0 Then
            MsgBox ("Can't find the file " & FileName & ".")
        Else
           gvFileName = "C:\Apps\Templates\fields.ini"
        End If
    End Sub
    

    Add a variable to the top of your module called gvFilename. Note that these variables are reset if an unhandled error occurs, so you may need to run the sub again if gvFilename contains a null string (vbNullString).

    These:

    Public Function regString()
        regString = ReadIni(FileName, "Registry", "String")
    End Function
    

    Can probably be changed to:

    Public Function regStuff(ToRead As String)
        regStuff = ReadIni(FileName, "Registry", ToRead)
    End Function
    

    Which can be called like so:

    regString=regStuff("String")
    

    The registry reader can also probable be changed like so:

    Public Function regStuff(ToRead As String)
        regToRead = ReadIni(FileName, "Fields", ToRead)
        Set objShell = CreateObject("Wscript.Shell")
        Dim sValue As String
        sValue = objShell.RegRead(regPath & "\" & regToRead)
        regStuff = sValue
    End Function
    

    As an aside, avoid using reserved words like Value in your code, it can lead to untold grief.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.