|
-
Mar 18th, 2007, 01:12 PM
#1
[RESOLVED] Why does this work only once
The following code copies whatever text is selected then adds it to a newly-created Word doc, removes numbering (as in bullet numbering) and then replaces what is on the clipboard with the modified text. It works fine when Word isn't already open. However if Word is already open it works fine the first time but after that the content of the clipboard isn't changed. Can anybody tell me why?
vb Code:
SendKeys "^c", True
With wdApp
.Documents.Add , , wdNewBlankDocument, True
.Documents(1).Activate
DoEvents
.Selection.PasteAndFormat wdPasteDefault
.Selection.WholeStory
.Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
.Selection.Copy
.Documents(1).Close SaveChanges:=wdDoNotSaveChanges
If .Documents.Count = 0 Then
.Quit
Set wdApp = Nothing
End If
-
Mar 18th, 2007, 01:15 PM
#2
Member
Re: Why does this work only once
Not an expert so if im wrong just tell me and ill delete it
I think if you clear the clipboard in your code it will work
like...
as soon as the text copies an event clears the clipboard and starts over
Just a thought
-
Mar 18th, 2007, 01:21 PM
#3
Re: Why does this work only once
 Originally Posted by blckoutdfndr
Not an expert so if im wrong just tell me and ill delete it
I think if you clear the clipboard in your code it will work
like...
as soon as the text copies an event clears the clipboard and starts over
Just a thought
Why then does it work when Word isn't open?
-
Mar 18th, 2007, 02:17 PM
#4
Re: Why does this work only once
That's a possibility, as you often need to have "Clipboard.Clear" when using VB's clipboard functions, but I suspect it isn't the issue here..
One problem is that you are referencing the document by position, and if any others were already open this position will not be correct. I think you also need to Select the document (or perhaps a range in it) in addition to Activating it.
Untested, but I think this will be better:
vb Code:
SendKeys "^c", True
Dim wdDoc as Word.Document 'or "as Object" for late bound
With wdApp
Set wdDoc = .Documents.Add (, , wdNewBlankDocument, True)
wdDoc.Activate
wdDoc.Select
DoEvents
.Selection.PasteAndFormat wdPasteDefault
.Selection.WholeStory
.Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
.Selection.Copy
wdDoc.Close SaveChanges:=wdDoNotSaveChanges
Set wdDoc = Nothing
If .Documents.Count = 0 Then
.Quit
Set wdApp = Nothing
End If
Thank, I'll try that but I checked and Word always (at least the way I'm using it) seems to add the new doc as Documents(1).
Last edited by MartinLiss; Mar 18th, 2007 at 02:25 PM.
-
Mar 18th, 2007, 02:23 PM
#5
Re: Why does this work only once
I would say that since you are hardcoding in a document index number instead of a name or using a document object variable, it is pasting the text into the first document and not the added document.
I will test out your code and see if I can figure it out...
Edit: lol, all the points I was going to make in the utility thread.
Martin, you can compare my spellchecker code to what you are using too to see the differences.
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 
-
Mar 18th, 2007, 02:28 PM
#6
Re: Why does this work only once
I don't think the problem has anything to do with your code as such - If you stick Debug.Print Clipboard.GetData after the SendKeys line you'll see that the text just isn't being copied. When word (or any office app) is open the office clipboard takes over, and perhaps the extra layer (whatever office does) increases the chance of (the already flaky) SendKeys falling over. When I was trying out your code I found the behaviour to be sporadic.
-
Mar 18th, 2007, 02:33 PM
#7
Re: Why does this work only once
Here is my spellchecker code to eitehr start a new or attach to an existing word app object:
vb Code:
Public Sub InitializeMe()
On Error Resume Next
'<INITIALIZE WORD>
Set moApp = GetObject(, "Word.Application")
If TypeName(moApp) <> "Nothing" Then
Set moApp = GetObject(, "Word.Application")
Else
Set moApp = CreateObject("Word.Application")
End If
End Sub
And to use a sure fire way of copying the correct document data using the range.copy instead... (setting your added doc to a document object variable)
vb Code:
Clipboard.Clear
oDoc.Select
'Do your bullet removal stuff
oDoc.Range.Copy
sReplace = Clipboard.GetText(1)
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 
-
Mar 18th, 2007, 02:35 PM
#8
Re: Why does this work only once
Si, I see I edited your post above instead of replying to it Sorry about that but in any case I just tried your modification and there's no change. With Word open it works only once.
-
Mar 18th, 2007, 02:38 PM
#9
Re: Why does this work only once
Rob as far as opening Word I'm doing the following which I believe works just as well.
vb Code:
hwnd = FindWindow("OpusApp", vbNullString)
If hwnd = 0 Then
Set wdApp = New Word.Application
Else
Set wdApp = Word.Application
End If
I'll try your other change however.
-
Mar 18th, 2007, 02:41 PM
#10
Re: Why does this work only once
Yes it does but its also using early binding which may create issues when other members try it as the reference will be broken.
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 
-
Mar 18th, 2007, 02:55 PM
#11
Re: Why does this work only once
Thanks. I'll change that.
The problem may be due to the way Word (or I) handles the clipborad because after adding ClipBoard.Clear I get a Word error 4605 "This method or property is not available because the clipboard is empty or not valid".
-
Mar 18th, 2007, 02:56 PM
#12
Re: Why does this work only once
First attempt when word is alread running with one document in it...
Prompts to close/save document1.
Quits Word.
Seems its always closing word if its already open. Looks like it needs a boolean flag to prevent it from getting quit when already was running.
More testing...
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 
-
Mar 18th, 2007, 03:04 PM
#13
Re: Why does this work only once
 Originally Posted by RobDog888
First attempt when word is alread running with one document in it...
Prompts to close/save document1.
Quits Word.
Seems its always closing word if its already open. Looks like it needs a boolean flag to prevent it from getting quit when already was running.
More testing...
It doesn't do that when I run it.
-
Mar 18th, 2007, 03:10 PM
#14
Re: Why does this work only once
Well I simulated a user typing in a document1 without it being saved. Then ran the app and alt+c etc and got the document1 closing and re-adding a new one for pasting.
Looks like I may have fixed the issues. I added a boolean for quitting word, my late bound initializeme sub (modified), and a module level boolean variable.
For me, word remains open when it should, correct document adding, if user closes their instance of word it checks and recreates a new one, etc.
Let me know if it works for you. 
Affected subs/code lines:
vb Code:
Private mbKillMe As Boolean
Public Sub OpenWord()
'Dim hwnd As Long
Dim nRpt As Integer
On Error GoTo ErrorRoutine
StartOver:
InitializeMe
Exit Sub
ErrorRoutine:
Select Case Err.Number
Case -2147023174
' The user has manually shut down Word, so we need to
' clean up and then restart Word.
For nRpt = 1 To wdApp.Documents.Count
wdApp.Documents(nRpt).Close SaveChanges:=wdDoNotSaveChanges
Next nRpt
wdApp.Quit
Set wdApp = Nothing
Resume StartOver
Case 462 'Server not available - why??? 'Fixed but keep error handling just in case
Resume Next
Case Else
DisplayError "OpenWord"
End Select
End Sub
Public Sub InitializeMe()
On Error Resume Next
'<INITIALIZE WORD>
Set wdApp = GetObject(, "Word.Application")
If TypeName(wdApp) <> "Nothing" And TypeName(wdApp) <> "Object" Then
Set wdApp = GetObject(, "Word.Application")
mbKillMe = False
Else
Set wdApp = CreateObject("Word.Application")
mbKillMe = True
End If
End Sub
Public Sub Hotkey_Hit()
Dim oDoc As Object
OpenWord
SendKeys "^c", True
With wdApp
' .Visible = True
Set oDoc = .Documents.Add(, , wdNewBlankDocument, True)
oDoc.Range.PasteAndFormat (wdPasteDefault)
' .Selection.PasteAndFormat (wdPasteDefault)
oDoc.Range.WholeStory
' .Selection.WholeStory
oDoc.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
' .Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
oDoc.Range.Copy
' .Selection.Copy
oDoc.Close SaveChanges:=wdDoNotSaveChanges
Set oDoc = Nothing
If mbKillMe = True Then
.Quit
Set wdApp = Nothing
End If
End With
End Sub
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 
-
Mar 18th, 2007, 03:15 PM
#15
Re: Why does this work only once
If you are using Alt_c then you are probably using the code that I posted in the Utility forum which did behave the way you described and I was able to change that with the Documents.Count code but then I ran across the two-times-with-Word-open problem which caused me to start this thread.
I'll take a look at your code however.
-
Mar 18th, 2007, 03:18 PM
#16
Re: Why does this work only once
Yes, I got your code from the UB download and using Alt+C
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 
-
Mar 18th, 2007, 03:30 PM
#17
Re: Why does this work only once
Any idea how to handle Word error 4605?
-
Mar 18th, 2007, 03:34 PM
#18
Re: Why does this work only once
I forgot to add the constant definitions for when using late bindings...
vb Code:
Const wdNewBlankDocument = 0
Const wdPasteDefault = 0
Const wdNumberParagraph = 1
Const wdDoNotSaveChanges = 0
What is error 4605?
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 
-
Mar 18th, 2007, 03:36 PM
#19
Re: Why does this work only once
 Originally Posted by RobDog888
I forgot to add the constant definitions for when using late bindings...
vb Code:
Const wdNewBlankDocument = 0
Const wdPasteDefault = 0
Const wdNumberParagraph = 1
Const wdDoNotSaveChanges = 0
What is error 4605?
See my post #11.
Why do I need the constants? Aren't they built in?
-
Mar 18th, 2007, 03:40 PM
#20
Re: Why does this work only once
Found it. Are you using Word 2000?
CAUSE
One of these error messages may appear when no documents are currently open, or the document that you are referencing is not open. Word can change only the properties of an open (or visible) document.
http://support.microsoft.com/kb/290934
Also in Word 2002:
http://support.microsoft.com/kb/288424/
Worrd 2003:
http://support.microsoft.com/kb/896987/
Looks like you need Office 2003 Service Pack 2 for the fix on this version.
Edit: Yes they are built in but only defined when using Early Binding. When using Late Binding you need to define all constants.
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 
-
Mar 18th, 2007, 04:09 PM
#21
Re: Why does this work only once
Thanks for every-one's help. There are bloodstains on the wall from me beating my head against it but I was able to fix the problem by doing this.
vb Code:
On Error Resume Next
'<INITIALIZE WORD>
' Set wdApp = GetObject(, "Word.Application")
' If TypeName(wdApp) <> "Nothing" Then
' Set wdApp = GetObject(, "Word.Application")
' Else
Set wdApp = CreateObject("Word.Application")
' End If
In other words by always creating my own temporary word object rather than trying to share any existing one.
-
Mar 18th, 2007, 04:17 PM
#22
Re: Why does this work only once
What version of word were you using it on? It worked fine for me on 2007. 
Dont you get a time delay creating the app object each time?
The other issues are fixed too now, correct?
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 
-
Mar 18th, 2007, 04:23 PM
#23
Re: Why does this work only once
1. 2002
2. Not a noticeable one (perhaps because it's not visible)
3. Yes.
There's one really weird thing left but I'm not going to worry about it. When I posted this
vb Code:
'<INITIALIZE WORD>
' Set wdApp = GetObject(, "Word.Application")
' If TypeName(wdApp) <> "Nothing" Then
' Set wdApp = GetObject(, "Word.Application")
' Else
Set wdApp = CreateObject("Word.Application")
' End If
My actual code didn't have the GetObject line commented out but when I saw what I had posted I said to myself "self, you don't need that because of the CreateObject line that follows" and I made it a comment here. I then went back to the program and changed it there and it no longer worked! Strange. I've changed it back and it works again.
-
Mar 18th, 2007, 04:28 PM
#24
Re: [RESOLVED] Why does this work only once
Oh ya, and you may want to check for the word version and if word is installed on the system too.
Maybe there was a hidden instance of word that was giving the issue?
Glad the other issues are fixed but I still wonder why the app object is so troublesome. I still have a minor issue on my code similar to this too.
Maybe with my updated code I have been working on, I can fix the getobject issue.
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 
-
Mar 19th, 2007, 04:13 AM
#25
Re: [RESOLVED] Why does this work only once
See my signature for an Add-In that does the same thing.
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
|