2021年5月3日星期一

How do I insert a building block that will be chosen based on Environ("USERNAME")?

It's a long while since I've written vba script and I am struggling! Any assistance would be gratefully received as the code that I tried to write has been almost wholly unsuccessful.

The purpose of the code is to insert in the current Word doc, a signature para based on who is logged in to the computer. The signature paras are building blocks (custom quick parts in a category named 'Sign-off') and have been created/saved using the same name the authors will have as Environ("USERNAME"). The signature image files that are later inserted are all named the same but each user will have their confidential image file saved to an Environ("USERPROFILE") folder.

The building blocks include a bookmark named Signature and, once the building block has been inserted and that bookmark exists, the confidential signature image should be inserted at that bookmark.

I'm embarrassed to include the code I've written but here it is:

Dim AuthorName As String  Dim objTemplate As Template  Dim objBB As BuildingBlock  Dim SignatureImage As String     Set AuthorName = Environ("USERNAME")  Set objTemplate = Environ("USERPROFILE") & "\AppData\Roaming\Microsoft\Templates\Normal.dotm"  Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustomQuickParts).Categories("Sign-off").BuildingBlocks("AuthorName")  Set SignatureImage = Environ("USERPROFILE") & "\AppData\Roaming\Microsoft\Templates\Signature.jpg"        If Application.ActiveDocument.Bookmarks.Exists("SignatureBlock") = True Then          Application.ActiveDocument.Bookmarks.Item("SignatureBlock").Select          objBB.Insert Selection.Range      End If            If Application.ActiveDocument.Bookmarks.Exists("Signature") = True Then          Application.ActiveDocument.Bookmarks.Item("Signature").Select          SignatureImage.Insert Selection.Range      End If  
https://stackoverflow.com/questions/67377562/how-do-i-insert-a-building-block-that-will-be-chosen-based-on-environusername May 04, 2021 at 09:07AM

没有评论:

发表评论