2009-07-13 27 views
0

Wordマクロでどのボタンが押されたかを簡単に判断できる方法があるのでしょうか?私はすべてのマクロを起動する必要がありますいくつかのボタンとドキュメントテンプレートを持っています。 問題は、私は各ボタンで呼び出される1つのマクロを作成したいと思います。私は、各ボタンにたくさんのマクロを必要としません。どのボタンが押されたかを確認しますか?

このマクロは、ボタンを押すと画像が挿入され、この画像のサイズはボタンのサイズに基づいて選択されます。意味、これは画像のプレースホルダーとして終わります。しかし、マクロを動的に記述して、マクロを呼び出すだけではなく、同じコードが各ボタンで機能するようにしたいと思います。

マクロはすでに完成しています。これを達成する方法について誰かが知っていれば、この最後のことを知る必要がありますか? :)あらかじめThanx!

UPDATE:この瞬間のコードである

ます。Private Sub ImageButton1_Click() PicturePlaceholder ImageButton1 End Subの

ます。Private Sub ImageButton2_Click() PicturePlaceholder ImageButton2 End Subの

Public Sub PicturePlaceholder(ByVal oButton As CommandButton)  
Dim oShape As Word.Shape 
Dim Dlg As Office.FileDialog 
Dim strFilePath As String 
Dim oDoc As Document 
Dim rgePlace As Range 
Dim buttonHeight As String 
Dim buttonWidth As String 

Set Dlg = Application.FileDialog(msoFileDialogFilePicker) 
Set oDoc = ActiveDocument 


Set rgePlace = Selection.Range.Fields(1) _ 
.Result.Paragraphs(1).Range 

Response = MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?") 
If Response = vbYes Then rgePlace.Fields(1).Delete 
If Response = vbCancel Then Exit Sub 
If Response = vbNo Then 

With Dlg 
.AllowMultiSelect = False 
If .Show() <> 0 Then 
strFilePath = .SelectedItems(1) 
End If 
End With 

If strFilePath = "" Then Exit Sub 
Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath, _ 
LinkToFile:=False, SaveWithDocument:=True, _ 
Anchor:=rgePlace) 
With oShape 
.Height = oButton.Height 
.Width = oButton.Width 
End With 

rgePlace.Fields(1).Delete 


End If 
End Sub 

答えて

3

OKですので、ドキュメントにはCommandButtonがあります。

この場合、何もできません。Button1_ClickButton2_Clickなど(またはボタン名が何であれ)ハンドラが必要です。

ただし、このような何かを行うことができます。

Private Sub Button1_Click(...) 
    DoStuff Button1 
End Sub 

Private Sub Button2_Click(...) 
    DoStuff Button2 
End Sub 

Private Sub DoStuff(ByVal oButton As CommandButton) 
    ' All your shared code goes here 
    MsgBox oButton.Caption 
End Sub 

は、コード内であなたのボタンを作成する方法のためにもthis tech note参照してください。


EDIT:共有機能は、ボタンのプロパティにアクセスできるようにCommandButton参照を渡すように更新。


EDIT 2:InlineShapesを使用して完全なコードを表示するように更新。ボタンの幅/高さはフィールドから直接取得できるため、これはButtonオブジェクトにはもう渡らないことに注意してください。

Private Sub CommandButton1_Click() 
    PicturePlaceholder 
End Sub 

Private Sub CommandButton2_Click() 
    PicturePlaceholder 
End Sub 

Public Sub PicturePlaceholder() 

    ' Get the selected field, which must be a button field 

    Dim oField As Field 
    Set oField = Selection.Fields(1) 

    Debug.Assert oField.Type = wdFieldOCX 


    ' Ask the user what he wants to do 

    Select Case MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?") 

     Case vbCancel 
      Exit Sub 

     Case vbYes 
      oField.Delete 
      Exit Sub 

    End Select 


    ' Get the filename of the picture to be inserted 

    Dim strFilePath As String 

    With Application.FileDialog(msoFileDialogFilePicker) 

     .AllowMultiSelect = False 

     If .Show() <> 0 Then 
      strFilePath = .SelectedItems(1) 
     End If 

    End With 

    If strFilePath = "" Then 
     Exit Sub 
    End If 


    ' Figure out where to insert the picture, and what size to make it 

    Dim oRange As Range 
    Set oRange = oField.Result 

    Dim sglWidth As Single 
    sglWidth = oField.InlineShape.Width ' oButton.Width 

    Dim sglHeight As Single 
    sglHeight = oField.InlineShape.Height ' oButton.Height 


    ' Delete the button field 

    oField.Delete 


    ' Insert and resize the picture 

    Dim oInlineShape As Word.InlineShape 
    Set oInlineShape = oRange.InlineShapes.AddPicture(FileName:=strFilePath, LinkToFile:=False, SaveWithDocument:=True, Range:=oRange) 

    With oInlineShape 
     .Width = sglWidth 
     .Height = sglHeight 
    End With 

End Sub 

EDIT 3:シェイプではなくInlineShapesを使用することが要求されるように更新しました。(CommandButtonと挿入された図形は両方とも図形になりました)。


Private Sub CommandButton1_Click() 
    PicturePlaceholder 
End Sub 

Private Sub CommandButton2_Click() 
    PicturePlaceholder 
End Sub 

Public Sub PicturePlaceholder() 

    ' Get the selected shape, which must be a button shape 

    Debug.Assert Selection.Type = wdSelectionShape 

    Dim oButtonShape As Shape 
    Set oButtonShape = Selection.ShapeRange(1) 


    ' Ask the user what he wants to do 

    Select Case MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?") 

     Case vbCancel 
      Exit Sub 

     Case vbYes 
      oButtonShape.Delete 
      Exit Sub 

    End Select 


    ' Get the filename of the picture to be inserted 

    Dim strFilePath As String 

    With Application.FileDialog(msoFileDialogFilePicker) 

     .AllowMultiSelect = False 

     If .Show() <> 0 Then 
      strFilePath = .SelectedItems(1) 
     End If 

    End With 

    If strFilePath = "" Then 
     Exit Sub 
    End If 


    ' Insert the picture at the same size/position 

    Dim oPictureShape As Shape 
    Set oPictureShape = ActiveDocument.Shapes.AddPicture _ 
     (_ 
     FileName:=strFilePath, _ 
     LinkToFile:=False, _ 
     SaveWithDocument:=True, _ 
     Left:=oButtonShape.Left, _ 
     Top:=oButtonShape.Top, _ 
     Width:=oButtonShape.Width, _ 
     Height:=oButtonShape.Height, _ 
     Anchor:=oButtonShape.Anchor _ 
     ) 


    ' Copy across the button shape formatting 

    oButtonShape.PickUp 
    oPictureShape.Apply 


    ' Copy across other layout details 

    oPictureShape.LayoutInCell = oButtonShape.LayoutInCell 

    oPictureShape.LockAnchor = oButtonShape.LockAnchor 

    oPictureShape.RelativeHorizontalPosition = oButtonShape.RelativeHorizontalPosition 
    oPictureShape.RelativeVerticalPosition = oButtonShape.RelativeVerticalPosition 

    oPictureShape.WrapFormat.Type = oButtonShape.WrapFormat.Type 
    oPictureShape.WrapFormat.Side = oButtonShape.WrapFormat.Side 
    oPictureShape.WrapFormat.DistanceTop = oButtonShape.WrapFormat.DistanceTop 
    oPictureShape.WrapFormat.DistanceLeft = oButtonShape.WrapFormat.DistanceLeft 
    oPictureShape.WrapFormat.DistanceBottom = oButtonShape.WrapFormat.DistanceBottom 
    oPictureShape.WrapFormat.DistanceRight = oButtonShape.WrapFormat.DistanceRight 
    oPictureShape.WrapFormat.AllowOverlap = oButtonShape.WrapFormat.AllowOverlap 


    ' Delete the button shape 

    oButtonShape.Delete 

End Sub 
+0

ありがとうございました! :)しかし、私はどのようにボタンの名前を共有コードから抽出するのですか?たとえば、私が必要とするのは、クリックされたボタンの高さと幅を抽出することだけです –

+0

ボタンを共有関数のパラメータとして渡す方法を示す答えを編集し、そのプロパティにアクセスしました。 –

+0

うわー、これはかなりうまくいく! :) 最初の投稿で更新したコードを簡単に見てみることができますか? 私のコードでは、oShapesとして写真を挿入しているので、より簡単に移動できます。しかし、ボタンは実際にはインラインであるため、画像の位置はボタンの正確な位置になります。 ボタンをちょっと形にするためにコードを少し変更することはできますか?コードを試してみると、それは動作しますが、画像は削除される前のボタンのように、oShapeで、inLineのシェイプではないため、ドキュメントのテキストの上に表示されます。 –

0

あなたは別のサブにあなたの基本マクロを配置し、各ボタンのクリックからマクロを呼び出すことができますパラメータとして所望のサイズを渡す。次に、ボタンの中にある唯一のコードは、ベースサブの呼び出しです。

1

ボタンがコマンドバーボタン(別名ツールバーボタン)であると仮定します。

もしそうなら、Application.CommandBars.ActionControlを使用して、クリックされたボタンへの参照を取得できます。そこから、キャプションやタグなどを調べることができます。

+0

こんにちは、[OK]をので、私はこれを試してみましたが、それは動作していないようです。 私は戻って、マクロから任意の情報を取得しなかった理由私は理解していなかったので、私はこのコードを試してみました: 公開サブPicturePlaceholder() Application.CommandBars.ActionControlは何もしない場合は次に MSGBOX「マクロが開始されませんでしたボタンから」、そうでないvbInformation MSGBOX 『マクロ』のボタンから開始された、vbInformation&Application.CommandBars.ActionControl.Caption エンド 場合そして私は、ドキュメント内の各ボタンから、このマクロを呼ばれます。しかし、それでもActionControlはNothingだと思われます。どうしてこれなの?ああ、これらはドキュメントのボタンです。 –

+1

意味、これらはツールバーボタンではなく、デザインモードで挿入されたコマンドボタンです。 –

0

あなたはそれが引数を取るマクロを呼び出すときに引数を渡すためにVBAでボタンを持つことができます。 例:function test(xは文字列) と呼ばれる関数がある場合、マクロを呼び出すボタンの構文はonclick( "sheetx!test"、 "whatever")となります。そうすることで、呼び出される汎用マクロを持つことができます。これがあなたを助けることを願ってください。私のためのActiveXボタンやテキストボックスの作品の様々なクリック/ got_focus /変更イベントに次のように配置

0

MsgBox ThisDocument.ActiveWindow.Selection.Fields.Item(1).OLEFormat.Object.Name 
関連する問題