Results 1 to 8 of 8

Thread: Copy a text from a Word Document to a string? [Resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Question Copy a text from a Word Document to a string? [Resolved]

    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?
    Last edited by Pirre001; Nov 3rd, 2003 at 12:51 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Help....

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    it should be a simple modification, show us the code you have got at the moment...

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    I got this:
    VB Code:
    1. Private Sub WordCopyPaste()
    2.  
    3. Dim wo As word.Application
    4. Dim strt As String
    5.  
    6.     On Error Resume Next
    7.     Set wo = GetObject(, "Word.Application")
    8.     If Err.Number <> 0 Then
    9.         ' if word isn't open, then open it
    10.         Set wo = CreateObject("Word.Application")
    11.     End If
    12.     Err.Clear
    13.     'On Error GoTo WordError
    14.    
    15.     wo.Visible = False
    16.    
    17.     wo.Documents.Open ("C:\TestFile.doc")
    18.    
    19.     If wo.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    20.         wo.ActiveWindow.Panes(2).Close
    21.     End If
    22.     If wo.ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
    23.        wo.ActiveWindow.ActivePane.View.Type = wdPrintView
    24.     End If
    25.     wo.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    26.     wo.ActiveWindow.Panes(2).Activate
    27.     wo.Selection.MoveRight Unit:=wdCell
    28.     wo.Selection.MoveRight Unit:=wdCell
    29.     wo.Selection.MoveRight Unit:=wdCell
    30.     wo.Selection.MoveRight Unit:=wdCell
    31.     wo.Selection.MoveRight Unit:=wdCell
    32.     wo.Selection.MoveRight Unit:=wdCell
    33.     wo.Selection.MoveRight Unit:=wdCell
    34.     wo.Selection.MoveRight Unit:=wdCell
    35.     wo.Selection.MoveRight Unit:=wdCell
    36.     wo.Selection.MoveRight Unit:=wdCell
    37.     wo.Selection.Copy
    38.     wo.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    39.  
    40.     'Here I want to paste the copy into a string, like this:
    41.     'strt = wo.Selection.PasteAndFormat(wdPasteDefault)
    42.     'But it doesent work.
    43.  
    44.     wo.ActiveDocument.Close
    45.     wo.Application.Quit
    46.  
    47.  
    48.     Set wo = Nothing
    49.  
    50. End Sub

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    is there a particular reason for using the clipboard?

    if not, this will do the trick:
    VB Code:
    1. '....
    2.     wo.Selection.MoveRight Unit:=wdCell
    3.     strt = wo.Selection.Text
    4.     wo.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    5.  
    6.     wo.ActiveDocument.Close
    7. '...

    NB: wo.Selection.Text might not work, you may need .Value or some other property.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    [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]

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    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.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    It works now!

    I missed to remove this row:

    wo.Selection.Copy

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