Search code examples
excelvbapublisher

VBA Draw shape in Publisher from Excel sub


I have a sub in Xcel that I want to draw a formatted textbox into an existing publisher document. However, it keeps giving me a type mismatch when I try to draw the shape in. (This works perfectly when written as a sub on the publisher document, but not in the excel sub)


Sub UpdateInfo()
'Purpose: Input information from xcel sheet to formatted shapes within publisher doc
    Dim filename As String
    Dim appPub As New Publisher.Application  
    Dim NameVal As String
    Dim NameShp As Shape
    
    'Open publisher doc
    filename = GetFilePath & Replace(ThisWorkbook.Name, ".xlsm", ".pub")
    appPub.Open (filename)
    NameVal = "Test Name"
    

    
    '**PROBLEM HAPPENS HERE**
    '______________________________________________________________
    Set NameShp = appPub.ActiveDocument.Pages(1).Shapes.AddTextbox _
    (Orientation:=msoTextOrientationHorizontal, _
    Left:=316.8, Top:=653.04, Width:=254.88, Height:=16.68)
    '______________________________________________________________
    


    NameShp.TextFrame.TextRange.Text = NameVal
    NameShp.TextFrame.TextRange.Font.Name = "Monsterrat"
    NameShp.TextFrame.TextRange.Font.Size = 11 
    NameShp.TextFrame.TextRange.Font.Color.RGB = RGB(17, 145, 208)
    

    appPub.ActiveDocument.Close

End Sub

I have tried using integers instead of floats for Left Top Width and height, but that doesn't solve the issue either. Something in Shapes.AddTextbox isn't working, and I can't figure out why.


Solution

  • An Excel Shape is not a Publisher Shape.

    Dim NameShp As Shape
    

    is equivalent to

    Dim NameShp As Excel.Shape
    

    You need

    Dim NameShp As Publisher.Shape