Using formatted text from Word table to Publisher - vba

I'm trying (and failing) to fill out a text box (TextFrame) in Publisher using a macro from content in a Word table. I'm trying to do something along the lines of:
With doc.Pages(1).shapes(1)
.GroupItems.Item(8).TextFrame.TextRange = table.Cell(2, 3).Range.FormattedText
End With
The source text from table has a bunch of font formatting that I need in the text box but it won't seem to be able to copy over the formatting and I just get the plain text. Any ideas on how to get this working properly?
Edit: It seems like TextFrame can't accept formatted text at all. Is there any way around this?

In Word TextFrame.TextRange returns a range which has a FormattedText property. The usage for that would be:
.GroupItems.Item(8).TextFrame.TextRange.FormattedText = table.Cell(2, 3).Range.FormattedText
In your code you haven't specified which property of the TextRange you want to assign the formatted text to. This means it will be assigned to the default property, Text, which is just a string and cannot contain any formatting.
Golden Rule: never rely on default properties, always specify the
property you want to use.
Given that you appear to be taking a value from Word into Publisher you should be looking at the documentation for VBA in Publisher which shows you that the TextRange object in Publisher does not have a FormattedText property, so you cannot take formatting across using this method.

Related

Access Macro - Was textbox changed from default value?

I have an Access form with textboxes that contain some default text. The users are required to replace the text with words of their own, so I wrote a macro to confirm that (before proceeding to the next record) the text boxes no longer contain the default text. (It gives user a warning plus some other tasks, which is why I'm not using built-in Validation rules.)
The macro contains this sequence:
SetLocalVar
Name: booDetailDefault
Expression = [Forms]![frmChangeReq]![txtProblem].[Text]=[Forms]![frmChangeReq]![txtProblem].[DefaultValue]
The idea is if ...[Text] is the same as ...[DefaultValue], then booDetailDefault would be TRUE, and if not, FALSE.
However, what I do get is "Type Mismatch". I would think that Text and DefaultValue would be the same type.
Can you tell why I got this error?
Is there any way to display the Text and DefaultValue values so I can see why Access thinks they're different?
Is there a better way to see if the text boxes have been changed?
EDIT: The default text is set in the Property Sheet for the textbox. I've been running it manually, not tying it to an event yet, until I get it working.
I used Text instead of Value because that's what was available in the Macro Builder, but just in case I tried changing it to Value. It works, although I have to tell the expression to expect quotation marks around half of it:
[Forms]![frmChangeReq]![txtProblem].[DefaultValue]="""" & [Forms]![frmChangeReq]![txtProblem].[Value] & """"

RichEditBox loses formatting for empty text

I have something like a diagramming UWP application where each shape has an RichEditBox. The problem is, that I loose the formatting when the text is empty.
For Example:
Change the text to bold: EditBox.Document.Selection.CharacterFormat.Bold = FormatEffect.On.
Delete all text
Get the text via "EditBox.Document.GetText(GetTextFormat.FormatRtf, out text);
=> The result is just an empty string, not valid RTF document at all. This is a problem in my scenario. Is there an approach to solve it? I think RichEditBox should always provide a valid RTF document.

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)

automating word 2010 to generate docs

the webapp was already done on office2007 and i need to convert it so it'll work in office2010.
i was able to convert the header generator part of the code but i have problem with the body of the doc itself. the code copy the data from a "data" doc and paste it into the generated doc.
appword.activewindow.activepane.view.seekview = 0
'set appsel1 = appword.activewindow.selection
set appsel1 = appword.window(filepath).selection -that is the original one
appdoc1.bookmarks("b1").select
appword.selection.insertafter("some text")
appsel1.endkey(6) -the code stops here
appword.selection.insertafter("some other text")
the iexplorer debuger says ERROR:appsel1 object required. and when i view its data using the iexplorer debugger its data is "empty" instead of "{...}"
can anyone tell me what i'm doing wrong
if you need more of the code tell me.
From MSDN
After this method is applied, the selection expands to include the new
text.
If you use this method with a selection that refers to an entire
paragraph, the text is inserted after the ending paragraph mark (the
text will appear at the beginning of the next paragraph). To insert
text at the end of a paragraph, determine the ending point and
subtract 1 from this location (the paragraph mark is one character).
However, if the selection ends with a paragraph mark that also happens
to be the end of the document, Microsoft Word inserts the text before
the final paragraph mark rather than creating a new paragraph at the
end of the document.
Also, if the selection is a bookmark, Word inserts the specified
text but does not extend the selection or the bookmark to include the
new text.
So I suspect that you still have no selected text.
I wonder if you can do a Selection Collapse(wdCollapseStart) but that's just a thought.

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.