2016-03-23 46 views
0

由於舊邏輯具有已糾正的各種錯誤,並且在無法找到答案的一種情況下將其縮小爲一個錯誤,所以代碼已從之前的帖子中清除並更改。當我的url被讀爲只有值並且即使數組被初始化時拋出下標超出範圍錯誤,目前出現錯誤。其他條件,當用戶有預設項目或完全沒有關鍵的作品。謝謝。使用數組將多個多字符串值添加到註冊表中

option explicit 
    'on error resume next 
    Dim ObjName,oADSysInfo,strComputer 
    Dim objReg,IE_Main,mstrValName,strFunctionIntranet,strNYHomepage,multiStringValues(),allURLs(),itemname,a,return 
    Set oADSysInfo = CreateObject("ADSystemInfo") 
    Set ObjName = GetObject("LDAP://" & oADSysInfo.UserName) 
    strComputer = "." 
    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 
    Const HKCU = &H80000001 
    IE_Main = "Software\Microsoft\Internet Explorer\Main" 
    mstrValName = "Secondary Start Pages" 

    strNYHomepage = "http://www.google.com" 
    strFunctionIntranet = "www.mycompany.com" 

    SetHomePage 

    Sub SetHomepage 

      objReg.setStringValue HKCU,IE_Main,"Start Page",strNYHomepage 

      'Reading MultiStringValue of "Secondary Start Pages" for HKCU and continuing if it has something preset. 
      return = objReg.getMultiStringValue (HKCU,IE_Main,mstrValName,multiStringValues) 

      If return=0 Then 
         a=0 
         'Reading all items currently set to make sure users retain their existing URLs. 
         For Each itemname In multiStringValues 

         'Only continue if any of the existing URLs DO NOT MATCH what we are enforcing as the URL. 
          If itemname <> strFunctionIntranet Then 
           WScript.Echo itemname    
           WScript.Echo "itemname is NOT equal intranet" 
           a = a + 1 
           ReDim Preserve allURLs(a) 
           allURLs(a) = itemname 
           'a = a + 1 
          End If  
         Next 

         objReg.DeleteValue HKCU,IE_Main,mstrValName 
         'Enforce our URL to always be the first item. 
         allURLs(0)=strFunctionIntranet 

         'Set the new MultiStringValue registry key back. 
         objReg.setMultiStringValue HKCU,IE_Main,mstrValName,allURLs 
         WScript.echo "finished setting all secondary tabs... " 

      Else 

       strFunctionIntranet = Array(strFunctionIntranet) 
       objReg.setMultiStringValue HKCU,IE_Main,mstrValName,strFunctionIntranet      

      End If 

    End Sub 
Wscript.Quit 
+0

不能使用'陣列()':

改變這一行:

ReDim Preserve allURLs(a+1) 

到這一點。另外,'allURLs'已經是一個數組了,爲什麼'Split'呢? –

+0

嗨Ansgar,其實我已經拿出了數組(allURLs)和它上面的ReDim,仍然是類型不匹配的錯誤。請幫忙。感謝您快速查找並回復我的問題。 – NYPkgFellos

+0

請顯示確切的錯誤消息和引發它的行。根據你的代碼,你應該得到一個「無效參數」錯誤。 –

回答

1

你的數組包含一個空元素,因爲你創建它one field too big。在賦值的左側

ReDim Preserve allURLs(a) 
+0

感謝您的幫助,我修改了代碼,使其更具可讀性,並更改了您提供的內容,但在另一種情況下又出現了另一個錯誤,但其他條件正常工作。我用新的邏輯更新了上面的代碼。謝謝。 – NYPkgFellos

+0

沒有意識到有關於此的另一個問題,擴大了問題[這裏](http://stackoverflow.com/a/37142455/692942)。 – Lankymart

相關問題