Friday, December 07, 2012 12:56 AM
We have several Micrsoft Word templates that were developed prior to the release of Word 2007/2010. These templates use an Attachment tool to embed several different documents and maps. When opened in compatibility mode they will not open pdf documents. I have found a solution using vba (below). My question, how can I create a button/add-in that will replace the the code with the code below. The name of the procedure in the old version is the same as this new one?
Private Sub GetAttachment() Dim Doc As String Dim Doc2 As String Dim WordDoc As Boolean On Error GoTo Error_Handler ActiveDocument.Unprotect ("sampson") Doc = Application.ActiveWindow.Caption ActiveDocument.Bookmarks.Add Name:="LeftOff" Selection.GoTo what:=wdGoToBookmark, Name:=CommandBars.ActionControl.Parameter Selection.InlineShapes(1).OLEFormat.Activate WordDoc = False If Application.ActiveWindow.Caption <> Doc Then Doc2 = Mid(Application.ActiveWindow.Caption, 1, InStr(Application.ActiveWindow.Caption, " [Compatibility Mode]") - 1) WindowsIndex = 0 Do While Application.ActiveWindow.Caption <> Doc WindowsIndex = WindowsIndex + 1 Documents(WindowsIndex).Activate Loop WordDoc = True End If Selection.GoTo what:=wdGoToBookmark, Name:="LeftOff" ActiveDocument.Bookmarks("LeftOff").Delete Error_Handler: If Err.Number <> 0 Then MsgBox (Err.Description) End If ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="sampson" If WordDoc Then Documents(Doc2).Activate End If End Sub
Friday, December 07, 2012 1:33 AM
Can anyone point me to information that will explain how to create an executable in VB that will replace the word module with an updated module?
Friday, December 07, 2012 1:16 PMModerator
I hesitate to reply here, as I really haven't the time to go into nitty gritty details...
There is an automation interface for the VBA Editor (VBE) that will allow you to change the content of the code project associated with an Office file.
In order for it to work, it's necessary for you to change the Trust Center setting (we're talking 2007 and later) that allows access to the VBA object model. Installation default is to not allow as this is obviously a security risk.
In the VBA Editor Tools/References you need to set a reference to the "Microsoft Visual Basic for Applications Extensibility 5.3" library. Once you've set that, you can see the various objects, properties, etc. in the VBA Object Browser (F2).
There used to be a Language Reference for it, but I don't find anything on MSDN except for VB6 (which VBA inherits from): http://msdn.microsoft.com/en-us/library/aa443983(VS.60).aspx.
You'll probably find this website very useful: http://www.cpearson.com/excel/vbe.aspx
Cindy Meister, VSTO/Word MVP, my blog
- Marked As Answer by JamesLNeal Monday, December 10, 2012 1:17 PM
Monday, December 10, 2012 1:16 PMThank you Cindy!