尝试将 contentcontrol 标签/标题作为 VBA 函数的参数传递。

huangapple go评论69阅读模式
英文:

Trying to pass a contentcontrol tag/title as argument in a VBA function

问题

I understand you'd like a Chinese translation for the provided content. Here it is:

我正在尝试创建一个函数,通过按下按钮将内容控件框的内容放入剪贴板。

到目前为止,我成功地手动实现了这一点,通过为每个按钮创建一个子程序,将按钮与标题对应的内容控件文本框连接起来。请参见下面的代码:

```vba
Private Sub DemandeAvec_Click()
    Dim obj As New DataObject
    Role = ActiveDocument.SelectContentControlsByTitle("Rôle")(1).Range.Text
    PusleDemande = ActiveDocument.SelectContentControlsByTitle("Demande")(1).Range.Text
    obj.SetText Role & vbNewLine & vbNewLine & PusleDemande
    obj.PutInClipboard
End Sub

问题在于,我正在尝试通过创建一个函数来自动化它,该函数在按下任何按钮时调用,通过将内容控件框的名称作为参数传递给函数,并使用其标题作为函数中的参数。

在下面的代码中,涉及到的参数名为“PulseName”。

Function ClicBoutonRole(PulseName As String) As String
    Dim result As New DataObject
    Dim PulseFinder
    Role = ActiveDocument.SelectContentControlsByTitle("Rôle")(1).Range.Text
    PulseContent = ActiveDocument.SelectContentControlsByTitle("**PulseName**")(1).Range.Text
    result.SetText Role & vbNewLine & vbNewLine & PulseContent
    result.PutInClipboard
End Function

'名为ModificationsSCAvec_Click的按钮

Sub ModificationsSCAvec_Click()
    Dim PulseName As String
    PulseName = ModificationsSC 'ModificationsSC是我尝试连接到按钮的内容控件框的标题
    ClicBoutonRole (PulseName) '通过以PulseName作为函数参数来调用函数,试图传递内容控件框的标签
End Sub

我必须说我对VBA非常陌生,所以我完全意识到这段代码在许多方面都是错误的。

每次我点击按钮时,Word都会给我返回代码5941,集合的成员不存在。

如果我的英文表达不够完美,我很抱歉,因为我不是以英语为母语。

我的问题是:我漏掉了什么?


<details>
<summary>英文:</summary>

I am trying to create a function that put a contentcontrol box content&#39;s into clipboard by pressing a button.




So far I managed to do it manually by creating a sub for each button that ties the button to a contentcontrol text box by title. See code below


Private Sub DemandeAvec_Click()
Dim obj As New DataObject
Role = ActiveDocument.SelectContentControlsByTitle("Rôle")(1).Range.Text
PusleDemande = ActiveDocument.SelectContentControlsByTitle("Demande")(1).Range.Text
obj.SetText Role & vbNewLine & vbNewLine & PusleDemande
obj.PutInClipboard

            End Sub


The problem is that i&#39;m trying to automate it by creating a function that is called when pressing any button, by passing the name of the contentcontrol box it is tied to by using its title as an argument in the function. 

In the code below, the argument in question in named &quot;PulseName&quot;.


Function ClicBoutonRole(PulseName As String) As String
Dim result As New DataObject
Dim PulseFinder
Role = ActiveDocument.SelectContentControlsByTitle("Rôle")(1).Range.Text
PulseContent = ActiveDocument.SelectContentControlsByTitle("PulseName")(1).Range.Text
result.SetText Role & vbNewLine & vbNewLine & PulseContent
result.PutInClipboard

End Function

*Button named ModificationsSCAvec_Click

Sub ModificationsSCAvec_Click()
Dim PulseName As String
PulseName = ModificationsSC
*ModificationsSC being the title of the content control box I'm trying to link to the button
ClicBoutonRole (PulseName)

*Trying to pass the tag of the contentcontrolbox by calling the function with PulseName as the argument of the function.

End Sub

I got to say I&#39;m really new to VBA, so I&#39;m fully aware this code is wrong in many ways.

Each time I click the button, word gives me the code 5941, member of collection doesn&#39;t exist.

I&#39;m sorry if my english is not perfect, as I&#39;m not a native english speaker. 

My question is the following ? What am I missing ?

</details>


# 答案1
**得分**: 0

I'm afraid you have several problems with your code, but you are getting error #5941 because the line `ActiveDocument.SelectContentControlsByTitle("PulseName")(1).Range.Text` does not find a ContentControl.

You need to pass the parameter `PulseName` to `SelectContentControlsByTitle`, now you are passing the string `"PulseName"`.

Also, in the code that calls `ClicBoutonRole` you need to pass the string with the title of the ContentControl, now you are passing the ContentControl-object itself.

Final tip, put `Option Explicit` at the top of your code module, and the compiler will not let you run code without making sure all the variable names are correct, you have a few of those errors in there as well.

Try something like this:

```vba
Option Explicit

Function ClicBoutonRole(PulseName As String) As String
    Dim result As New DataObject
    Dim Role As String
    Dim PulseContent As String
    
    Role = ActiveDocument.SelectContentControlsByTitle("Rôle")(1).Range.Text
    PulseContent = ActiveDocument.SelectContentControlsByTitle(PulseName)(1).Range.Text
    result.SetText Role & vbNewLine & vbNewLine & PulseContent
    result.PutInClipboard
End Function


Sub ModificationsSCAvec_Click()
    ClicBoutonRole("ModificationsSC")
End Sub

(Note: I've removed the HTML encoding &quot; to make the code more readable.)

英文:

I'm afraid you have several problems with your code, but you are getting error #5941 because the line ActiveDocument.SelectContentControlsByTitle(&quot;**PulseName**&quot;)(1).Range.Text does not find a ContentControl.

You need to pass the parameter PulseName to SelectContentControlsByTitle, now you are passing the string &quot;**PulseName**&quot;.

Also, in the code that calls ClicBoutonRole you need to pass the string with the title of the ContentControl, now you are passing the ContentControl-object itself.

Final tip, put Option Explicit at the top of your code module, and the compiler will not let you run code without making sure all the variable names are correct, you have a few of those errors in there as well.

Try something like this:

Option Explicit

Function ClicBoutonRole(PulseName As String) As String
    Dim result As New DataObject
    Dim Role As String
    Dim PulseContent As String
    
    Role = ActiveDocument.SelectContentControlsByTitle(&quot;R&#244;le&quot;)(1).Range.Text
    PulseContent = ActiveDocument.SelectContentControlsByTitle(PulseName)(1).Range.Text
    result.SetText Role &amp; vbNewLine &amp; vbNewLine &amp; PulseContent
    result.PutInClipboard
End Function


Sub ModificationsSCAvec_Click()
    ClicBoutonRole (&quot;ModificationsSC&quot;)
End Sub

huangapple
  • 本文由 发表于 2023年6月27日 17:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定