none
Trouble with ribbon button script RRS feed

  • Question

  • I've been asked to make a simple and quick way for our users to insert logos into Office 2007 and 2010 documents. I was able to get everything done via macros but deploying macros company wide it not ideal. So I've been working with VSTO and reading everything I can online but I just can't seem to get the ribbon button to insert a picture. I do have my references added and I was able to get my code to build successfully but nothing happens. I've used similar code to insert text commands just fine. I feel like I'm close to seeing what I'm missing so any help would be greatly appreciated.

    Public Class Ribbon1

        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Dim instance As Word.InlineShapes
            Dim FileName As String = "c:\Logos\logo_Small.png"
            Dim LinkToFile As Object = False
            Dim SaveWithDocument As Object = True
            Dim returnValue As Word.InlineShape

            returnValue = instance.AddPicture(FileName, _
             LinkToFile, SaveWithDocument, )
        End Sub
    End Class

    Friday, February 1, 2013 4:41 PM

Answers

  • I think the (a) problem is that you're not assigning anything to the object instance. You declare it, give it a data type, but you don't assign anything to it. For example

       instance = Globals.ThisAddin.Application.ActiveDocument.InlineShapes


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by J_Furmanek Friday, February 1, 2013 7:07 PM
    Friday, February 1, 2013 5:36 PM
    Moderator

All replies

  • I think the (a) problem is that you're not assigning anything to the object instance. You declare it, give it a data type, but you don't assign anything to it. For example

       instance = Globals.ThisAddin.Application.ActiveDocument.InlineShapes


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by J_Furmanek Friday, February 1, 2013 7:07 PM
    Friday, February 1, 2013 5:36 PM
    Moderator
  • you just declared variables, you haven't asseigned them to any values. Paste here original vba macro that works and we will see aboyut converting this to vb.net code.
    Friday, February 1, 2013 5:38 PM
  • Thanks for the quick replies. I'm trying to give myself a crash course in VB while doing this. I was thinking I needed to add activedocument somewhere so it would know to use the current open file, but didn't know where to put it.

    Macro Code:

    Sub Logo_Small()
    '
    ' Logo_Small Macro
    '
    '
        Selection.InlineShapes.AddPicture FileName:= _
            "c:\Logos\logo_Small.png", LinkToFile:=False, _
            SaveWithDocument:=True
    End Sub

    Friday, February 1, 2013 6:24 PM
  • Hi J_Furmak

    Try the code I gave you, placing it before the line starting with returnValue.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, February 1, 2013 6:28 PM
    Moderator
  • Hi J_Furmak

    Try the code I gave you, placing it before the line starting with returnValue.


    Cindy Meister, VSTO/Word MVP, 

    Awesome! thank you thank you :) It's all slowly making sense again, haven't had to write any code since college.
    Friday, February 1, 2013 7:07 PM
  • If anyone happens to take a look back in here, didn't want to start another topic. My button works now thanks to the help from Cindy. However unlike my macro the vbs script is always placing the image at the start of the document. I would like to have it insert at the cursor location. I'm thinking I have to use the range or selection expression but I'm not sure how. I have found a good article http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/4db5a5f0-c0f9-490c-a443-143d9d0034dd/ but I'm not seeing where to adapt it to my code
    Saturday, February 2, 2013 5:44 PM
  • Please start a new discussion for this in the Word forum:
      http://social.msdn.microsoft.com/Forums/en-US/worddev/threads

    When you post, please also include the code as it currently stands. If you think anything in this discussion is relevant to the issue, you can include a link back to it.

    How graphics are handled depends very much on Word, and the version involved. You'll find many more Word specialists in the Word forum than in VSTO.

     

    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, February 3, 2013 7:49 AM
    Moderator