VBA Excel Dynamic Form Creation - vba

Question: I have a form I created in Excel (Developer mode). The form has a MutliPage control on it. I am able to add a dynamically add new Page to the multipage control with VBA. I have no idea how to add any content onto this page that I have just created - for example adding a new checkbox, labels etc.
The only code I have at the moment is:
DataQueryForm.DimensionTabs.Pages.Add "MyName", "My Caption"
Dim currentPage As Page
Set currentPage = DataQueryForm.DimensionTabs.Pages(0) 'this line fails
As you can see I don't even know how to get the first Page into a variable of type Page - so it's hard to know how to begin.
Any pointers in how to add new checkboxes to the page would be most appreciated. I'm really struggling to find decent documentation on this, really, anything at this stage would be helpful.
Thanks

You need to specify MSForms.Page so you don't get a type mismatch (Excel also has a Page object) and you can set the variable as you add the page:
Dim currentPage As MSForms.Page
Set currentPage = DataQueryForm.Dimensiontabs.Pages.Add("MyName", "My Caption")

Related

Run-Time Error 4605: The ShowHiddenText method or property is not available because this command is not available for reading

I'm fairly new to using content controls and I'm designing a Word form for work. The current form has the help text for content controls hidden (I have the option enabled to display hidden controls on my computer); however, I want to make sure users of the form are able to see these controls when they open the form. The issue is that when the help text is not hidden, it prints on the form. Is there a way to set the controls so the help text does not print but it is still visible to users when they open the form?
I tried using the following code, but I ran into issues with it. The code worked on my computer, but when a coworker tested it it caused an error message to appear and I could not figure out why. The single line of code was highlighted but as the code worked on my computer, I'm not sure how to fix it (I am also very new to Visual Basic).
Sub AutoOpen()
ActiveWindow.View.ShowHiddenText = True
End Sub
I was hoping that when another user opened the document, they would see the hidden text as if it were not hidden; however, when another user opens the document, they get this error:
"Run-time error '4605':
The ShowHiddenText method or property is not available because this
command is not available for reading."
The error message is happening because Word is opening the document in the Reading rather than the Print view. That is often the case when a document is opened from email, for example.
Adding the following line before the line for hidden text should help, as it will force the document to switch to the Print Layout view:
ActiveWindow.View = wdPrintView
So
Sub AutoOpen()
Dim vw as View
Set vw= ActiveWindow.View
vw = wdPrintView
vw.ShowHiddenText = True
End Sub

how to clear a picture content control with macro in ms word

I have created a form where the user has the option to reset (clear) the form. I was able to clear everything from the form (textbox's, combobox's & checkboxes), but not sure how to clear a picture if one was inserted without deleting the entire option. I've tried using the bookmark method,however, with not success. the bookmark is selected then delete. Which cause the code not to recognize the Inlineshape (hence - Run-time error '5941 "the requested member of the collection does not exist".
ActiveDocument.Bookmarks("picture").Select
Selection.InlineShapes(1).Delete
Selection.InsertAfter ""
You code makes no mention of content controls, however
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("Picture1").Item(1)
If oCC.Range.InlineShapes.Count > 0 Then oCC.Range.InlineShapes(1).Delete
where 'Picture1' is the title of the picture content control, should do the trick.

Send Parameter via new() or pre-set properties before calling the new form?

I would really appreciate your advice on the following:
I'm working on Windows forms using VB.NET (even though the language is irrelevant to the question at hand).
I've got a main form and wish to call out another one, however depending on a given variable I need the text on some of the new form's elements to change as well as disable some of its controls.
There are two ways I see of doing it:
Send a parameter from the main form and have some logic on the second form to deal with everything on load.
Main Form:
dim newform as new frmcalculate(byval type as string)
New Form:
public sub getexplanation(byval type as string)
select type
case "Sum"
lblexplanation.text = "this is a sum"
case "Subtraction"
lblexplanation.text = "this is a subtraction"
End sub
Set exactly what I want on the main form before calling the new form.
i.e:
dim newform as new frmcalculate()
newform.lblexplanation.text = "This is a sum"
I hope I've managed to explain it correctly.
I'm still new at this especially getting the formats right on Stackoverflow.
In the first approach the code is best managed and organized for further editing. So each form has it own code.
It is not best practice to use second approach. (Editing a form designer from another one)

VBA and DOM to fill out input box, in a frame (code here)

I have made references to the MSHTML library and MS Internet Controls setup, and I am trying to fill out a form using a variable. The main page consists of 3 frames, and the "top" frame is where my form is located. The code below will work, if I talk to the frame directly, with the form in the frame being called "NavPage". and the Cnum being the name of the element i want to add a value to, then click a button to retrieve the number
Dim HTMLDoc2 As MSHTML.HTMLDocument
Set HTMLDoc2 = Browser.document
HTMLDoc2.forms("NavPage").CNum.Value = "12345" 'will change this to become a variable
HTMLDoc2.forms("Navpage").GetCase.Click
Question is, how do I reference to the first top frame using DOM, because right now, it doesnt work, and it spits "Object variable or With block variable not set" at me. Thanks for any insight, as theres not much to be found concerning VB about this
You should be able to use this to reference the document in the first frame:
Set HTMLDoc2 = Browser.document.frames(0).document

VB.NET / Windows Forms - Data from user input to DataReport?

I'm developing a windows forms application using VB.NET. I'm currently working on DataReport (by Microsoft, not Crystal). Everything works fine when using DataSet as DataSource. But I have a question on how am I going to put data to my DataReport from a user input? Preferably when the user is on the act of printing(clicking the print button in the ReportViewer), a Form will pop-up and ask him for input. That input I'm talking about is random names of people, and we don't have to track their names so there's no reason to put it on the database.
Here's my simple setup:
MainForm.vb (Form)
MyReportViewer (ReportViewer)
MyReport.rdlc (DataReport)
InputForm (Form)
MyInputBox (TextBox)
If it is impossible to do it on the act of printing. Maybe on the MainForm_Load event or before the generation of report can do.
I've searched the net for an hour but I'm out of luck.
I've found the answer, it's called ReportParameters.
Here's how to set it:
'Get input from user...
Dim OneParameter As String = InputBox("Enter something to display on report!", "Report Parameter")
'Prepare report parameters... The `SetParameters` requires array of `ReportParameter`...
Dim ReportParameters As New List(Of ReportParameter)
'Add the user input to `ReportParameter` array...
ReportParameters.Add(New ReportParameter("OneParameter", OneParameter, True))
'Set the `ReportParameters` of the `ReportViewer`...
FormWithReportViewer.TheReportViewer.LocalReport.SetParameters(ReportParameters)
FormWithReportViewer.Show()
FormWithReportViewer.Focus()
On the DataReport(.rdlc), we have to add a ReportParameters (Menu>Report>Report Parameters). The same name (OneParameter) should be used to avoid error. We can now use the parameter in our textbox, just put =Parameters!OneParameter.Value and we're done!
Note: We have to set the report parameters before rendering the report. If we cannot avoid that, refresh the report after adding parameters so it will be updated:
FormWithReportViewer.TheReportViewer.RefreshReport