Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2016, 05:26 AM
neoported neoported is offline INCLUDETEXT is file doesn't exist Windows 10 INCLUDETEXT is file doesn't exist Office 2013
Novice
INCLUDETEXT is file doesn't exist
 
Join Date: Jul 2016
Posts: 2
neoported is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-19-2016, 10:22 PM
gmayor's Avatar
gmayor gmayor is offline INCLUDETEXT is file doesn't exist Windows 10 INCLUDETEXT is file doesn't exist Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 07-21-2016, 11:47 PM
neoported neoported is offline INCLUDETEXT is file doesn't exist Windows 10 INCLUDETEXT is file doesn't exist Office 2013
Novice
INCLUDETEXT is file doesn't exist
 
Join Date: Jul 2016
Posts: 2
neoported is on a distinguished road
Default

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
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Just need to tinker with that now to join your code to it. I'm sure I'll get there in the end
Reply With Quote
Reply



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 is file doesn't exist 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
INCLUDETEXT is file doesn't exist 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft