Possible to manipulate text labels in report section headers using VBA? (MS-ACCESS) - vba

Possible stupid question here...
But is it possible in MS-Access to programmatically manipulate text label captions in such a way that that a different caption will appear in the header of each report section?
Ie., The design view shows a text label object in the GroupLevel zero header, with a default caption of "blah"....but upon execution of a Report_Load() sub, the actual text displayed is different for each section in the report? Say, simply "Section 1", "Section 2', "Section 3" and so on?
My suspicion is that this is not possible, but just wondering if anyone has some creative ideas how to make it work.
I realize that there are other/better ways of accomplishing the same thing...but is such a thing possible using VBA and Label objects specifically (at the moment, this is an external constraint and one that I cant change).
EDIT: https://drive.google.com/open?id=1PID58qMyp_rNxv9tsQk38-Co9sDOFgzY
EDIT 2: Original post specifically designated LABELS as the only object for an acceptable solution. Edit to include LABELS and TEXT BOXES...which of course makes the question nearly trivial. Apologies to #peakpeak for my lack of clarity!

You can change the caption with
Me.<name of header>.Caption = "whatever"
Select Properties for the header in design view and find out and/or change the Name property. Me assumes that the VBA code is located under Microsoft Access Class Objects in the form you want to manipulate.

Actually, thank you for your help, but I have found something that appears to function as a decent work-around within the constraints I described above. Strictly speaking, this does not satisfy the terms of the question as originally asked (-1 to me for lack of clarity, and apologies to #peakpeak, who admittedly had essentially zero chance of answering this question as asked), but it this is close enough that it solves my immediate problem:
Change the object intended to contain my dynamic text from a label to a text box (duh!), keep all formatting settings, etc. the same so that the graphical presentation is unchanged.
Set the Control Source of the new text box to a public function, where the argument of the function is the name of a relevant field in the underlying query ("tName" in the linked example), so that the dynamic text box has a control source "=GetText([tName])" and the GetText() function is defined in the appropriate module for the report, and defines the text as desired, e.g.:
GetText(tName as String) as string
SELECT Case tName
Case "Albert"
GetText = "Section 1"
Case "Barry"
GetText = "Section 2"
Case Else
GetText = "Section 3"
'and so on
End Select
`
End Function

Related

Can I use the Shrink Method for the whole document in word

I don't know anything about code. I work with e-learnings in Storyline 3. I sometimes localize these e-learnings and use the translation tool in Articulate which basically exports an MS Word file. Sometimes the target languages are longer and I need to decrease the font size by percentage for the whole document. Usually, there are at least 3 different font sizes that I need to decrease accordingly. I am wan to develop a macro that I will use for multiple documents.
I couldn't find a way to do this by percentage, but looks like the Shrink or Grow Methods will do the work! I found this code in the reference page but looks like it works only for a selected object. The issue is that the exported MS Word file is in a table with each text box in the storyline separated to a cell. When I select the whole table it does not work.
If Selection.Type = wdSelectionNormal Then
Selection.Font.Grow
Else
MsgBox "You need to select some text."
End If
Could you please help me and let me know if this would be possible for the whole document, or the selected table? It would be very much appreciated. Thanks in advance.
It is unclear from your question whether the table in the Word document contains the actual text boxes or just the text they contain.
If it is just the text then Shrink may work. I tested this on a document with a single table containing only text:
ActiveDocument.Range.Font.Shrink

How can i set the name of a textbox in publisher?

I want to set the name of the text box so it can be easily accessed by code.
e.g
I am looking for an editing field similar to this
Thanks
There's a properties Window that can be accessed for each of the controls on the UI. There you may rename the controls. (Since you do not seem to have a VBA code yet and you want to rename the control from UI)
The other alternative. Record a macro, do some changes to the textbox (e.g. resize, change text etc). Then check the programme assigned default name of the textbox from the VBA editor. As you said, you can access the control via this default name and utilizing your VBA code (as you said), rename the textbox.
If you really want to be editing a worksheet object in Publisher you will have to get the OLEobject of the Shape and interpret it as an Excel.Application.
If you are just looking for a placeholder solution for Publisher documents, you could simply create a textbox that contains a certain string, then loop through all pages, all shapes on each page where HasTextFrame = msoTrue, and compare shape.TextFrame.TextRange.Text to your placeholder string. If it's the one you're after, you can do anything you want with the shape in question.
Sorry for the vague answer, but your images don't work anymore.
Edit: you can work with Shape.Name for your comparison (you mentioned this property in a comment), but I have no idea how you'd set the value from the interface, without using VBA, in the first place, so if you're making templates the approach I outlined above might be easier for users (see https://msdn.microsoft.com/EN-US/library/office/ff939233.aspx for Shape.Name). There is also a .Name property for page objects (https://msdn.microsoft.com/EN-US/library/office/ff940382.aspx), so you should be able to do something like ActiveDocument.Pages("page_name").Shapes("shape_name").TextRange.Text = "your content" once you've figured out how to actually set the name values
Edit 2:
You can also try to use search and replace as per Replacing Text in Microsoft Publisher Using Powershell if you don't need to do anything advanced beyond placing some text
Edit 3: Given the title of your question, unless you can figure something out with Publisher's interface, you can set the .Name property of the selected text box (or other shape) with dim shape = Selection.ShapeRange.TextFrame.Parent and shape.Name = "your_name". You can set the name of the selected page with ActiveDocument.ActiveView.ActivePage.Name="your_name". (Create a VBA macro that prompts you for names and you should be good to go)

Creating a self test program with save/import functions (images, description inside)

I'm basically trying to create a program that will allow me to use textboxes to input questions and associated answers to test myself. Mostly, I just want to practice some things I've been learning for vb.net while taking an extra step forward.
Here's a mockup:
Any tips on how to do this? What I'm especially not sure about is how to have the textfile save/import work with adding back into comboboxes and making sure the correct questions and answers are associated with one another. I was thinking also that I could have some sort of check when I import a save file that shows me which questions and answers refer to one another
Update on form design:
Some things I'm trying to practice:
Using arrays
Saving/Opening files (text files in this)- in order to import into program
importing into program
Using the progress bar
Having a way to undo entries
Randomizing sort order in the combo box
Maintaining references between variables (like which answer
corresponds with which question) regardless of sort order or using
the undo function
Some posts in stackoverflow I'm thinking of using as reference:
Compare String with Strings in array
Best way to associate two strings of text in VB.NET
Array to combo box?
Update: Trying to be more specific on my question. Basically, I'm trying to use a list to account for all the inputted questions and answers, however I've only used really basic arrays before, like:
val(1) = "test"
val(2) = "this"
val(3) = "string"
Or the kind where you use a For x to z loop. Based on some stackoverflow posts I've gotten to the point where the code works in that (I think?) the list is updated and the combobox for questions and answers reflect what is on the list. However, I can't connect the two so that I can select things in the comboboxes and check if they are correct.
In terms of saving as a text file. I figured it would be simpler. Basically, have it output sort of like
"question goes here" ; "answer goes here"
"next question goes here" ; "answer goes here"
and so on. Then I can use the semicolon as reference in the code when importing. I've never done it before, but it's generally what I do for Microsoft Access or Excel text imports so I figured it was possible.
Code that I have so far (reddit post link): http://redd.it/2716tw

Printing custom ranges or items in Word 2010 using VBA

I am fairly new to VBA (Word 2010) and I'm unsure if something I'd like to do is even possible in the way that I want to do it, or if I need to investigate completely different avenues. I want to be able to print ranges (or items) that are not currently enumerated as part of either wdPrintOutRange or wdPrintOutItem. Is it possible to define a member of a wd enumeration?
As an example, I'd like to be able to print comments by a particular user. wdPrintComments is a member of the wdPrintOutItem enumeration, but, I only want comments that have an Initial value of JQC. Can I define a wdPrintCommentsJQC constant? My code is reasonably simple; I have a userform that lets the user pick some settings (comments by user, endnotes only, etc.) and a Run button whose Click event should generate a PrintOut method with the proper attributes. Am I on the wrong track?
(If it matters, the Initial values will be known to me as I write the code. I have a discrete list.)
No, it's not possible to add a constant to a predefined enumeration type.
However, one possible way to do this would be to build a string of page numbers which contain the items you wish to print, open the print dialog in the "dialogs" collection, and set it to print a specified range, andinsert the string containing the list of pages (separate them with commas). Finally, execute the .show method of the print dialog to show it to the user and give them the opportunity to set any other items and click the "ok" button. I've done something very similar when I needed to print a specific chapter of a long document, and so I had to specify the "from" section and page and the "to" section and page for the user. Below I just show how to specify a list of pages instead of the ".form" and "to" I was using:
With Dialogs(wdDialogFilePrint)
.Range = wdPrintRangeOfPages
.Pages = "3,5,7-11"
.show
end with
I'm not sure how you want to print the comments (or other elements), but you could create another document and insert what you want to print on this document.
According to what you want, you could insert them as they were (comments, footnotes, etc) or as plain text, or any other format.

Using VBA in MS Word 2007 to define page elements?

I'd like to be able to create a page element which I can feed text and it will form itself into the preferred layout. For instance:
{MACRO DocumentIntro("Introduction to Business Studies", "FP015", "Teachers' Guide")}
with that as a field, the output should be a line, the first two strings a certain size and font, centred, another line and then the third string fonted, sized and centred.
I know that's sort of TeX-like and perhaps beyond the scope of VBA, but if anyone's got any idea how it might be possible, please tell!
EDIT:
Ok, if I put the required information into Keyword, as part of the document properties, with some kind of unique separator, then that gets that info in, and the info will be unique to each document. Next one puts a bookmark where the stuff is going to be displayed. Then one creates an AutoOpen macro that goes to that bookmark, pulls the relevants out of the keywords, and forms the text appropriately into the bookmark's .Selection.
Is that feasible?
You're certainly on the right track here for a coding solution. However, there is a simpler way with no code - this is the type of scenario that Content Controls in Word 2007 were built for and with Fields/Properties, you can bind to content controls (CC). These CC can hold styles (like centered, bold, etc.). No VBA required.
The very easiest thing to do is to pick 3 built-in document properties that you will always want these to be. For example, "Title" could be your first string, "Subject" your second string and "Keywords" your third. Then, just go to the Insert ribbon, Quick Parts, Document Properties and insert, place and format those how you like. Then go to Word's start button (the orb thingy) and then under Prepare choose Properties. Here you can type, for example "Introduction to Business Studies", into the Title box and then just deselect it somehow (like click in another box). The Content Control for Title will be filled in automatically with your text.
If you want to use this for multiple files, just create this file as a .dotx (after CC insertion/placement/formatting and before updating the Document Properties' text). Then every time all you'll have to do is set these three properties with each new file.
Well, yes, it did turn out to be feasible.
Sub autoopen()
Dim sKeywords As String
sKeywords = ActiveDocument.BuiltInDocumentProperties(4)
ActiveDocument.Bookmarks("foo").Select
Selection.Text = sKeywords
End Sub
Okay, I have some filling out to do, but at least the guts of it are there.