How can I copy a text from a word document into a string?
I have tried, but I can only copy not paste the text into a string?
I want to copy a text from the header in the word dokument. The text I'm coping is from the 11:th cell. How can I do that?
Printable View
How can I copy a text from a word document into a string?
I have tried, but I can only copy not paste the text into a string?
I want to copy a text from the header in the word dokument. The text I'm coping is from the 11:th cell. How can I do that?
Help....
it should be a simple modification, show us the code you have got at the moment...
I got this:
VB Code:
Private Sub WordCopyPaste() Dim wo As word.Application Dim strt As String On Error Resume Next Set wo = GetObject(, "Word.Application") If Err.Number <> 0 Then ' if word isn't open, then open it Set wo = CreateObject("Word.Application") End If Err.Clear 'On Error GoTo WordError wo.Visible = False wo.Documents.Open ("C:\TestFile.doc") If wo.ActiveWindow.View.SplitSpecial <> wdPaneNone Then wo.ActiveWindow.Panes(2).Close End If If wo.ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then wo.ActiveWindow.ActivePane.View.Type = wdPrintView End If wo.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader wo.ActiveWindow.Panes(2).Activate wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.MoveRight Unit:=wdCell wo.Selection.Copy wo.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Here I want to paste the copy into a string, like this: 'strt = wo.Selection.PasteAndFormat(wdPasteDefault) 'But it doesent work. wo.ActiveDocument.Close wo.Application.Quit Set wo = Nothing End Sub
is there a particular reason for using the clipboard?
if not, this will do the trick:
VB Code:
'.... wo.Selection.MoveRight Unit:=wdCell strt = wo.Selection.Text wo.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument wo.ActiveDocument.Close '...
NB: wo.Selection.Text might not work, you may need .Value or some other property.
[list=1][*]Manually open your document in Word[*]Start the recording of a macro[*]Manually do what you want your program to do[*]Stop the macro recording[*]Edit the macro and copy the pertinant code to your program.[*]Add wo. object references to Word's code[/list=1]
si_the_geek,
I tried your example, but it doesen't work...? The string is empty?
MartinLiss,
The code I posted here is a macro I recorded. But the last bit to paste the text, which is copied, into a string doesen't work.
It works now!
I missed to remove this row:
wo.Selection.Copy