So my challange is this.
I've created a Word Macro Enabled Template (MS Word 2016) which creates a document using the MailMerge function using data from an Excel file which colleagues complete.
Once completed, they add in their signature - a scanned jpeg file of their signature from a central network.
My code worked on the principle of attaching to a bookmark, unbeknown to me that when executing a MailMerge, it removes all bookmarks.
I am thinking that if I can change my code after ActiveDocument so that instead of looking for Bookmark it looks for specific text, I can then insert this text in the specific place in the Word template so when the MailMerge is executed it places their signature in that specific [text] place.
Is this possible?
Thanks in advance.
My code is
Sub CurrentUserSignature()
Dim folder As String
folder = "C:\\MacroTemp\\"
Dim path As String
path = folder & Application.UserName & ".jpg"
Dim shape As InlineShape
Set shape = ActiveDocument.Bookmarks("Signature").Range.InlineShapes.AddPicture(path, False, True)
With shape
.LockAspectRatio = msoTrue
.Height = CentimetersToPoints(4.3)
End With
End Sub
`
You could simply insert a borderless single-cell table, a Rich Text Content Control, or a Picture Content Control at the desired location in the mailmerge main document, then access each of those in the output document. If you use a borderless single-cell table, you can give it a fixed height and width so the inserted image's size is automatically constrained to fit.
Where multiple output documents are being produced, each to be signed by the same person, you might do better to insert the signature into the mailmerge main document before execution, then close that without saving changes post-merge.
Alternatively, especially if different records require different signatures and the relevant details can be gleaned from the data source, you can add field coding to the mailmerge main document to automatically add the signature images. See Managing Mailmerge Graphics in the Mailmerge Tips and Tricks thread at:
http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
or:
http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks
Related
I have a Word document that uses VBA forms to generate the document content based on selection boxes. The VBA copies and pastes from other word documents into the main document. I use explicit paths to specify where it should go find the word documents.
It would be nice to use UNC paths instead, to remove some of the "explicitness" of the file locations (c:\Files\Example\Content\PROFILE.docx) and maybe replace it with something like "..\Content\Profile.docx "
Example of the code:
Sub GenerateProfile()
Dim currentPathProfile As String
currentPathProfileText = ActiveDocument.Path
currentPathProfileText = currentPathProfileText & "c:\Files\Example\Content\PROFILE.docx"
Documents.Open FileName:=currentPathProfileText
Dim currentPathProfileDoc As Document
Set currentPathProfileDoc = Documents(1)
currentPathProfileDoc.Activate
Call CopyWholeContent
currentPathProfileDoc.Close
Call PasteWholeContent
End Sub
If the other word documents are in a relative path position to the template holding the code you can do it. It is easier if they are in a subfolder. Generate the path of ThisDocument (your code holder) and use that as a base.
ThisDocument.Path
However, rather than pull from documents, why not have the text stored in your template in the form of AutoText and use that instead? If the information is needed in more than one template, it can be stored in a global template as AutoText. This is much more flexible and less prone to problems, IMO.
Consider VBA for PowerPoint -
Is this a valid Presentations function call?
Presentations(".\directory\ppname.ppt")
Note that this will be called from within a PowerPoint presentation VBA, to open another one in a sub-directory.
The Microsoft Presentations examples (and most others) are not specific about the filename forms accepted, e.g. those using the ".", "..", "\" directives recognized in DOS scripts.
This seems to work with Powerpoint 2007.
As described in the comment, it takes a block of text (the TextRange), the starting position and length of file reference, also a LinkAddr. LinkAddr is essentially a DOS-style file reference, like "..\folder\ppfile.ppt".
The intention here is to launch another PowerPoint tool by invoking its show.ppt or show.ppsm file name. The file name can refer to some other directory using MSDOS file name stuff. I don't know whether it can span different machine platforms, but it seems to work within one Windows 10 system.
My difficulty in getting this to work was some full-path links to images, e.g. something like C:\blah\blah\image.jpg, in the Visual Basic code. PowerPoint didn't like these, instead asked about enabling macros, and just hung up when one of my file links were to be executed. By getting rid of the "macro" complaints, this suddenly started working.
Or maybe some bugs have been fixed in recent repairs to my 2007 PowerPoint tools??
Go figure...
Sub InsertLink(ByRef Trange As TextRange, fpos As Long, msglen As Long, LinkAddr As String)
' Insert an HTML link into the selected TextFrame.
' get the character range
Dim Hrange As TextRange
Set Hrange = Trange.Characters(Start:=fpos, length:=msglen)
' make it an HTML link
With Hrange.ActionSettings(ppMouseClick)
.Action = ppActionHyperlink
.Hyperlink.Address = LinkAddr
End With
End Sub
I have a word template that is filled in with information from an Access database and part of that template includes a table with two cells, type and location, for all attachments for that specific document. Within those fields are content controls, name accordingly, and then the whole table is wrapped in a repeating section content control titled "Attachments".
For each specific document there is an attachments folder that I iterate through to get the attachment type and attachment filepath. I would like to add this data to the Attachments content controls but am having issues with displaying the information correctly.
Private Sub AttachmentControl(wrdApp As Word.Application, wrdDoc As Word.Document, attachmentsFolder As String)
Dim fso As New Scripting.FileSystemObject, folderAttachments As Files, attachment As file, i As Long
Set folderAttachments = fso.GetFolder(attachmentsFolder).Files
For Each attachment In folderAttachments
If attachment.Type <> "Data Base File" Then
wrdDoc.SelectContentControlsByTag("Type").Item(1).Range.Text = attachment.Type
wrdDoc.SelectContentControlsByTag("Location").Item(1).Range.Text = attachment.Path
wrdDoc.SelectContentControlsByTag("Attachments").Item(1).RepeatingSectionItems.Item(1).InsertItemBefore
End If
Next attachment
End Sub
The attachmentFolder is where the attachments I want to iterate through are located. For my testing purposes the files within this folder are 6 jpg images named Image 1 through Image 6 but in real life applications will contain more attachments than just images.
Expected Result
Actual Result
Is this a problem with my code or am I just not correct in how this should work?
I need to find a way to automatically update some procedure documents (word docs) with fields from a spreadsheet:
There are 20 documents in total, so really I would like a single method where I can apply updates to all documents in one go.
All the documents are based on the same template; however, each contians an table (which logs the version history for that document). The content of this table is unique and the number of rows varies from document to document.
The first thing I though of was using a mailmerge; however, this would mean I woudl loose the unique tables from each document and would have to re add them, which would defeate the point of automating the process.
I have made the fields that I want to update in the word doc as Legacy Text Form Fields, and have found some VBA code which enables me to specify, that specific form fields should update from specific cells in my spreadsheet; however, I have to specify these for each docment individually.
This is the code I am using
"FormFields("Field1").Result =ActiveWorkbook.Sheets("Sheet4").Range("A2").Value"
Ideally I need a method that will work for every word doc in a given folder, identifing the related row in the spreadsheet and updating the doc accordingly. The word docs are named as the Doc ID (first column in spreadsheet).
Add DocVariables in Word. If you don't know how to do this, Google it. Then, run the script below, from Excel.
Sub PushToWord()
Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bkmk As Word.Bookmark
sWdFileName = Application.GetOpenFilename(, , , , False)
Set doc = objWord.Documents.Open(sWdFileName)
objWord.activedocument.variables("BrokerFirstName").Value = Range("BrokerFirstName").Value
objWord.activedocument.variables("BrokerLastName").Value = Range("BrokerLastName").Value
ActiveDocument.Fields.Update
objWord.Visible = True
End Sub
Finally, loop through all Word docs in a single folder.
I'm not very used to VBA language, so I come to you for help to solve my problem.
Basically, what I want to do is, while having a MS Word 2010 document opened, open a new one, set the focus on it and do stuff with it.
The problem I'm facing is that I can effectively open the new document, but the .activate function to set the function on it doesn't work, as the instructions following it are still executed in the first document.
Here is my code:
Private Sub BOK_Click()
*...instructions...*
'Opens a new document
Application.Documents.Add
'Select the latest opened document and sets the focus on it
Application.Documents(Application.Documents.Count).Activate
* do stuff *
End Sub
If it may help, the full context is :
I have a main template with 6 pre-filled templates in it, and following the choice of the user (from a drop-down form) it selects the right pre-filled template and opens it in a new Word document, then closed the main template.
What Documents.Add does:
Create a window
Render a copy of the provided (or Normal, in this case) template
Display it in the window
Raise the index of all open documents by 1
Assign index 1 to the newly added document
Bring the window to the front and give focus to it
There's (usually) no need to explicitly activate a document you just added. If you must or want to use Activate, it's best practice to reference a document by it's name, since indexes tend to drift (as described above).
doc1 = ActiveDocument.Name
Documents.Add
doc2 = ActiveDocument.Name
' Do something with document2
Documents(doc1).Activate
' Do something with document1
Documents(doc2).Activate
' Do something with document2 again