Word macro: Copy specific textbox from specific document and paste

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Word macro: Copy specific textbox from specific document and paste

Post by yanlok1345 »

Hi everyone,

I currently developed a word macro to:

1) Find textbox in specific document named "ABC"
2) Copy that textbox
3) Paste it near the cursor

Here's the code:

Code: Select all

Sub CopyTextbox()

Application.ScreenUpdating = False

    ' Open the source document
    Dim SourceDoc As Document
    Set SourceDoc = Documents.Open("D:\TextBox.docx")

    ' Find the textbox named "ABC"
    Dim shp As Shape
    For Each shp In SourceDoc.Shapes
        If shp.Name = "ABC" Then
            ' Copy the textbox
            shp.Select
            
            
            Selection.Copy
            Exit For
        End If
    Next shp

    ' Close the source document without saving changes
    SourceDoc.Close SaveChanges:=wdDoNotSaveChanges

    ' Paste the textbox at the cursor position in the active document
    Selection.Paste

    ' Get the pasted shape
    Dim pastedShp As Shape
    Set pastedShp = ActiveDocument.Shapes(ActiveDocument.Shapes.Count)

    ' Make the pasted textbox transparent
    pastedShp.Fill.Visible = msoFalse
    pastedShp.Line.Visible = msoFalse

Application.ScreenUpdating = False

End Sub
However, I noticed that I need to edit the active document every time before I run a certain macro, otherwise it doesn't work. Could you kindly advise me on a solution to this problem, so that I can use the macro without any issues? Thank you so much for your kind assistance!

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word macro: Copy specific textbox from specific document and paste

Post by HansV »

What exactly do you mean by "edit the active document"?
Best wishes,
Hans

snb
4StarLounger
Posts: 583
Joined: 14 Nov 2012, 16:06

Re: Word macro: Copy specific textbox from specific document and paste

Post by snb »

Why not creating directly ?

Code: Select all

Sub M_snb()
   ActiveDocument.Shapes.AddOLEObject("Forms.checkbox.1", Anchor:=Selection.Range).ConvertToInlineShape
End Sub

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Word macro: Copy specific textbox from specific document and paste

Post by yanlok1345 »

HansV wrote:
24 Jan 2024, 08:10
What exactly do you mean by "edit the active document"?
For instance, simply press the "Enter" key, delete, copy, or paste something. If no actions have been performed prior to executing this macro, there is a 50% to 100% chance that it will not be executable.

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Word macro: Copy specific textbox from specific document and paste

Post by yanlok1345 »

snb wrote:
24 Jan 2024, 15:28
Why not creating directly ?

Code: Select all

Sub M_snb()
   ActiveDocument.Shapes.AddOLEObject("Forms.checkbox.1", Anchor:=Selection.Range).ConvertToInlineShape
End Sub
Considering the presence of a table with various formatting within the target textbox, the most effective approach would be to utilize the copy and paste method.

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word macro: Copy specific textbox from specific document and paste

Post by HansV »

I'm sorry, I have no idea what would cause that, nor how to solve it.
Best wishes,
Hans

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Word macro: Copy specific textbox from specific document and paste

Post by yanlok1345 »

HansV wrote:
25 Jan 2024, 11:36
I'm sorry, I have no idea what would cause that, nor how to solve it.
No worries, I'll make an attempt to solve it on my own. Many thanks for your help.

snb
4StarLounger
Posts: 583
Joined: 14 Nov 2012, 16:06

Re: Word macro: Copy specific textbox from specific document and paste

Post by snb »

the presence of a table with various formatting within the target textbox
triggers my curiosity: please post an example.

User avatar
Charles Kenyon
5StarLounger
Posts: 621
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Word macro: Copy specific textbox from specific document and paste

Post by Charles Kenyon »