Results 1 to 11 of 11

Thread: Reading Microsoft Word Documents

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Reading Microsoft Word Documents

    Hi all,

    I'm trying to read a word document for the purpose of obtaining a word count, I realise Word has built in functionality for presenting a word count but I want to write a little app that will omit certain parts of the document from the word count.

    So far I have tried this code to open the document but I am getting an error 'Word.Document cannot be found' and 'Microsoft.Office.Interop cannot be found'. I have added a reference to the Microsoft Office 12.0 Object Library under the COM tab. I have Office 2007 installed and I'm using VB2005.

    Code:
    Imports Microsoft.Office.Interop
    
    Dim appWord As New Microsoft.Office.Core.Application
    Dim docWord As New Word.Document
    
    docWord = appWord.Documents.Open("c:\test.doc")
    Any ideas?
    Chris

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Reading Microsoft Word Documents

    Try this...

    Code:
    '~~> Set reference to Microsoft 12.0 Object Library
    
    Private Sub cmdOpen_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles cmdOpen.Click
        Dim strFileName As String, strFilePath As String, strFileTitle As String
        Dim word_server As Word.Application
    
        Application.DoEvents()
    
        strFileName = "c:\test.doc"
        strFileTitle = strFileName.Substring(strFileName.LastIndexOf("\") + 1)
        strFilePath = strFileName.Substring(0, strFileName.LastIndexOf("\"))
    
        word_server = New Word.Application()
        
        word_server.ChangeFileOpenDirectory (strFilePath)
        
        word_server.Documents.Open(FileName:=strFileTitle, _
        ConfirmConversions:=False, ReadOnly:=False, _
        AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, _
        WritePasswordDocument:="", WritePasswordTemplate:="", _
        Format:=Word.WdOpenFormat.wdOpenFormatAuto)
    
        word_server.Visible = True
    
        '~~> What ever you want to do here...
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Reading Microsoft Word Documents

    Hi koolsid, thanks for the quick reply...

    Unfortunately I am still getting the same error (Word.Application is not defined) on the line:

    Code:
    word_server = New Word.Application()
    I have selected the reference to MS Office 12 Object Library, how can I verify this is attached to my project?
    Chris

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Reading Microsoft Word Documents

    I have selected the reference to MS Office 12 Object Library, how can I verify this is attached to my project?
    The moment you type

    word_server = New Word.

    Intellisense should give you Application()

    or when you type

    Dim word_server As

    Intellisense should give you Word.Application

    Edit: How are you selecting the reference?
    Last edited by Siddharth Rout; Oct 29th, 2009 at 01:36 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: Reading Microsoft Word Documents

    I have selected the reference to MS Office 12 Object Library, how can I verify this is attached to my project?
    Are you sure you have the correct PIA's (Primary Interop Assemblies)installed on your system?

    PIA Download: http://www.microsoft.com/downloads/d...displaylang=en

    ^^For office '07
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

  6. #6

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Reading Microsoft Word Documents

    Quote Originally Posted by BackWoodsCoder View Post
    Are you sure you have the correct PIA's (Primary Interop Assemblies)installed on your system?

    PIA Download: http://www.microsoft.com/downloads/d...displaylang=en

    ^^For office '07
    I installed this but nothing has changed - even after a reboot. I'm not sure if they installed correctly though because there was no 'finished' or 'success' message, after running the setup the progress bar advances then the window disappears. Could be a problem with Vista?

    Quote Originally Posted by koolsid
    How are you selecting the reference?
    All I do is go to Project>Add Reference>COM>Select Microsoft Office 12.0 Object Library>OK
    Chris

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Reading Microsoft Word Documents

    What about the intellisense question in post 4?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Reading Microsoft Word Documents

    Hi mate, There's no item in the intellisense list called 'Word'. VS underlines Word.Application with the error 'Word.Application is not defined'.
    Chris

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Reading Microsoft Word Documents

    Just a stupid doubt...

    How many office versions are installed in your pc? What I mean is do you have 2003 and 2007 together?

    If no then can you upload your project?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Reading Microsoft Word Documents

    Good point, I have only Office 2007, but I do have MS Office Powerpoint Viewer 2003. Could that be causing the problem?
    Last edited by the182guy; Oct 30th, 2009 at 08:52 AM.
    Chris

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Reading Microsoft Word Documents

    All I do is go to Project>Add Reference>COM>Select Microsoft Office 12.0 Object Library>OK
    Quote Originally Posted by the182guy View Post
    Good point, I have only Office 2007, but I do have MS Office Powerpoint Viewer 2003. Could that be causing the problem?
    Ok post a snapshot of the screen when you are adding references....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width