#1
|
|||
|
|||
INCLUDETEXT is file doesn't exist
I'm looking to include a merge field that pics up a Windows user's signature based on their login name and whilst I've got a macro to pick up the username and replace the @@WinUsername@@ bit.
It's working if the user's signature file exists but it produces an "A field calculation error occurred in record 1" if the file doesn't exist and it's looking to display the default.docx file. It then includes the default.docx file as required. Just looking to get rid of the error message. So, inspite of my ramblings I'd like to inclde a file if it exists and if it doesn't to include a default file instead of the Error file not found message. Is this possible. Below is my work in progress {IF { INCLUDETEXT "Q:\\Letters\\Signatures\\@@WinUsername@@.docx "} ="Error*" { INCLUDETEXT "Q:\\Letters\\Signatures\\default.docx"} { INCLUDETEXT "Q:\\Letters\\Signatures\\@@WinUsername@@.docx "} } Thanks in advance |
#2
|
||||
|
||||
If you are using a macro to get the username, then why not use the macro to test if the file exists and write the appropriate field to the merge document? E.g. as follows, which wites to the cursor, though it wouldn't be much of a stretch to write it to a bookmark or specific range.
Code:
Sub WriteField() Dim strName As String strName = Environ("UserName") If FileExists("Q:\Letters\Signatures\" & strName & ".docx") Then ActiveDocument.Fields.Add Selection.Range, wdFieldIncludeText, """Q:\\Letters\\Signatures\\" & strName & ".docx""", False Else ActiveDocument.Fields.Add Selection.Range, wdFieldIncludeText, """Q:\\Letters\\Signatures\\default.docx""", False End If End Sub Private Function FileExists(strFullName As String) As Boolean 'Graham Mayor - http://www.gmayor.com 'strFullName is the name with path of the file to check Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(strFullName) Then FileExists = True Else FileExists = False End If lbl_Exit: Set fso = Nothing Exit Function End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Excellent, thanks for the pointers
The macro I'm currently using is Code:
'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Dim WinUsername As String 'Replace the text @@WinUsername@@ with the environment variable username ActiveWindow.View.ShowFieldCodes = True WinUsername = Environ("Username") Selection.WholeStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "@@WinUsername@@" .Replacement.Text = WinUsername .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll ActiveWindow.View.ShowFieldCodes = False '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copy and paste file but folder doesn't update | Haha88 | Windows | 2 | 09-24-2015 09:54 AM |
INCLUDETEXT dynamic file name? | kwstewart | Mail Merge | 3 | 08-17-2015 09:04 AM |
A text box (active X) control doesn't work on reopening the file | Chris Baker | PowerPoint | 1 | 06-07-2013 05:51 AM |
The printed pdf file doesn't print an equation inside text box! why? | Jamal NUMAN | Word | 4 | 07-01-2011 06:27 PM |
[PowerPoint 2010] Change font for all slides in an exist *.PPTX file | LongTTH | PowerPoint | 0 | 12-15-2010 02:07 AM |