-
Dec 15th, 2017, 06:44 PM
#1
Thread Starter
Frenzied Member
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
-
Dec 15th, 2017, 08:37 PM
#2
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
-
Dec 17th, 2017, 11:46 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Dec 17th, 2017, 02:21 PM
#4
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|