Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    insert text file into a template (English/Word 2000)

    I am very new to Word 2000 and VBA but I have managed to make a few templates with customized user boxes that actually work!
    Right now, I have another template with a custom user box. The user box has about 6 text boxes for various information to be put on the final document, but at a bookmark that I have named "email", I want to insert a notepad .txt file that has the user's email address. I don't want to put a text box in the user box with this field because it never changes; I thought putting static text from a notepad file into the code would be easier; Below is what I tried, but I keep getting a compile error that says "Expected Function or Variable:
    With Active Document:
    .Bookmarks("Email").Range.Text = Selection.InsertFile("C:Email Address.txt")
    End With
    End Sub

    So then I did a little more research and I tried:

    With Active Document:
    .Bookmarks("Email").Range.Text = Selection.InsertFile Filename="C:Email Address.txt"
    End With
    End Sub

    Now I am getting a compile error that says "Expected: End of statement

    What am I doing wrong or is there another way to do this? This is all trial and error for me but today it's mostly error! <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: insert text file into a template (English/Wor

    <P ID="edit" class=small>(Edited by jscher2000 on 01-Dec-04 15:50. Sorry, there was a space in ActiveDocument and a colon missing in the parameter assignment...)</P>You can't actually set the .Text equal to the InsertFile method of the Selection, because the InsertFile method doesn't return a string.

    Try this:

    With ActiveDocument
    .Bookmarks("Email").Range.InsertFile Filename:="C:Email Address.txt"
    End With

    Does it work? (You can set the ConfirmConversions parameter to avoid the pop-up box if you like.)

  3. #3
    New Lounger
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: insert text file into a template (English/Wor

    Ohmygosh! A breakthrough finally! Thank you so much! I tried it and didn't get a compilation error. Only when I ran the template, I got an error saying it could not find the file "email.txt". I know it's in the right directory. It's not hidden and it's not misspelled. Any other suggestions. Thanks again.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: insert text file into a template (English/Wor

    I know you have checked the path and filename, but I would double check it. Also check if the quotes are placed correctly in your code.
    I just tested Jefferson's code with a text file named "Email Address.txt" and it worked fine.

  5. #5
    New Lounger
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: insert text file into a template (English/Wor

    Thanks for the reply. I double checked everything, and I even had someone else look at it to make sure I had the path right, but when I run the template I get an error. The exact error is:

    Run-time error '5174'

    This file could not be found.
    Try one or more of the following:
    *Check the spelling of the name of the document.
    *Try a different file name.
    (False)

    What does (False) mean?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: insert text file into a template (English/Wor

    I have no idea what (False) means in this context.

    This is very strange. Could you try the following:
    - Give the file another name and change the code accordingly. By preference, give it a name without spaces.
    - Move the file to another location and change the code accordingly.
    Do you still get the same error message?

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: insert text file into a template (English/Wor

    I'm not sure what is happening with your code. Is the document in a path that contains a space anywhere in the name? This will throw certain functions, but Office's built-in methods usually can handle that. You could try adding extra quotation marks inside the path string, a pair on each end, to see if that helps. For example:

    <code><big>"""c:my pathwith spacessome file name.txt"""</big></code>

    Another option would be to make it interactive (show a File>Open or Insert>File dialog), but I understand that your goal is to make it seamless, so I won't bother illustrating that.

  8. #8
    New Lounger
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: insert text file into a template (English/Wor

    Thanks to all for the replies. I didn't get to work on it yesterday, but I did a little this morning. I have tried everything. I am going to start over from scratch and test the template after every line of code to see if that helps to isolate the problem.

  9. #9
    New Lounger
    Join Date
    Dec 2004
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: insert text file into a template (English/Wor

    SUCCESS AT LAST! I did the whole darn thing over again and it works! I printed out the code before I erased it and I printed out the new code. I can't find any differences! So I don't know what happened there. I do have a new problem though, the text that was inserted is formatted in the Notepad font. I had hoped it would pick up the font that is in the Word doc. Any suggestions as to how to fix this problem?

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: insert text file into a template (English/Wor

    You can reset the style for the inserted text:

    Dim lngLenBefore As Long
    Dim lngLenAfter As Long

    lngLenBefore = ActiveDocument.StoryRanges(wdMainTextStory).End
    ActiveDocument.Bookmarks("Email").Range.InsertFile FileName:="C:Email Address.txt"
    lngLenAfter = ActiveDocument.StoryRanges(wdMainTextStory).End

    With ActiveDocument.Range(ActiveDocument.Bookmarks("Ema il").End, _
    ActiveDocument.Bookmarks("Email").End + lngLenAfter - lngLenBefore)
    .Style = wdStyleNormal
    .Font.Reset
    End With

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: insert text file into a template (English/Wor

    <P ID="edit" class=small>(Edited by jscher2000 on 03-Dec-04 15:09. Second version uses Hans' method of measuring the change to the document. Much better.)</P>This is a little complicated. My thought was to extend the bookmark to cover the new material, but I found that counting characters was not completely reliable (the Bookmark's .End seems to count paragraph marks differently than the document's .Characters.Count). So I wrote this probably unnecessarily complicated code. I'm sure someone will have a better idea for how to style the inserted text.

    <code>Sub InsertTextToBmkAndFormat()
    Dim lngChars As Long, lngParas As Long
    With ActiveDocument
    lngChars = .Characters.Count
    lngParas = .Paragraphs.Count
    With .Bookmarks("Email")
    .Range.InsertFile FileName:="C:Email Address.txt"
    .End = .End + (ActiveDocument.Characters.Count - lngChars) + _
    (ActiveDocument.Paragraphs.Count - lngParas - 1)
    .Range.Style = wdStyleNormal
    End With
    End With
    End Sub</code>

    Better version:

    <code>Sub InsertTextToBmkAndFormat()
    Dim lngOldEnd As Long
    With ActiveDocument
    lngOldEnd = .Content.End
    With .Bookmarks("Email")
    .Range.InsertFile FileName:="C:Email Address.txt"
    .End = .End + (ActiveDocument.Content.End - lngOldEnd)
    .Range.Style = wdStyleNormal
    End With
    End With
    End Sub</code>

  12. #12
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: insert text file into a template (English/Wor

    Here's a calculation-free alternative (courtesy of the MVPs website):
    <pre> Dim rngStart As Word.Range
    Dim rngEnd As Word.Range
    Dim rngX As Word.Range
    Set rngStart = ActiveDocument.Bookmarks("Email").Range
    rngStart.Collapse wdCollapseStart
    Set rngEnd = rngStart.Duplicate
    rngEnd.InsertParagraph
    rngStart.InsertFile "C:Email Address.txt"
    rngEnd.Characters.Last.Delete
    Set rngX = ActiveDocument.Range(rngStart.Start, rngEnd.End)
    rngX.Style = ActiveDocument.Styles("Normal")
    Set rngStart = Nothing
    Set rngEnd = Nothing
    Set rngX = Nothing
    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •