Using Macros to Control Word Through Excel (or other program)

OffTopic

New Member
Joined
Sep 4, 2014
Messages
11
I'm working on a macro that copies text from CorelDraw and puts it into text boxes in Word. I realize this is an Excel forum but I'm thinking that since it's all Visual Basic, a lot of the code would be the same as an Excel macro to control Word.

Right now I can get the following code to work from CorelDraw:

Sub WordPlay()


Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add ' create a new document


With wrdDoc
Dim Box As Shape
For i = 1 To 2
.Content.InsertAfter "Here is a example test line #" & i
.Content.InsertParagraphAfter
Next i
End With

End Sub

Does anyone know how I can get the sample text into text boxes?

Thanks in advance, I've been searching and experimented but haven't gotten very far.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Do you have a Word document with textboxes?

If you do/did then you could try recording a macro when you manually insert text in a textbox.

Then you should be able to adapt the code generated by the recording.
 
Upvote 0
Ideally the Macro would generate the boxes and insert the text.

I can find code to create text boxes and insert text but I can't get them to work through CorelDraw.

Rich (BB code):
Rich (BB code):
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">    Dim Box As Shape    Set Box = ActiveDocument.Shapes.AddTextbox( _        Orientation:=msoTextOrientationHorizontal, _        Left:=50, Top:=50, Width:=100, Height:=100)                Box.TextFrame.TextRange.Text = "text"
</code></pre>


I've tried placing this in various ways under the "
With wrdDoc" line I keep getting errors like "object doesn't support property or method."
 
Upvote 0
Pretty sure you can't just stick that code into what you already have.

Use it as a guide/template and adapt it as needed.

By the way, will the number of textboxes be fixed or can it vary?
 
Upvote 0
Yeah, it didn't work at all but it's the closest thing to a macro for creating a text box that I could find.

I'm having trouble finding the basic information for the syntax. I don't understand how to use a dot "." at the beginning of a line.

The boxes will vary. The first part of the Macro finds text boxes within Corel that have a certain word in them. This information must be deleted from the Corel file and inputted into a word document. I would hope to also copy over the text formatting but it's not crucial at this point. I really just need to figure out how to create text boxes first.
 
Upvote 0
You have the code to create a textbox, using it outwith Word VBA might look something like this.
Code:
Dim Box As Object

    ' code to create new instance of Word, add new document etc 
    With wrdDoc
        Set Box = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=50, Top:=50, Width:=100, Height:=100) 
    End With
                 
    Box.TextFrame.TextRange.Text = "text"
 
Upvote 0
First off, thank you.

Second, I get a "specified value is out of range -2147024809 (80070057)" on the "Set Box = .Shapes...." line.

I thought it may have been the specifics of the properties, like Left = 50 might have put the box outside of the margins, so I changed the values to a bunch of things and couldn't shake the error. Just to make sure I tried the original textbox macro in Word and it works fine.

I can't find much on the error code, at least not related to this specific context, but I feel a little closer to the answer.
 
Upvote 0
I used Tools>References and linked the Word library. This didn't immediately fix the problem but as I was rewriting "Set Box = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal" VB prompted me with:

"The library which contains this symbol is not referenced by the current project, so the symbol is undefined. Would you like to add a reference to the containing library now?"

Once I clicked OK everything started working. If I unlink the library in Tools>References it reverts to the previous "specified value" error. Even after relinking the library the macro will not work until I rewrite the "Set Box = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal" and get the prompt.

This concerns me that I will have trouble sharing the macro with coworkers if the only way to link the library is the equivalent of casting a spell and hoping for a prompt. I believe I came across a post from you (Norie) about early and late binding but didn't have any luck with the shared code.

Thank you very much for the help. This was a large hurdle.
 
Upvote 0
Unless you are using early binding a constant like msoTextOrientationHorizontal means nothing in the application you are automating from and will most likely be treated as n undeclared variable with the value 0, vbNullString, Empty, Nothing.

Normally you would replace the constant with it's actual value, have you tried that?
 
Upvote 0
No, I'm not sure what the constant of msoTextOrientationHorizontal is. I brought the macro to a different computer and it works fine. Thanks again for the help.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top