Summary:
Using Visual Basic for Applications (VBA), I'm trying to draw a Textbox and apply the TextEffect msoTextEffectShapeArchUpCurve to it.
This works fine when drawing this in an Excel sheet, but will not work in a Word document (although it can be done in the same way using the GUI).
So I have an Excel file where the script is run from. This script creates a new Word document and draws shapes inside of it.
Example:
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objDoc.Activate
With ActiveDocument.Shapes.AddTextbox( _
msoTextOrientationHorizontal, _
50, _
50, _
200, _
200 _
)
.TextFrame.TextRange.text = "Some text to curve"
.TextEffect.PresetShape = msoTextEffectShapeArchUpCurve '<-- error
End With
Problem:
The code gives an Error 4652: Invalid type of drawing object for this command.
According to the documentation this should work. (And it can be done with the GUI, although no code is produced when recording the actions in a macro..)
Am I doing something wrong? And how can I fix this?
A screenshot of how it should look like (left) and how this is done in the GUI (right):
Note: Using AddTextEffect instead of AddTextbox is not really an option, since WordArt text is distorted compared to a textbox with the chosen effect.
You are using late binding (with CreateObject) so Excel does not know what msoTextEffectShapeArchUpCurve is.
It is a constant, a value, so you can just replace it with 9.
Unfortunately, it's still a mystery why Word won't let VBA set this text effect on textboxes, while on the other hand allowing it in the GUI. (Any help or information on that would still be greatly appreciated!)
However, there is a work around that worked for me:
Create the textbox in the Excel document and apply the text effect there (using the code from my example).
Then let VBA cut and paste the shape into the new Word document.
Related
I use a long Excel spreadsheet containing incorrect and correct terms to check consistency between documents (e.g. anti-citrullinated is always hyphenated). I've added quite a few of these as autocorrect entries via the AutoCorrect Options feature in Word but it's time-consuming .
I came across the following code that will add long lists of autocorrects.
Sub BatchAddAutoCorrectEntries()
Dim objTable As Table
Dim objOriginalWord As Cell
Dim objOriginalWordRange As Range
Dim objReplaceWordRange As Range
Dim nRowNumber As Integer
Set objTable = ActiveDocument.Tables(1)
nRowNumber = 1
For Each objOriginalWord In objTable.Columns(1).Cells
Set objOriginalWordRange = objOriginalWord.Range
objOriginalWordRange.MoveEnd Unit:=wdCharacter, Count:=-1
Set objReplaceWordRange = objTable.Cell(nRowNumber, 2).Range
objReplaceWordRange.MoveEnd Unit:=wdCharacter, Count:=-1
AutoCorrect.Entries.Add Name:=objOriginalWordRange.Text, Value:=objReplaceWordRange.Text
nRowNumber = nRowNumber + 1
Next objOriginalWord
MsgBox ("All autocorrect items in the table1 are added.")
End Sub
It doesn't preserve any formatting: super- or subscripts, etc. Formatting autocorrect entries are stored in the Normal.dotm file and not in the regular .acl file so I haven't been able to figure out a way around this.
In a similar post, someone suggested a Find and Replace macro but Find and Replace doesn't allow me to replace with super- or subscripts.
There are two methods of adding Auto Correct Entries, Add and AddRichText. It is this second one that you use for formatted entries.
When faced with an issue like this my first resort is to check the Object Brower in the VBA editor (press F2 to display) to see what methods and properties may be available. My next step is to look them up in the VBA technical reference, aka Help, to check the usage.
If the problem is just sub/superscribt, then you could use uni-codes. Those are also available in autocorrect. Fx writing the unicodes ₁₂₃₄₅₆₇₈₉ instead of using formating on a normal 2. Most (but not all) characters exist in super and sub unicode.
The program is not working. It is giving an error message
Compile Error Expected Function or Variable
It is showing the following line as error
Autocorrect.Entries.Add Name:=objOriginalWordRange.Text, Value:=objReplaceWordRange.Text
I'm trying to record a macro that will set the size of a pasted image to 6.5 x 4 cms and the image layout 'in front of text'. I usually set this by right clicking on the image and setting the picture properties but this isn't available when recording the macro. Using Shift-F10 does bring up the menu, but the option to set the image layout properties is greyed out.
Please help!
Thanks
Yes, that's interesting about the macro recorder...
If you need to do something like this in the future it will help you to know that an object with any kind of "text wrap" formatting (that "floats") is a Shape. An object that behaves like a character is an InlineShape. And you can convert between the two using ConvertToInlineShape / ConvertToShape. For future things of this nature that should give you a starting point :-)
The following code sample uses ConvertToShape so that text wrap formatting can be applied. (The conversion is an extra step involved that you don't have to do in the UI - Word does it for you.)
Before this happens the code changes the size, but it could also be the other way around, changing the size on the Shape object.
What else is happening in the code: When a picture is pasted inline it's not selected. So this code figures out how many pictures (InlineShapes) are already in the document up to the selection. After the paste the code then picks up the existing number of pictures up to that point, plus one, to get the picture that was just pasted.
The code uses CentimetersToPoints to convert the number of centimeters wanted to the Points measurement, since that's what Word uses to size graphical objects (and lots of other things).
How did I know to use wdWrapFront: When shp.WrapFormat.Type = is typed the VBA Editor will automatically show a list of valid entries for text wrap formatting. Similarly, when shp. is typed a list of valid properties for a Shape will appear, and so on. (This is called IntelliSense and is a wonderful help!)
Sub PasteAndSelectPicture()
Dim ils As Word.InlineShape
Dim shp As Word.Shape
Dim lNrIls As Long
Dim rngDoc As Word.Range
Dim rngSel As Word.Range
Set rngDoc = ActiveDocument.content
Set rngSel = Selection.Range
rngDoc.End = rngSel.End + 1
lNrIls = rngDoc.InlineShapes.Count
rngSel.Paste
' Debug.Print rngDoc.InlineShapes.Count, lNrIls
Set ils = rngDoc.InlineShapes(lNrIls + 1)
ils.width = CentimetersToPoints(6.5)
ils.height = CentimetersToPoints(4)
Set shp = ils.ConvertToShape
shp.WrapFormat.Type = wdWrapFront
End SUb
I would like to create an excel macro, in which I create some chechboxes. When I record the macro, I choose the properties of the chechbox in edit mode, then on the left side i give new position values, size values, name and caption to the chechbox. But the macro itself don't remember, just the size and position values.
It will be in the visual basic code:
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
DisplayAsIcon:=False, Left:=128.25, Top:=84.75, Width:=108, Height:= _
21).Select
When I try to write in the code itself, that Caption:="xyz", that will be error.
How can I handle this?
If you want to do this via your VBA macro. Then the code in this question (not answers) can provide the syntax needed.
Otherwise, the following should work, it's not the cleanest but does the job (hat tip to Google for finding this):
Sub test()
Dim obj As OLEObject
Set obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1")
obj.Name = "checkboxx"
obj.Object.Caption = "CAPTION"
End Sub
Note that you will not be able to run this using the Step Into functionality of VBA (F8).
Please take a look at the attached image 1
I want to export data from a spreadsheet such as name, date of birth, address etc to a letter that I'm writing in word.
I've been following this tutorial:
I've had success in populating the information in word using a command button, but i don't want the ugly grey button in word so i tried making a macro and pasted the same script into a macro vba.
here is the script I'm using when trying to make the macro:
Sub Macro1()
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")
Set exWb = objExcel.Workbooks.Open("C:\Users\Admin\Desktop\Case Log.xlsx")
ThisDocument.solicitor.Caption = exWb.Sheets("Sheet1").Cells(4, 3)
exWb.Close
Set exWb = Nothing
End Sub
when i run the macro it highlights the word "solicitor" and displays the message "Compile error: Method or data member not found"
I've checked and rechecked and the label name is correct, it works with the command button, i don't understand why it shouldn't work as a macro.
Any helps would be much appreciated.
I found a native solution for doing this.
Mail Merge!
I set one up and saved it, now everytime I open the document, it loads all the info from the spreadsheet which it is already pointing to.
Thanks for all the suggestions
I'm writing a PowerPoint macro that opens a Word document and imports every picture in the document to its own slide. Because I can't place the Word InlineShape objects directly in PowerPoint, apparently, I'm using the clipboard. The relevant code is:
Dim wdApp As Object
Dim SourceDoc As Document
Dim TargetSlide As Slide
Dim Figures As Word.InlineShapes
Dim Figure As Word.InlineShape
Dim TargetShapeRange As ShapeRange
Set wdApp = CreateObject("Word.Application")
Set SourceDoc = wdApp.Documents.Open(FileName:=PathToFile, ReadOnly:=True)
Set Figures = SourceDoc.InlineShapes
For Each Figure In Figures
Figure.Range.Select
Figure.Application.Selection.Copy
Set TargetSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, _
ActivePresentation.SlideMaster.CustomLayouts(6))
Set TargetShapeRange = TargetSlide.Shapes.Paste
Next Figure
With PowerPoint 2010 and Word 2010, this works perfectly. In PowerPoint 2013 (with Word 2013 installed, and referencing the Microsoft Word 15.0 Object Library rather than 14.0), the Set TargetShapeRange = TargetSlide.Shapes.Paste line gives me run-time error '-2147188160 (80048240)':
Shapes (unknown member): Invalid request. Clipboard is empty or contains data which may not be pasted here.
I've tried using the Copy and CopyAsPicture methods on both the Selection and Range objects. I've tried using the Shapes.PasteSpecial command. I've tried several different ways of accessing the Word Application object. Nothing works.
Notably, if I modify the code to copy the preceding paragraph of text in addition to the picture, the script works—it retrieves the text, but not the picture.
I know this question is almost two years old but I found this through google after having a similar issue so I'll post this here.
Try
Figure.Application.ActiveWindow.ScrollIntoView Selection.Range, True
right before copying. It works for me even with screenupdating disabled or if the application is not visible.