Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Aug 2013
    Posts
    46
    Thanks
    11
    Thanked 0 Times in 0 Posts

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

    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?

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    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.

  3. #3
    Lounger
    Join Date
    Aug 2013
    Posts
    46
    Thanks
    11
    Thanked 0 Times in 0 Posts
    thanks, but I need it as a push button, not as an input text.

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Lounger
    Join Date
    Aug 2013
    Posts
    46
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Andrew Lockton View Post
    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.

  6. #6
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Quote Originally Posted by YigalB View Post
    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.

  7. #7
    Lounger
    Join Date
    Aug 2013
    Posts
    46
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jjfreedman View Post
    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

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •