English 中文(简体)
VBA 用户论坛:在运行期间添加文本Box或Button指挥系统和活动
原标题:VBA UserForm: Add TextBox or CommandButton and event during runtime

我很高兴得到一些帮助! 我在寻找整个网络,但我m!

我早就一直在为之进行规划,但我仍在努力理解这一语言!

I want to create a VBA UserForm in MS Project 2007 VBA. A few data are dynamic and so I need to add a few text fields during runtime.

我把一些法典放在一起添加这些内容,而且这项工作非常出色。

www.un.org/Depts/DGACM/index_spanish.htm 我的问题是在这些案文领域增加活动。

My example is the txtPath text field. I create it with this code:

  Set NewTextBox = Me.InfoMultiPage(page).Controls.Add("Forms.textbox.1")
  With NewTextBox
      .name = "txtPath"
      .value = "Test"
      .top = m2w_style("top") + (m2w_style("height") * 1)
      .Left = m2w_style("left") + m2w_style("lblWidth") + m2w_style("margin")
      .Width = m2w_style("txtWidth")
      .height = m2w_style("height")
      .font.Size = m2w_style("fontsize")
      .font.name = m2w_style("font")
  End With

And I want a reaction if the value of txtPath has changed. Here the code:

Private Sub txtPath_Change() Event doesn t shoot readProjectsFromConfig (Me.value) End Sub

我的所有网站都浏览和搜索了,显示它应该以这种方式工作,但活动只是没有打断。

我发现,像人工制作的文字箱一样,在“当地窗口”的树林中,动态生成的案文领域没有在同一地方展示。

So I tried this to at least get the value of the text field and it works.

Private Sub btnPath_Click()
   txtPath.value = "Hello World!"   Doesn t work. Dynamicly created text field seems not to exist but is visible in UserForm
   Controls.Item("txtPath").value = "Hello World!"   This works!
  Controls.Item("txtPath").value = GetDirectory("Pick the folder")   Pick a folder and write it in the text field txtPath
End Sub

这里的全部测试代码:

  Reference to Library
  Microsoft XML, v5.0 need to be activated.
  Go to menu: Tools->References
  Select Microsoft Scripting Runtime

Public m2w_config As Dictionary
Public m2w_style As Dictionary


Sub m2wVariables()
    Set global Variables for configuration in a kind of hash.
  Set m2w_config = New Dictionary
  Set m2w_style = New Dictionary

   Styles for teh UserForm
  m2w_style("font") = "Arial"
  m2w_style("fontsize") = 10
  m2w_style("top") = 6
  m2w_style("left") = 6
  m2w_style("height") = 20
  m2w_style("btnHeight") = 8
  m2w_style("width") = 40
  m2w_style("lblWidth") = 40
  m2w_style("h1Width") = 400
  m2w_style("txtWidth") = 180
  m2w_style("btnWidth") = 72
  m2w_style("margin") = 6

  m2w_config("XMLDateFormat") = "YYYY-MM-DD"
  m2w_config("XMLConfigFileName") = "config.xml"   should not be changeable
  m2w_config("AppPath") = ""
  m2w_config("Headline") = ""   Headline in Website
  m2w_config("UpdateHref") = ""
  m2w_config("SubFolder") = ""   Is it used?
  m2w_config("default_subfolder") = ""   Is it used?

End Sub

  Private Sub UserForm_Activate()

      Dim LabelArr As Variant
      Dim ProbNameArr As Variant
      Dim TempForm As Object
      Dim NewButton As MSForms.CommandButton
      Dim NewLabel As MSForms.Label
      Dim NewTextBox As MSForms.TextBox
      Dim e As Variant
      Dim x As Integer
      Dim page As String
       Dim Line As Integer
       Dim MyScript(4) As String

      m2wVariables


        Setup userform
       ~~~~~~~~~~~~~~~~

         This is to stop screen flashing while creating form
        Application.VBE.MainWindow.Visible = False

          Setup tab Website
         ===================
          page = "Website"
          Set NewLabel = Me.InfoMultiPage(page).Controls.Add("Forms.label.1")
          With NewLabel
              .name = "lblHeadlinePath"
              .Caption = "This is the local path where the website shall be stored."
              .top = m2w_style("top") + (m2w_style("height") * 0)
              .Left = m2w_style("left")
              .Width = m2w_style("h1Width")
              .height = m2w_style("height")
              .font.Size = m2w_style("fontsize")
              .font.name = m2w_style("font")
          End With

          Set NewLabel = Me.InfoMultiPage(page).Controls.Add("Forms.label.1")
          With NewLabel
              .name = "lblPath"
              .Caption = "Path:"
              .top = m2w_style("top") + (m2w_style("height") * 1)
              .Left = m2w_style("left")
              .Width = m2w_style("lblWidth")
              .height = m2w_style("height")
              .font.Size = m2w_style("fontsize")
              .font.name = m2w_style("font")
          End With

          Set NewTextBox = Me.InfoMultiPage(page).Controls.Add("Forms.textbox.1")
          With NewTextBox
              .name = "txtPath"
              .value = "Test"
              .top = m2w_style("top") + (m2w_style("height") * 1)
              .Left = m2w_style("left") + m2w_style("lblWidth") + m2w_style("margin")
              .Width = m2w_style("txtWidth")
              .height = m2w_style("height")
              .font.Size = m2w_style("fontsize")
              .font.name = m2w_style("font")
          End With

           Add event onClick
            This is completely weird, it actualy writes code.
            My intention is to add an event at runtime.
          With ThisProject.VBProject.VBComponents("msp2web_SettingsForm").CodeModule
            .insertlines .CountOfLines + 1, "Sub txtPath_Change()" & vbCrLf & "MsgBox Me.txtPath.Value" & vbCrLf & "End Sub"
            Debug.Print Now & " This macro has code lines " & .CountOfLines
          End With


          Dim btnName As String
          btnName = "btnPath"
           Set NewButton = Me.InfoMultiPage(page).Controls.Add("Forms.commandbutton.1", btnName)   Add dynamicly - but I m too stupid to add an event action to an dynamicly created button...
          Set NewButton = Me.InfoMultiPage(page).Controls.Item(btnName)
          With NewButton
              .Caption = "Browse..."
              .top = m2w_style("top") + (m2w_style("height") * 1)
              .Left = m2w_style("left") + m2w_style("lblWidth") + m2w_style("margin") + m2w_style("txtWidth") + m2w_style("margin")
              .Width = m2w_style("lblWidth")
              .height = m2w_style("btnHeight")
              .font.Size = m2w_style("fontsize")
              .font.name = m2w_style("font")
              .AutoSize = True
          End With


          Setup Tab Project
         ===================
        page = "Project"
        LabelArr = Array("Hallo", "Welt", "Model Year")
        ProbNameArr = Array("Hallo", "Welt", "Model Year")

         Create 10 Labels just for testing - works fine
         For x = 0 To 9
        x = 0
        For Each e In LabelArr
            Set NewLabel = Me.InfoMultiPage(page).Controls.Add("Forms.label.1")
            With NewLabel
              .name = "FieldLabel" & x + 1
              .Caption = e
              .top = m2w_style("top") + (m2w_style("height") * x)
              .Left = m2w_style("left")
              .Width = m2w_style("lblWidth")
              .height = m2w_style("height")
              .font.Size = m2w_style("fontsize")
              .font.name = m2w_style("font")
            End With
            x = x + 1
        Next

         Create 10 Text Boxes
         For x = 0 To 9
        x = 0
        For Each e In ProbNameArr
            Set NewTextBox = Me.InfoMultiPage(page).Controls.Add("Forms.textbox.1")
            With NewTextBox
              .name = "MyTextBox" & x + 1
              .top = m2w_style("top") + (m2w_style("height") * x)
              .Left = m2w_style("left") + m2w_style("lblWidth") + m2w_style("margin")
              .Width = m2w_style("lblWidth")
              .height = m2w_style("height")
              .font.Size = m2w_style("fontsize")
              .font.name = m2w_style("font")
            End With
            x = x + 1
        Next

    End Sub

    Private Sub btnPath_Click()
       txtPath.value = "Hello World!"   Doesn t work. Dynamicly created text field seems not to exist but is visible in UserForm
       Controls.Item("txtPath").value = "Hello World!"   This works!
      Controls.Item("txtPath").value = GetDirectory("Pick the folder")   Pick a folder and write it in the text field txtPath
    End Sub

    Private Sub txtPath_Change()   Event doesn t shoot
      readProjectsFromConfig (Me.value)
    End Sub


    Private Sub Refresh_Click()
      readProjectsFromConfig (Controls.Item("txtPath").value)
    End Sub

www.un.org/Depts/DGACM/index_spanish.htm 寒冷的人告诉我,如何建立基于(运行时间)的文字箱和指挥区,并增加它们的活动?

最佳回答
问题回答




相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签