• VBA word: Adding a dynamic CommandButton and assign it a macro

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VBA word: Adding a dynamic CommandButton and assign it a macro

    Author
    Topic
    #490616

    I have a working VBA code which creates a new word file, adding some text, drop-down lists, and few text box.

    I would like to add also a CommandButton named “create PDF” which will create a PDF out of this word document and will save it in current working directory.

    I couldn’t find a way to do that dynamically.

    Any idea?

    Viewing 3 reply threads
    Author
    Replies
    • #1408120

      Code to save the active document as a PDF would be something like this:

      Code:
      Sub CreatePDF()
          Dim fname As String
          fname = InputBox("Filename:")
          If StrPtr(fname) = 0 Then Exit Sub
          If Len(fname) = 0 Then Exit Sub
          ActiveDocument.SaveAs2 FileName:=fname, FileFormat:=wdFormatPDF
      End Sub
      

      If your macro already knows the filename, you can use that instead of the InputBox. The SaveAs2 method has other, optional arguments that you can specify (http://msdn.microsoft.com/en-us/library/ff836084.aspx).

      The CommandButton is another issue. If it’s an ActiveX button on the surface of the document, it’s going to appear in the PDF. If the code will be in a template, the better alternative is to save a Quick Access Toolbar button in the same template, assigned to the macro.

    • #1408128

      thanks, but I need it as a push button, not as an input text.

      • #1408301

        thanks, but I need it as a push button, not as an input text.

        That’s why I wrote “If your macro already knows the filename, you can use that instead of the InputBox.” Instead of asking for input, have the macro create the file name from the value of whatever field the user has filled with the necessary information. Sorry, I can’t be any more specific than that without seeing your document.

        • #1408347

          That’s why I wrote “If your macro already knows the filename, you can use that instead of the InputBox.” Instead of asking for input, have the macro create the file name from the value of whatever field the user has filled with the necessary information. Sorry, I can’t be any more specific than that without seeing your document.

          Your suggestion is good, but not suitable for my case, perhaps I didn’t explain it well:
          – My VBA code creates a WORD file, with few drop-down lists and text boxes.
          – the code needs to create a PushButton, which it’s name and action (once pushed) depends on pre-known run time information. (already known when creating the WORD file, but unknown at the coding stage, so it is dynamic)

          I would appreciate VBA example code that does the following:
          – Creates push button
          – Modifies it’s name (caption, right?)
          – Calls a procedure that messages “hello world” once the PushButton is pushed

    • #1408145

      It would actually be easier for you to create the button where you want it and attach the code to it directly. Then save the document as a template and use that template when you ‘create a new Word file’. This saves you a bunch of code and is far more creator-friendly.

      • #1408147

        It would actually be easier for you to create the button where you want it and attach the code to it directly. Then save the document as a template and use that template when you ‘create a new Word file’. This saves you a bunch of code and is far more creator-friendly.

        I know and agree with the concept, but in this specific case the button depends on previous input from the user, so it has to be dynamic.

    • #1408362

      I don’t know if a pushbutton even exists in Word so I’ll give you code to create a macrobutton instead which needs to be doubleclicked to run the macro

      Code:
      Sub AddAField()
        Dim rng As Range
        Set rng = ActiveDocument.Paragraphs(1).Range
        rng.Collapse wdCollapseStart
        ActiveDocument.Fields.Add Range:=ActiveDocument.Paragraphs(1).Range, Text:="Macrobutton HelloWorld Don't Touch Me", PreserveFormatting:=False
      End Sub
      Sub HelloWorld()
        MsgBox "Mum! That man just touched me"
      End Sub
    Viewing 3 reply threads
    Reply To: VBA word: Adding a dynamic CommandButton and assign it a macro

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: