私は職場でVBAを使用して文書を作成しています(これは職場で使用できる唯一のものです)。変数が設定された後、空の文字列が返されます。メソッドのハットが実行時に動的に生成されるので、ユーザーフォームにデータを渡そうとしています。公開変数は空の文字列を返します
私はそうThisDocumentのモジュール
Public theName As String
の先頭にその後、私はこれを実行するようにチェックボックスが
With tblNew
'.Cell(Row:=rowCount, Column:=2).Merge MergeTo:=.Cell(Row:=rowCount, Column:=3)
.Rows(rowCount).SetHeight RowHeight:=InchesToPoints(0.35), HeightRule:=wdRowHeightExactly
.Cell(Row:=rowCount, Column:=1).SetWidth ColumnWidth:=InchesToPoints(0.75), RulerStyle:=wdAdjustNone
.Cell(Row:=rowCount, Column:=2).SetWidth ColumnWidth:=InchesToPoints(2.08), RulerStyle:=wdAdjustNone
.Cell(Row:=rowCount, Column:=3).SetWidth ColumnWidth:=InchesToPoints(1), RulerStyle:=wdAdjustNone
.Cell(Row:=rowCount, Column:=4).SetWidth ColumnWidth:=InchesToPoints(2), RulerStyle:=wdAdjustNone
.Cell(Row:=rowCount, Column:=5).SetWidth ColumnWidth:=InchesToPoints(1.85), RulerStyle:=wdAdjustNone
.Cell(rowCount, 1).Range.InsertAfter "Name:"
.Cell(rowCount, 3).Range.InsertAfter "Type:"
.Cell(rowCount, 2).Range.InlineShapes.AddOLEControl ClassType:="Forms.TextBox.1"
Set myCB = .Cell(rowCount, 4).Range.InlineShapes.AddOLEControl(ClassType:="Forms.TextBox.1")
Dim uofCode As String
Dim doc As Word.Document
Set doc = ActiveDocument
theName = myCB.OLEFormat.Object.Name
MsgBox theName ‘this message works fine
uofCode = "Private Sub " & myCB.OLEFormat.Object.Name & "_GotFocus()" & vbCrLf & _
vbCr & "Load uofForm" & vbCr & "uofForm.Tag = theName" & vbCr & "uofForm.Show" & vbCr & vbCrLf & _
"End Sub"
doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString uofCode
End With
チェックされているとき、私はこの行theName = myCB.OLEFormat.Object.Name
で変数theName
を設定する変数を設定し、 MsgBoxでテストするように設定されていることを確認してくださいMsgBox theName
このメッセージはうまくいきます。今問題は、変数が空である関数を生成するときです。変数theName
が設定されていない理由は何ですか?
あなたの '&" uofForm.Tag = theName "&'は '&" uofForm.Tag = "" "&myCB.OLEFormat.Object.Name&" "" "を達成するちょうど複雑な方法であると感じています。 & '。 (私の推測では、あなたの変数が設定されていない理由は、コードをいくつか変更して変数をリセットするということです。コードの追加が原因である可能性があります。コードを更新して何が起こるかを確認してください) – YowE3K