none
Word-add in which runs on open or close RRS feed

  • Question

  • I Nedd to deploy this and add-in seems to be the easiest way so how would I go about doing that I know I have to But the code i want to run in a module but how would I call it to a word document is it something like this? If not please correct me!

    This is the code I Should put in this doucment I believe am not too sure I did something likewise with an excel add-in

    Option Explicit
    
    Private WithEvents app As Application
    
    Private Sub app_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
        Call CreateHyperlinks
      
    End Sub
    
    Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
        Call CreateHyperlinks(Wb)
    
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Set app = Application
    End Sub
    
    Private Sub Workbook_Open()
        Set app = Application
    End Sub

    And this code would go into the Module but I dont know what to change to get it to run I know am missing something here:

    Sub CreateHyperlinks()
    Application.ScreenUpdating = False
    Dim vFindText As Variant, i As Long, Rng As Range
    vFindText = "ABC-????"
    vFindText = Split(vFindText, ",")
    For i = LBound(vFindText) To UBound(vFindText)
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Format = False
          .MatchAllWordForms = False
          .MatchWholeWord = True
          .MatchWildcards = True
          .Wrap = wdFindStop
          .Text = vFindText(i)
          .Replacement.Text = ""
          .Execute
       End With
        Do While .Find.Found
          Set Rng = .Duplicate
          Rng.Hyperlinks.Add Anchor:=Rng, Address:="www.google.com" & Rng.Text, _
            SubAddress:="", ScreenTip:="Click Here", TextToDisplay:=Rng.Text
          .Start = Rng.End + 1
          .Find.Execute
        Loop
      End With
    Next i
    Application.ScreenUpdating = True
    End Sub

    Please help :D


    Tuesday, November 5, 2013 10:43 AM

Answers

  • Based on the code you show us for Excel, I'd say you need to put the code in the ThisDocument class of the Normal.dotm template.

    Option Explicit
    
    Private WithEvents app As Application
    
    Private Sub app_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
        Call CreateHyperlinks
      
    End Sub
    
    Private Sub app_DocumentOpen(ByVal Doc As Document)
        Call CreateHyperlinks(Doc)
    
    End Sub
    
    Private Sub Document_BeforeClose(Cancel As Boolean)
        Set app = Application
    End Sub
    
    Private Sub Document_Open()
        Set app = Application
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by DuHelloBicBoI Thursday, November 7, 2013 10:31 AM
    Wednesday, November 6, 2013 7:03 PM
    Moderator

All replies

  • THe Word APIs have a DocumentOpen event that you'd implement the same way as the Excel event you show, using WithEvents. The event signature:

       DocumentOpen(Doc As Document)


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, November 5, 2013 5:38 PM
    Moderator
  • Thats great, but am not sure on how to implement it correctly, am still very new to the whole VBA/Office development scene ave tried to implement it but still nothing can you show me where to put the code exactly, my current version w rks but as it has to be deployed using a add-in would be far easier.

    This would an absolute massive help!Many Thanks"

    I had a simlar issue with my excel Add-in.

    Wednesday, November 6, 2013 8:38 AM
  • Based on the code you show us for Excel, I'd say you need to put the code in the ThisDocument class of the Normal.dotm template.

    Option Explicit
    
    Private WithEvents app As Application
    
    Private Sub app_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
        Call CreateHyperlinks
      
    End Sub
    
    Private Sub app_DocumentOpen(ByVal Doc As Document)
        Call CreateHyperlinks(Doc)
    
    End Sub
    
    Private Sub Document_BeforeClose(Cancel As Boolean)
        Set app = Application
    End Sub
    
    Private Sub Document_Open()
        Set app = Application
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by DuHelloBicBoI Thursday, November 7, 2013 10:31 AM
    Wednesday, November 6, 2013 7:03 PM
    Moderator
  • Thank You :)
    Thursday, November 7, 2013 10:32 AM