Getting text from a word file from placeholder bookmarks using VBA


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Getting text from a word file from placeholder bookmarks using VBA

  1. #1
    Join Date
    Jun 2008
    Posts
    4

    Getting text from a word file from placeholder bookmarks using VBA

    Hi,

    I am trying to rename Microsoft Word files using some of the data contained within the word file itself. The files have originally been created using Word 95 templates and so the bookmarks are placeholders and do not let me use the range function. (arrgh) - I am using VBA btw as that is all i have availabe to me.

    The code i have so far is:

    Dim strOriginalFileName As String
    Dim strNo As String
    Dim strNewFullName As String
    ' open word application instance and create list for storing document names
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add()

    objWord.Visible = True

    ' scan through directory for all files
    objWord.FileSearch.fileName = "nd*.doc"
    objWord.FileSearch.LookIn = "R:\Proton Bank\"
    objWord.FileSearch.SearchSubFolders = True
    objWord.FileSearch.Execute

    For Each objFile In objWord.FileSearch.FoundFiles
    ' open each document to allow bookmark searching
    Set objCurrentDoc = objWord.Documents.Open(objFile, , ReadOnly)
    ' save object name ready for renaming to temp file and path
    strOriginalFileName = objCurrentDoc.Name
    strPath = objCurrentDoc.Path

    objCurrentDoc.Activate
    objCurrentDoc.Bookmarks("num").Select

    Selection.MoveRight Unit:=wdWord, Count:=2

    strHospNo = Selection.Text

    strNewFullName = strPath + "\" + strDate + " - " + strNo + " - " + strOriginalFileName

    ' objDoc.SaveAs (strNewFullName)
    objCurrentDoc.Close
    Next

    objWord.Quit
    The problem is i can't get the text after the bookmark and then save to a variable for use in the renaming (the bit in yellow). It seems that the selection function is not working and i have no idea why. is there another/better method for getting the text other than Selection.Text???

    Any help would be appreciated

    Confused

    PieEater

  2. #2
    Join Date
    May 2008
    Location
    Chicago, IL
    Posts
    64
    you're missing the reference to Word

    Code:
    objWord.Selection.MoveRight Unit:=wdWord, Count:=2
    David CodeGuru Article: Bound Controls are Evil-VB6

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2008
    Posts
    4
    Hi there,

    this is great, it now jumps to the end of the word. (goes over the word i want) but how do i get it to highlight it so i can select the text?

    Thanks

    PieEater

  4. #4
    Join Date
    Jun 2008
    Posts
    4
    Found it!

    objWord.Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    So, does this mean your issue is resolved, or do you still have questions?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Importing text file using schema.ini
    By Kevin in forum VB Classic
    Replies: 3
    Last Post: 12-05-2005, 06:25 PM
  2. Replies: 3
    Last Post: 08-13-2005, 01:55 PM
  3. File Locking a Text File on a Network Drive
    By Miles Tones in forum VB Classic
    Replies: 1
    Last Post: 05-20-2005, 01:57 PM
  4. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center