Results 1 to 6 of 6
  1. #1
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts

    Problems with macro that copies terms to another document

    Hi, folks,

    I've been using this macro to make my editing conventions list since 2004 (I got it fro the Editorium.)
    Code:
    Sub GetThat2()
         Selection.Copy
         WordBasic.NextWindow
         Selection.PasteAndFormat (wdPasteDefault)
         Selection.TypeParagraph
         WordBasic.NextWindow
         Selection.MoveRight Unit:=wdCharacter, Count:=1
    
    End Sub
    Today, my client did not allow me to load my macros onto its computer. So I recorded a macro like this:
    Sub GetTerm2()
    '
    ' GetTerm2 Macro
    '
    '
    Application.Run MacroName:="Editing.WordMacros.EditCopy"
    Windows("conventions doc").Activate
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.TypeParagraph
    Windows("Main doc").Activate
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    End Sub
    I don't want the file names in the code because the macros needs to work on many different files--like the old one does. I tried replacing the Windows....Activate lines with "ActiveWindow.Next.Activate" and "ActiveWindow.Previous.Activate" (which I found on a VBA code snippets site), but the resulting macro doesn't work. I haven't put"WordBasic.NextWindow" in there because I know from experience that Word Basic commands sometimes cause Word to hang and it's pretty clear that MS is moving away from them. Can someone tell me how to fix the code, tell me how to keep the macro recorder form putting in file names, or take pity on me and give me working code?

    Cheers
    Pam Caswell

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    This works for me:
    Code:
    Sub Demo()
    With Application
      .ScreenUpdating = False
      Selection.Copy
      With .ActiveWindow.Next.Selection
        .PasteAndFormat (wdPasteDefault)
        .TypeParagraph
      End With
      .ScreenUpdating = True
    End With
    End Sub
    I'm not sure what the issue is re "my client did not allow me to load my macros onto its computer", since the end result is no different than typing the same macro from scratch. Perhaps they'd be less reticent if you stored your macros in plain text files and simply copied the ones you needed from there?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts
    Thanks, Paul. The client won't allow anyone to insert a thumb drive or other device. Their security software doesn't even like it when someone charges their phone via a USB port. They are my only client that is so strict. A few insist on running antivirus software on the thumb drive, but most give me permission to install the add-ons—with my promise to remove them when the job is done.

    The really interesting thing is how different yesterday's recorded macro is from the one recorded in 2004. I recall re-recording several macros in 2007 using W2007. They were the same as those recorded using W2003 (and used Word basic commands).

    Just by reading, I see that your macro does the first part of what my recorded macro does. The second part—to return to the first document with the cursor position just past the previously selected text. I'll try to add that later today.

    Thanks very much,
    Pam
    Pam Caswell

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub Demo()
    With Application
      .ScreenUpdating = False
      Selection.Copy
      With .ActiveWindow.Next.Selection
        .PasteAndFormat (wdPasteDefault)
        .TypeParagraph
      End With
      Selection.Collapse wdCollapseEnd
      .ScreenUpdating = True
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts
    Hi, Paul,

    With your code and with the code I tried to modify, I'm getting an "Object variable or block variable not set error" message.

    Pam
    Pam Caswell

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    The only time I get an error is if nothing is selected. Using 'On Error Resume Next' before 'Selection.Copy' would take care of that, though you might then end up with whatever else is in the clipboard being pasted. Better would be:
    Code:
    Sub Demo()
    With Application
      .ScreenUpdating = False
      On Error GoTo ErrExit
      Selection.Copy
      With .ActiveWindow.Next.Selection
        .PasteAndFormat (wdPasteDefault)
        .TypeParagraph
      End With
      Selection.Collapse wdCollapseEnd
    ErrExit:
      .ScreenUpdating = True
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •