2016-06-18 7 views
0

私は単純なテンプレート(Open word file--> Alt + F11 --> Save file as .dtom)を作成して、選択したテキストにコメントを追加しています。私は.dotmとしてファイルを保存してスタートアップフォルダに配置しますC:\Users\abc\AppData\Roaming\Microsoft\Word\STARTUPしかし、私はMs Word 2013のマクロ設定のエラーを受けています。Ms Word 2013でVBAを使用して選択したテキストにコメントを追加するにはどうすればよいですか?

私のコードを添付しました。私がコード側から何かを見逃していると誰も示唆できますか?

コード:

Sub autoexe() 
    Dim MainMenu As CommandBarControl 
    Dim MenuItem As CommandBarPopup 
    'add pop button 
    MenuItem = MainMenu.Controls.Add(msoControlPopup, , , , True) 
    With MenuItem 
     .Caption = "Item1" 
     .Visible = True 
     'add simple button 
     Dim simpleButton As CommandBarButton 
     Dim commentText As String 
     commentText = "Comment inserted successfully" 
     simpleButton = MenuItem.Controls.Add(msoControlButton, , , , True) 
     With simpleButton 
      .Caption = "Show Message" 
      .Visible = True 
      .OnAction = "addComments(commentText)" 
     End With 
    End With 
End Sub 


Sub addComments(ByVal cmtText As String) 
    ActiveWindow.View.Type = wdPageView 
    Selection.Comments.Add Range:=Selection.Range 
    If (Len(Selection) > 0) Then 
     MsgBox ("inside comment") 
     With Selection 
      .TypeText (cmtText) 
     End With 
    End If 
End Sub 

enter image description here

+0

ファイルを開くときに 'Enabled Contents'を使いましたか?あなたのファイルにコードがありますか? – newguy

+0

.dotmファイル拡張子ですか? – dbmitch

+0

@newguy申し訳ありませんが、あなたの質問を理解できませんでした。説明していただけますか? Wordが起動すると、アドインが表示されます。私はボタンを見ることができます。クリックすると、メッセージの上にスローされます。私は 'Enabled Contents'のコードを設定していません。そのコードの例やリンクを教えてください。 –

答えて

1

あなたはそれをあなたが今セットアップしている方法を使用する場合は、複数を使用し続けることができますグローバル変数

Dim commentText As String 
Dim param2 As String 
Dim param3 As String 

commentText = "Comment inserted successfully" 
param2 = "This is parameter 2" 
param3 = "This is parameter 3" 
simpleButton = MenuItem.Controls.Add(msoControlButton, , , , True) 
With simpleButton 
    .Caption = "Show Message" 
    .Visible = True 
    .OnAction = "addComments()" 
End With 

Sub addComments() 
    commentText = Application.CommandBars.ActionControl.Parameter 
    ActiveWindow.View.Type = wdPageView 
    Selection.Comments.Add Range:=Selection.Range 
    If (Len(Selection) > 0) Then 
     MsgBox ("inside comment") 
     With Selection 
      .TypeText (commentText) 
     End With 
    End If 
    Msgbox "Param 2: " & param2 
    Msgbox "Param 3: " & param3 
End Sub 
+0

ええ、私もこれを使うことができました。ありがとうございました。 –

0

あなたが試してみました:

  1. は、[ファイル]タブをクリックして[オプション]をクリックし、[セキュリティセンター]をクリックし、[セキュリティセンターの設定]をクリックします。

  2. [マクロ設定]をクリックします。

  3. マクロの設定で、[すべてのマクロを有効にする]をクリックします。

あなたは、信頼できる場所にC:\Users\abc\AppData\Roaming\Microsoft\Word\STARTUPを追加することはできますか?

+0

私はこれを行い、アプリケーションを再起動しました。まだ動作していません –

+0

信頼できる場所に 'C:\ Users \ abc \ AppData \ Roaming \ Microsoft \ Word \ STARTUP'を追加できますか? – dbmitch

+0

はい。パスは既に[信頼できる場所]リストにあります。 –

0

解決されました。私はちょうどボタンクリックで関数のパラメータを渡す方法を変更します。私はなぜそれがエラーを生成していなかったのか分からないが、それはただ一つの変更によって行われた。ここで

は、コードを作業されています(私はまだ複数のパラメータを渡す方法がわからない)

Dim commentText As String 
     commentText = "Comment inserted successfully" 
     simpleButton = MenuItem.Controls.Add(msoControlButton, , , , True) 
     With simpleButton 
      .Caption = "Show Message" 
      .Visible = True 
      .OnAction = "addComments()" 
      .Parameter = commentText 
     End With 

Sub addComments() 
    Dim commentText As String 
    commentText = Application.CommandBars.ActionControl.Parameter 
    ActiveWindow.View.Type = wdPageView 
    Selection.Comments.Add Range:=Selection.Range 
    If (Len(Selection) > 0) Then 
     MsgBox ("inside comment") 
     With Selection 
      .TypeText (commentText) 
     End With 
    End If 
End Sub 
+0

あなたが使用する必要があるようにも見えません。パラメータ - commentTextはグローバルモジュール変数からaddCommentsで取得されているだけです。 – dbmitch

関連する問題