Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-07-2017, 09:55 AM
DeductiveBear23 DeductiveBear23 is offline New to VBA Windows 10 New to VBA Office 2016
Novice
New to VBA
 
Join Date: Apr 2017
Posts: 4
DeductiveBear23 is on a distinguished road
Unhappy New to VBA

Hello All,



Very new to the entire world of VBA and Macros. I started with a want to add a random password generator to Word document. Some other people wanted the same thing and they were provided with some code that works exactly how I would like to it to.
This being said, I would like the randomized string of characters to appear in a table titled 'Password' instead of the message box. The code I am using is:
Code:
Sub PASSWORD_GENERATOR()
MsgBox Pwd(8)
End Sub
 
Function Pwd(iLength As Integer) As String
Dim i As Integer, iTemp As Integer, bOK As Boolean, strTemp As String
'48-57 = 0 To 9, 65-90 = A To Z, 97-122 = a To z
'amend For other characters If required
For i = 1 To iLength
  Do
    iTemp = Int((122 - 48 + 1) * Rnd + 48)
    Select Case iTemp
      Case 48 To 57, 65 To 90, 97 To 122: bOK = True
      Case Else: bOK = False
    End Select
  Loop Until bOK = True
  bOK = False
  strTemp = strTemp & Chr(iTemp)
Next i
Pwd = strTemp
End Function
I was able to produce the desired result in an Excel cell by changing the second last line where 'Pwd = strTemp' to ActiveSheet.Range("C2").Formula = pw (C2 being any cell I need it to be). I have tried "ActivTable.Range ("Password") Formula = pw" but I always get a 424 error and am not sure what or where to place the missing 'Object'.

As always - Any and all assistance is appreciated - Thank you

Last edited by macropod; 05-02-2017 at 03:56 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 04-07-2017, 02:46 PM
dwirony dwirony is offline New to VBA Windows 7 64bit New to VBA Office 2003
Advanced Beginner
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

Instead of "MsgBox Pwd(8), try:

Code:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
        1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Selection.TypeText Text:="Password"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:=Pwd(8)
Reply With Quote
  #3  
Old 04-11-2017, 05:02 AM
DeductiveBear23 DeductiveBear23 is offline New to VBA Windows 10 New to VBA Office 2016
Novice
New to VBA
 
Join Date: Apr 2017
Posts: 4
DeductiveBear23 is on a distinguished road
Smile I will Give it a try

Thank you DW. I will try to make time later today and try your code and will post back to let you know how I make out.

Regards,
Reply With Quote
  #4  
Old 04-13-2017, 08:44 AM
DeductiveBear23 DeductiveBear23 is offline New to VBA Windows 10 New to VBA Office 2016
Novice
New to VBA
 
Join Date: Apr 2017
Posts: 4
DeductiveBear23 is on a distinguished road
Default New Code Error Received

Hi DW,

I have finally found a minute to try the code you suggested and I have developed a compile error. "Invalid outside Procedure". Do you have any suggestions?
Reply With Quote
  #5  
Old 04-14-2017, 10:06 AM
dwirony dwirony is offline New to VBA Windows 7 64bit New to VBA Office 2003
Advanced Beginner
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

Strange, worked just fine for me. Are you using the whole new block of code? It still needs the function:

Code:
Sub PASSWORD_GENERATOR()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
        1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Selection.TypeText Text:="Password"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:=Pwd(8)
End Sub

Function Pwd(iLength As Integer) As String
Dim i As Integer, iTemp As Integer, bOK As Boolean, strTemp As String
'48-57 = 0 To 9, 65-90 = A To Z, 97-122 = a To z
'amend For other characters If required
For i = 1 To iLength
Do
iTemp = Int((122 - 48 + 1) * Rnd + 48)
Select Case iTemp
Case 48 To 57, 65 To 90, 97 To 122: bOK = True
Case Else: bOK = False
End Select
Loop Until bOK = True
bOK = False
strTemp = strTemp & Chr(iTemp)
Next i
Pwd = strTemp
End Function
Reply With Quote
  #6  
Old 05-02-2017, 08:01 AM
DeductiveBear23 DeductiveBear23 is offline New to VBA Windows 10 New to VBA Office 2016
Novice
New to VBA
 
Join Date: Apr 2017
Posts: 4
DeductiveBear23 is on a distinguished road
Smile It Works!

Hi DW,

Sorry for the very late reply. I have been nursing a cold and trying to deploy a bunch of machines. I have finally found time to re-visit this issue and the code you provided works great! Thank you very much.
Reply With Quote
  #7  
Old 05-02-2017, 08:09 AM
dwirony dwirony is offline New to VBA Windows 7 64bit New to VBA Office 2003
Advanced Beginner
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

No problem, DeductiveBear23. Feel better soon-
Reply With Quote
Reply

Tags
msg box, table, vba in microsoft word



Other Forums: Access Forums

All times are GMT -7. The time now is 06:29 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft