Printing custom ranges or items in Word 2010 using VBA - 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.

Related

How can I build custom Word fields with VBA

I've found a question in stackoverflow: "How can I build Word fields with VBA" by JonnyGold.
I'm interested in the same question, but possibly on other reasons. The answers to JonnyGold question doesn't satisfy me. I'm still in MS-word 2003. My problem is to construct a custom word field, which would recognize a bookmark name around cursor location, saves that name in some custom variable/property, so that in a case of need a hyperlink of ref field could return a cursor to the said bookmark.
I need that mechanism to facilitate an easy work with a list of bibliographic sources, so that a user can by one click to go from a reference to a source and then to return back. Note that one source could be referenced in several different places and a user should be able to return to a reference, he/she clicked before.
I tried to use REF field with MACROBUTTON field inside, but MACROBUTTON requires double or one click on a button/text, which I want to avoid. I would like to create a field {RUNMACRO MacroName}, which would run a specified VDA Macro.

Microsoft Word MacroButton - placeholder text visibility

I have a Microsoft Office 2013 Word template, in which I have some text-field elements, created by using Quick Parts -> Field -> MACROBUTTON noname [Type your text here].
If I fill only some of these fields (i.e. "[Name]", "[Address]") and I print or save as PDF, all the fields that I have not filled will display as [Insert your text here] in the printed paper or PDF. To be clear, the placeholder text must be manually removed (or replaced with the text you want).
I've readed somewhere, that you can create a macro, which will not display the placeholder text in the PFD- or printed version of the document, if there is no text written manually to that specific field (you leave it as it was). As this would be handy in cases, where you don't fill all the neccessery fields, my question is:
Q: Can this be achieved only by using Macro Button, and if not, what is needed to create text fields as described below that are not included in the printed or PDF saved version of the document?
This cannot be achieved without using actual macro code. Right now your solution contains no macro code, the fields simply function as "targets" and when the user types on the field it is deleted. Where the user does not type, the prompt remains. You'd need code to delete these fields from the document.
Given your requirement, the code would have to fire in the DocumentBeforeSave and the DocumentBeforePrint events. These events require a class and supporting code in a standard module. The basic information on how to set these up is in the Word object model language reference: https://msdn.microsoft.com/en-us/library/office/ff821218.aspx
An alternative to MacroButton fields would be to use ContentControls. But here, again, code and the same events would be required to remove/hide placeholder text.

Working with multiple discontinuous selection

I'm trying to do something with a multiple selection. I wanna add some text before every selected paragraph but, when I select multiple discontinuous paragraphs, if I do Selection.Paragraphs.Count I always get "1".
How could I work with all paragraphs apart?
Example:
Paragraph1(Selected first)
Paragraph2
Paragraph3(Selected second)
What I got when I try to add some text at the beginning of these paragraphs:
Paragraph1
Paragraph2
TEXTParagraph3
What I really want to obtain:
TEXTParagraph1
Paragraph2
TEXTParagraph3
I'm working like this:
sub x()
dim p as paragraph
for each p in selection.paragraphs
p.range.insertbefore("TEXT")
next
End sub
Word simply cannot do what you'd like for it to do. Developers have wished for this since multiple selections were introduced in 2003 (I think it was, might have been version 2007). Word's object model simply does not support it.
If this is something you want to provide to the user to make life easier you'll need to give the tool a way to mark the paragraphs so your code can recognize them. You could provide a macro, for example, that assigns an incrementing bookmark name to each selection (the user selects, then runs your macro; repeat for each paragraph). Your code can then address each bookmark and perform the actions. To make this more user friendly you can assign the macro to a keyboard shortcut and/or a button in the Ribbon/QAT and/or the right-click menu.

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)

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.