我正在尝试在 Word 中使用在 Excel 中定义的变量。我使用了两个单独的宏,但所有代码都是在 Excel 中编写的。我正在尝试从 Excel 控制 Word。
这是我在 Excel 宏中的代码:
Public DJ, Pick, Customer, Line, Model As Variant
Public LineOne, LineThr As Integer
Dim OP As String
Dim LastRow As Long
'Dims variables for pick stickers
Public Subinv, Loc, PN, Req, Desc As String
Public Sub Picklist()
在这个子程序中,它继续将 DJ 变量定义为一组随机的 8 个数字(即 94673920),它每次都会改变。然后我尝试在不同的子程序下传递 Word 中的该变量。以下是代码:
Public Sub PickLabel()
Dim WordApp, WordDoc As Object
Set WordApp = CreateObject("Word.Application") 'Opens up the Pick Stickers Generator in Word
Set WordDoc = WordApp.Documents.Add
WordApp.Visible = True
Dim WordSel
Set WordSel = WordApp.Selection
With WordSel
.Font.Size = 60
.Font.Name = "Arial"
.TypeText ("DJ: " & DJ)
.ParagraphFormat.Alignment = wdAlignCenter
End With
子目录结束
它没有像我希望的那样插入 DJ 变量。我做错了什么?
答案1
使用以下命令调用您的代码:
PickLabel(DJ)
Public Sub PickLabel(vRandom as long)
Dim WordApp, WordDoc As Object
Set WordApp = CreateObject("Word.Application") 'Opens up the Pick Stickers Generator in Word
Set WordDoc = WordApp.Documents.Add
WordApp.Visible = True
Dim WordSel
Set WordSel = WordApp.Selection
With WordSel
.Font.Size = 60
.Font.Name = "Arial"
.TypeText ("DJ: " & vRandom)
.ParagraphFormat.Alignment = wdAlignCenter
End With