Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro help: Creating text box in Word from other application

    I'm working on a macro that copies text from CorelDraw and puts it into text boxes in Word. I realize this is an CorelDraw is a bit obscure 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.

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    I don't have CorelDraw to play with, but this code works as a macro in Excel after setting a reference to the "Microsoft Word xx.x Object Library" (where xx.x is whatever Office version you have on the computer).

    Code:
    Sub WordPlay()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim i As Integer
    Dim Box As Word.Shape
    
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add ' create a new document
    
    
    With wrdDoc
        For i = 1 To 2
            .Content.InsertAfter "Here is a example test line #" & i
            .Content.InsertParagraphAfter
        Next i
        
        Set Box = .Shapes.AddTextbox( _
            Orientation:=msoTextOrientationHorizontal, _
            Left:=InchesToPoints(1), Top:=InchesToPoints(2.5), _
            Width:=InchesToPoints(4), Height:=InchesToPoints(2), _
            Anchor:=.Paragraphs(1).Range)
        Box.TextFrame.TextRange.Text = "Text in box" & vbCr & "Second line"
    End With
    
    End Sub

  3. #3
    New Lounger
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. I feel like I'm almost there but I'm getting a "specified value is out of range -2147024809 (80070057)" error now.

    I asked the question at Mr.Excel:
    http://www.mrexcel.com/forum/excel-q...r-program.html

    The resulting code gave me the same error. This may be a CorelDraw thing.

    "I don't have CorelDraw to play with" Yeah, it's not a very popular program.

  4. #4
    New Lounger
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mentioning setting the reference was key. This didn't immediately fix the problem but as I was rewriting "Set Box = .Shapes.AddTextbox(Orientation:=msoTextOrientation Horizontal" 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:=msoTextOrientation Horizontal" 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.

    Thank you very much for the help. This was a large hurdle.

  5. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    I'm sorry it took me a while to get back here.

    To remove the need to have a reference to the library (which I think is the "Microsoft Office xx.x Object Library"), you can declare the equivalent value as a constant at the beginning of the same module with your macro:

    Const myTextOrientationHorizontal = 1

    and replace the mso constant in the AddTextbox call with this constant.

    I wouldn't use the same name as the constant defined in the library, in case some user has the library reference for some other reason; in that case you risk a "duplicate definition" error.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •