Results 1 to 4 of 4

Thread: Chnaging word in a Word document

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Chnaging word in a Word document

    I'm trying to make this work (bFile is the name of a file passed from elsewhere in the main program, and this subroutine is called multiple times in a loop to change a group of Word documents). I would appreciate it if someone could see where I am going wrong, still using my basic concept. The message I'm getting is that the document is not open for changing.

    Code:
    Private Sub DocChanger(bFile) 'put Word document changes here ### to do
        Set wrdApp = CreateObject("word.application")
        With wrdApp
          .Documents.Open (bFile)
          .WindowState = wdWindowStateMaximize
          .Visible = False
        End With
        With ActiveDocument.Sections(1).Range.Find
           .Text = "AND"
           .Replacement.Text = "BUT"
           .Execute Replace:=wdReplaceOne
        End With
        Set wrdApp = Nothing
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Chnaging word in a Word document

    do you already have the document open?

    unless you are going to use the Call keyword, bfile should not be in parenthesis
    should be
    Code:
    With wrdApp.ActiveDocument.Sections(1).Range.Find
    or better set the document to a document object variable and work with that

    for testing, change .visible to true, to see if the document does open correctly, not read only etc
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Chnaging word in a Word document

    Do you mean the error message is "Document is Read Only as its already opened by another user/process?

    If so check your task manager for any hidden instances of word from your debugging sessions and close them.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Chnaging word in a Word document

    Thanks both. West: Yes this routine is being called, hence the bFile. Rob: Yes, on checking the task manager, I find there are files left open from previous sessions as suggested. Will work on this further today.

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