Completing VBA Userform textboxes from Excel Cell - vba

I'm trying to fill in a userform ("Userform1") textboxes ("SecurityTextBox", "VersionTextbox") from an Excel Sheet ("Version"). I've tried to look up what's going wrong but I haven't managed to figure it out.
Private Sub UserForm1_Initialize()
Dim ws As Worksheet
Set ws = Worksheets("Version")
SecurityTextBox.Text = ws.Cells.Range("C8").Value
VersionTextbox.Text = ws.Cells.Range("C13").Value
DeveloperTextBox.Text = ws.Cells.Range("C14").Value
End Sub
The trouble is the text boxes are just appearing blank.

The _Initialize handler is invoked when the class/form instance is created. If you're showing the form like this:
UserForm1.Show
Then it might work as expected if that's the only thing you're ever doing with the form. Problem is, it's the form's default instance, and you don't quite control when VBA is going to initialize that global instance.
Take control.
With New UserForm1 'initializes a new instance of the class
.Show
End With
Now the _Initialize handler with systematically run every time, because it's a New instance every time. See my UserForm1.Show article for more pitfalls of using forms' default instances.

Related

Deleting specific ActiveX Objects in MS Word using VBA without affecting other buttons

I am trying to delete two specific objects, an combobox named ComboBox1 and a button named btnselect.
The code would run when clicking the btnselect.
The code that I've written is this one:
Dim obj1 As Object
For Each obj1 In ActiveDocument.InlineShapes
If obj1.OLEFormat.Object.Name = "ComboBox1" Then
obj1.Delete
End If
Next obj1
Dim obj2 As Object
For Each obj2 In ActiveDocument.InlineShapes
If obj2.OLEFormat.Object.Name = "btnselect" Then
obj2.Delete
End If
Next obj2
Also, I have tried the following code:
ActiveDocument.InlineShapes(1).Delete
ActiveDocument.InlineShapes(1).Delete
Both the code work and the specified ActiveX objects are deleted.
However, after running this code, the other buttons that have vba code attached to them would not work.
To be more specific, the problem occurs only when a create new document based on the template. If I run the buttons directly from the template, there is no problem. Without these codes, the buttons work as intended in a new document based on the template.
Could you help me find a way to delete these two objects without interfering with the other objects?
LATER EDIT: I narrowed the code down as much as I could so that the problem still persists.
Private Sub btnselect_Click()
ActiveDocument.InlineShapes(1).Delete
End Sub
Private Sub btnSubmit_Click()
MsgBox "Working"
End Sub
Private Sub btnPrint_Click()
MsgBox "Working"
End Sub
The template has three buttons, btnselect, btnSubmit and btnPrint and it contains the above-mentioned VBA code.
I want to delete the btnselect in a new document that's based on the template.
If I create a new document based on the template and click the btnselect, the button gets deleted, but then the other two buttons aren't working anymore.
If I open the template and click the btnselect, the button gets deleted and the other two buttons are working (they are displaying the messages).
To keep the other buttons working, don't delete any button. If you delete it, all other buttons from your ActiveDocument loose their reference to their VBA-code within your template.
Unfortunately you can not simply hide command buttons, but you may disable them and set their size to 1x1 points:
With ActiveDocument.InlineShapes(1)
.OLEFormat.Object.Enabled = False
.Width = 1
.Height = 1
End With

VBA Word Macro to Allow User to Select and Copy Text Multiple Times

I am working on a VBA script with Microsoft Word that allows the user to select text that will be be copied to the clipboard so that it can exported to an Excel file. The user will make a number of selections and finally indicate he/she is done when the contents of the clipboard will be copied to a template Excel file.
There are two forms: the first (UserForm1 in the code below) queries the user for the Word filename. The filename variable is passed to the second form. The second form (frmModeLessForInput) is a modeless form. The behavior I need is that program control goes to the second form with two buttons "Continue" and "Done".
The user is allowed to navigate the document and place the cursor anywhere in the document. Then when "Continue" is pressed the form will call a subroutine (Highlight_Sentence) to copy the selected text to a "clipboard" variable. When "Done" is pressed control will be passed to called main module which will then copy the clipboard to the Excel file.
Below is the code. I have noted with comments where I am trouble with the code. One problem is the variables defined as Public in the ThisDocument module are not defined in the userforms and their subroutines.
The second problem is in the main module that the frmModelessForInput is supposed to be displayed and control is not supposed to be transferred to next statement {MsgBox "Sentences will now be copied to Excel file"....this is where I will put the code to copy the clipboard to the Excel file.} but the message appears before the frmModelessForInput form is run...thus the clipboard will be empty.
The third problem is that in frmModelessForInput form the statement str_clipboard = str_clipboard + str_clipboard_line is not working. Each time the "Continue" button is pushed str_clipboard loses it previous contents.
Any assistance in resolving these problems is appreciated. As VBA programming is a sideline for me I am still learning.
Note this an updated question Pause VBA Word macro, allow user to make a selection, and restart where it left off adding some more detail on the requirement and the sample code.
MAIN MODULE:
Option Explicit
Public str_clipboard As String
Public txt_active_document As String
Public i_how_many_sentences As Integer
Private Sub Test_master_macro()
UserForm1.Show
i_how_many_sentences = 0
Call DisplayModeless
MsgBox "Sentences will now be copied to Excel file" 'Problem: this msg displays before the frmModelessForInput is displayed
End Sub
Sub DisplayModeless()
Dim frm As frmModelessForInput
Set frm = New frmModelessForInput
With frmModelessForInput
.str_word_doc_filename = txt_active_document
.str_no_copied = "0"
.Show False
End With
Set frm = Nothing
End Sub
USERFORM1: form has field for user entering the document filename to user (str_filename) and a command button to close form (cmd_start_selecting_text)
Private Sub cmd_start_selecting_text_Click()
'User enters filename on form for use in frmModelessForInput subroutine
txt_active_document = UserForm1.str_filename 'Problem: VBA reports txt_active_document as undefined even though it is a Public variable
Unload Me
End Sub
FRMMODELESSFORINPUT: Form displays filename of Word file entered in UserForm1 and how many sentences have been copied to the clipboard
Option Explicit
Private Sub cmdContinue_Click()
Dim str_clipboard, str_clipboard_line As String
Call Highlight_Sentence(str_clipboard_line)
i_how_many_sentences = i_how_many_sentences + 1 'Problem: VBA reports i_how_many_sentences as undefined even though it is a Public variable
frmModelessForInput.str_no_copied = i_how_many_sentences 'Same Problem
str_clipboard = str_clipboard + str_clipboard_line 'Problem: each time I select a new text/sentence str_clipboard does not contain the contents of the previous selection
End Sub
Private Sub cmdDone_Click()
Me.Hide
End Sub
Private Sub UserForm_Activate()
'Position the form near the top-left of the window
'So that the user can work with the document
Me.Top = Application.ActiveWindow.Top + 15
Me.Left = Application.ActiveWindow.Left + 15
End Sub
Private Sub Highlight_Sentence(clipboard As String)
'This sub extends the selection to the entire sentence and copies the selection, the page number on which the selection is contained and the filename to the clipboard variable
Dim txt_sentence, txt_page_no As String
With Selection
' Collapse current selection.
.Collapse
' Expand selection to current sentence.
.Expand Unit:=wdSentence
End With
txt_sentence = Selection.Text
txt_page_no = Selection.Information(wdActiveEndPageNumber)
clipboard = txt_active_document & vbTab & txt_page_no & vbTab & txt_sentence & vbCrLf 'Problem: VBA reports txt_active_document as undefined even though it is a Public variable
End Sub
From what you stated you are running this from the ThisDocument Class Module and unless you fully qualify your references to those Public variables with the Class Name that is why you cannot access them from the UserForms Class Modules.
If you are going to leave your "Main Module" code in the ThisDocument Class Module then whenever you reference those Public variable you need to add ThisDocument.str_clipboard to the command.
I recommend however, to place your Main Module in a general Module such as NewModule and if you need to run it at a Document_Open event that you put a call to the Main Module and its Public variables in the Private Sub Document_Open event of the ThisDocument Class Module.
Your Msgbox is appearing at the wrong time because you are displaying a modeless user form, which means the VBA script thread continues to run after the UserForm is displayed. Move the Msgbox to the UserForm_Activate routine of the second UserForm you are displaying or move it to the Click_Done routine of the first UserForm before you Hide or Unload it.
Finally, you are not really using the Clipboard and using that term makes your code confusing in my opinion. I think you should rename it. Your code also appears to just be building one continuous string of text. Is that really what you want to do? Or, do you really mean to capture each selected text string and ultimately place each into separate cells within an Excel Worksheet? If that is the case, use an Array for the separate text strings.

Change print method from PDF to default printer for a form printout

Hi I am trying to get the print out of a userform. While printing its always prompting to save the userform (a prompt box is being displayed). I don't want it.
I want to get the printout directly after choosing the printer without saving it.
Code below:
Private Sub CommandButton2_Click()
CommandButton2.Visible = False
CommandButton1.Visible = False
Application.Dialogs(xlDialogPrinterSetup).Show
Me.PrintForm
CommandButton2.Visible = True
CommandButton1.Visible = True
End Sub
You may not like this answer, because it involves more workt, but you probably want the userform to add the data into a formatted worksheet and then use the printout method of the worksheet.
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/sheets-printout-method-excel
Worksheets.("yourWorksheet").printout _
activeprinter:= yourPDFprinter, _
PrintTofFile:= True, _
PrToFileName:= filePathAndName
If you want to do it through the UserForm, you're probably going to end up making a bunch of Windows API calls.
At the end of your code you can force a workbook to close without saving any changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
Because the Saved property is set to True, Excel responds as though the workbook has already been saved and no changes have occurred since that last save.
EDIT:
Now I know what you're after I am going to presume you've created a button press for your form, so I think something like https://www.mrexcel.com/forum/excel-questions/544657-specifying-printer-printing-userform-using-vba.html . This will help you with what you want, as it will show you how to change the default print method, form PDF.

VBA: Code not running after ToggleFormsDesign

I have the following code in VBA (MS Word), that is meant to run after I click in a button, named cmdFormPreencher inserted in my Document:
Private Sub cmdFormPreencher_Click()
'
If ActiveDocument.FormsDesign = False Then
ActiveDocument.ToggleFormsDesign
End If
'
ThisDocument.cmdFormPreencher.Select
ThisDocument.cmdFormPreencher.Delete
ActiveDocument.ToggleFormsDesign
'
UserForm2.Show
End Sub
The purpose of the code above is to delete that button inserted in my document.
But when I run the code only the button is selected. When I tried to figure out what is happening by debugging, it showed me the code runs until ActiveDocument.ToggleFormsDesign and not running the code remaining
Is this a bug of VBA, or am I doing something wrong? If so, how can I get around this problem?
Thanks!
Note: The ActiveX button is not in Header and Footer. The Text Wrap is set to In Front of Text
Edit:
When I try to run a macro, activating FormDesign, Selecting the ActiveX button and then deleting, I get this code:
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveDocument.ToggleFormsDesign
ActiveDocument.Shapes("Control 52").Select
Selection.ShapeRange.Delete
ActiveDocument.ToggleFormsDesign
End Sub
But when I run this code nothing happens...
This is by design. When an Office application is in Design Mode code should not run on an ActiveX object that's part of the document.
I take it this is an ActiveX button and in that case, it's a member of the InlineShapes or Shapes collection - Word handles it like a graphic object. It should be enough to delete the graphical representation, which you can do by changing it to display as an icon instead of a button.
For example, for an InlineShape:
Sub DeleteActiveX()
Dim ils As word.InlineShape
Set ils = ActiveDocument.InlineShapes(1)
ils.OLEFormat.DisplayAsIcon = True
ils.Delete
End Sub
You just have to figure out how to identify the InlineShape or Shape. You could bookmark an InlineShape; a Shape has a Name property.
EDIT: Since according to subsequent information provided in Comments you have a Shape object, rather than an InlineShape, the following approach should work:
Dim shp As word.Shape
Set shp = ActiveDocument.Shapes("Shape Name") 'Index value can also be used
shp.Delete
Note that Word will automatically assign something to the Shape.Name property, but in the case of ActiveX controls these names can change for apparently no reason. So if you identify a control using its name instead of the index value it's much better to assign a name yourself, which Word will not change "on a whim".
Activate Design Mode.
Click on the control to select it
Go to the VB Editor window
Ctrl+G to put the focus in the "Immediate Window"
Type the following (substituting the name you want), then press Enter to execute:
Selection.ShapeRange(1).Name = "Name to assign"
Use this Name in the code above

How to clear the VBA code of a worksheet via a macro?

I have a file where there's a template sheet that needs to run some code when it's activated. This sheet is being duplicated to create sheets that don't need to run this code. Currently, I have the code to check for worksheet's codename when run so that it does nothing on extra sheets, but it still slows usage down when you switch between sheets.
Is there any way to make the macro that makes duplicates also clear their VBA code contents?
(Edit) Please note that the code I need to clear is not in a module. After some research, it seems I found a way to remove modules (by accessing VBProject.VBComponents), but I'm not sure how to access the VBA code of a worksheet.
To remove complete code in all Sheet modules you could try something like this:
Sub Remove_some_vba_code()
Dim activeIDE As Object 'VBProject
Set activeIDE = ActiveWorkbook.VBProject
Dim Element As VBComponent
Dim LineCount As Integer
For Each Element In activeIDE.VBComponents
If Left(Element.Name, 5) = "Sheet" Then 'change name if necessary
LineCount = Element.CodeModule.CountOfLines
Element.CodeModule.DeleteLines 1, LineCount
End If
Next
End Sub
Another way you could approach this is to keep all of your code out of the worksheet. Then you don't have to delete anything. The worksheet's code module is a handy place to code events, but you can create your own class module to handle events too. Put this in a standard module:
Public gclsEvent As CEvent
Sub Auto_Open()
Set gclsEvent = New CEvent
Set gclsEvent.This = Sheet1
End Sub
This will create an instance of CEvent that's global, so it won't lose scope as long as your workbook is open. It assigns the worksheet codenamed Sheet1 to the This property of the class. Create a class module named CEvent with this code
Private WithEvents mwsThis As Worksheet
Public Property Set This(ByVal wsThis As Worksheet): Set mwsThis = wsThis: End Property
Public Property Get This() As Worksheet: Set This = mwsThis: End Property
Private Sub mwsThis_Activate()
Me.This.Copy , Me.This.Parent.Sheets(Me.This.Parent.Sheets.Count)
End Sub
The WithEvents keyword exposes events for that object. Since we're only hooking up the events for Sheet1, activating another sheet won't trigger the code.