• Macro help: Creating text box in Word from other application

    Home » Forums » AskWoody support » Productivity software by function » Productivity software by function – other » Macro help: Creating text box in Word from other application

    Author
    Topic
    #496287

    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.

    Viewing 3 reply threads
    Author
    Replies
    • #1466490

      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
      
    • #1466523

      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-questions/803182-using-macros-control-word-through-excel-other-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.

    • #1466603

      You mentioning setting the reference was key. 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.

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

    • #1467201

      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.

    Viewing 3 reply threads
    Reply To: Macro help: Creating text box in Word from other application

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: