How to delete the last return in a PowerPoint field? - vba

I have created a function that allows a user to import one or more text files into a field in PowerPoint. This works really well. The user clicks in the field, clicks the button on the custom menu, selects the files from a list and in they go. The problem I have is that I have to put two returns between each imported text, which means there are two left on the end.
This is the usual result to delete a paragraph I have found:
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=27, Length:=1).Select
ActiveWindow.Selection.TextRange.Text = ""
This suggests I would have to calculate the last position int he selected field to be able to delete it.
Could anyone give me an idea of how I would go about this?

This solution is a bit easier (not tested):
dim tmpTXT as string
tmpTXT = ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Text
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Text = Left(tmpTXT, len(tmpTXT) -1)
You could do something similar earlier, before you write text to your shape which would be better.

Related

Word 2010 VBA: add text to template without deleting everything below

I have been writing a code to build a custom document for weekly reporting. I had it set to create a new doc, build the tables, format everything, insert a logo, etc., and I hit a wall when Word crashed every time I tried to resize the logo through VBA. It then occurred to me that I was doing this the wrong way anyway, and that I should build a template, have the code open that template, and then insert the changes directly into that document.
When I add text to the range I need, everything below is deleted. I have three lines of a header; the third line is variable and will change each week. Below that I have three tables, some cells of which will be updated each week. Right now I am just stuck at the third line of the document.
Here is an example of what I have. It is extremely short because I started anew and hit a dead end immediately.
Dim Template as Document
Set Template = ThisDocument
Dim InsertSpot as Range
Set InsertSpot = Template.Range(46)
InsertSpot.Text = "Hello"
When I run that, everything is deleted below "Hello".
I tried some different things, such as:
Set InsertSpot = Template.Range(46,50)
InsertSpot.InsertAfter "Hello"
That doesn't work; it just adds "Hello" to the first cell of the first table.
I feel silly being stuck at such an elementary part, but I honestly have no idea what to do here. Everything I looked at online talked about how to insert text at a bookmark without deleting the bookmark. I just want to enter text, period.
Any thoughts?
I found the answer. I did the following:
Dim Template as Document
Set Template = ThisDocument
Dim InsertSpot as Range
Set InsertSpot = Template.Range(46, 46)
InsertSpot.Text = "Hello"
I had to list both the starting AND the ending character so that the inserted text would not bleed over into the rest of the page. Hope I helped someone else who is having a similar problem. Unfortunately Word VBA is pretty lacking in support and clear explanations.
other alternatives can be
ThisDocument.Range(46).InsertBefore "Hello"
ThisDocument.Tables(1).Range.InsertBefore "Hello"
ThisDocument.Paragraphs(3).Range.InsertAfter "Hello"

Moving the code one coloumn to the left

I wrote a code and now my excel template has changed (the first column is no longer in need), is there a way to move the entry code 1 column to the left instant of correcting it step by step?
For example(This is the old code):
.Range("I1:J1") = Array("CHECK", "KEY")
Now I need to change it to:
.Range("H1:I1") = Array("CHECK", "KEY")
But it's a very long code and I want to know if there's any way do to it easier.
Thanks.
I'd suggest simply using the built in Search & Replace function ( Ctrl + f ). Put something like
.Range("I1:J1")
into the "Find What" field and
.Range("H1:I1")
into the "Replace With" field.
If you're really lazy you could hit the Replace All button, but this can be dangerous as in possibly changing parts you didn't want to change. However, using the Replace button and going through all entries can be fairly fast even in a longer code, and this way you can check with each entry if it's really correct to change it.

VBA Word : SmartArts and range

I'm currently working with SmartArts in Word.
The SmartArt already exists, and I would like to find a way to access a SmartArt value from a range, Office cursor (so Selection) or something akin to it.
Here's my SmartArt.
To be exact, the SmartArtLayout is Application.SmartArtLayouts(88)
What I can do already :
Create a SmartArt on my Document, keep it as a Shape to work on it.
Looping on all the Nodes of my SmartArt, selecting the ones I want to keep, and inserting the ones I want to keep in a database, with a link to their parent.
Now I'm blocked on something.
I want to find a Way to edit/delete a particular node, both in DB and in the SmartArt.
It would be possible if I made a Userform that put every Shape Text in a drop-down list, and I then looped on all shapes to find the shape with the chosen text in it.
If noone answers, that'll probably be what I am going to do.
But I don't find this way natural or practical.
So I'm looking for a method to be able to find the Node that have the cursor on it. I would've done something like that :
'Putting my SmartArt in SAShape
For Each Node In SAShape.SmartArt.AllNodes
If (Selection.Range.InRange(Node.Range)) Then
'my code
End If
Next Node
But It'd have been too easy... The shapes don't seem to have a range.
Is there a way to get the Node with focus, or to get the node we clicked on?
Thanks in advance!
Cordially,
firt It not like Question but it is task but i will give you some hint to help you
1) to get any node in any level loop will be like this
Set oShape = ActiveSheet.Shapes(1)
For i = 1 To oShape.SmartArt.AllNodes.Count
oShape.SmartArt.AllNodes(i).TextFrame2.TextRange.Text = "Sample " & i
Next
2)To Get specific node for delete or edit you must determine an id i can not do that perfect but i assign the id to alt text when i add alt text i can find any node
2.1) when shearch
nod.TextFrame2.TextRange.Text = str
nod.Shapes(1).AlternativeText = index
2.2) when check for search
If oShape.SmartArt.AllNodes(i).Shapes(1).AlternativeText = index
and the imge will show where alt store
that i think will help you in some way for do what you need but you must understand how smart art tree change after item delete
smartart

How to select multiple lines of code and space them all?

This may be a fairly simple answer, or relatively easy; though I haven't been able to find the correct command/way to do so from anywhere.
As the title states, I'm trying to figure out the way as to space multiple lines of code.
Here's an example of what I'm looking for:
testing = "Testing"
work = "Work"
answer = "Answer"
Now I'd like to add spaces to the first two lines (selected together) at the same time, as in:
testing = "Testing"
work = "Work"
answer = "Answer"
Hold CTRL and click at the beginning of each line.
This will create two cursor points and every command will be iterated at every point.
Another tip:
You can highlight a selection of text, and CTRL + D will highlight all matches for that text and activate a cursor next to it. Very useful for mass editing in things like tables or forms.

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)