Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-09-2019, 05:57 AM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default ConmandButton dissappears when Content dispalyed on Page 1


Hi

I just created an ActiveX command button on Page 1 of document to initilize userform
How can this be retained. ? because when clicking the command button of userform the document code overwrites and doest not display the command button as the content overwrites on page 1
any ideas if the document content is seen on Page 2. because on Page 1 i would just like the command button to retain

coding in thisDocument for CommandButton on Page1 of document
Code:
Private Sub cmdInitilizeuf_Click()
     Load userform1
    userform1.Show vbModeless
End Sub
in userform1 Coding
Code:
Private Sub cmdButton1_Click()
Dim objWord As Word.Application
Dim txtword As String
Dim objDoc As Document
Dim objRange As Range
Dim objTable As Table
Dim intRows As Integer
Dim intCols As Integer

    txtWord = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add
    objDoc.Range.Text = txtWord
    Set objRange = objDoc.Range    
    objRange.Collapse Direction:=wdCollapseEnd
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    objTable.Borders.Enable = True
End Sub
Thanks SamD
Reply With Quote
  #2  
Old 08-09-2019, 08:43 PM
gmayor's Avatar
gmayor gmayor is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you are running this button from a Word document, then you must already be in Word, so why create a new instance of Word? However when you create a new document using the code Set objDoc = objWord.Documents.Add this creates a document from the normal template which hopefully will not have your button. You need to create a new document from the template containing the button.

In any case you do not really need a button at all. You could use an autonew macro in the template you are creating to call the userform and that template could already have the table which is fixed in your code, so there is no need to build it.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-10-2019, 08:14 PM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Hi
the atmosphere is completely new in Word Classrom then Excel Classrom. I may take time in absorbing new syntaxes for VBA word. Please excuse me.

i tried the following instead of Set objDoc = objWord.Documents.Add
Set objDoc = Documents.Open("C:\WordTrial\cmdBtnWord.docm")

Then the above also led to overwriting.

I was trying for a possibility just like in Excel usually My first worksheet will have command button to initialize the userform. So in same manner was trying

Quote:
You need to create a new document from the template containing the button.
How is the above possible and if you can kindy help with positioning of Command button on Page 1 of Created New Document .because when created
ActiveX command button on Page 1 of document. i tried moving and placing. it did not move Just like in Excel you can paste it anywhere on the worksheet

Quote:
You could use an autonew macro in the template you are creating to call the userform and that template could already have the table which is fixed in your code, so there is no need to build it.
I checked in Developer Ribbon as there was no autonew macro. If yes then where can i search and get the same on ribbon
if no then what to code in AutoNew Macro or is what i've coded and to impletement the same in Autonew Macro
Also are you meaning the same to code in Document_Open() ?


Thanks
SamD
Reply With Quote
  #4  
Old 08-11-2019, 12:19 AM
gmayor's Avatar
gmayor gmayor is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There are some similarities between Word and Excel, but they are not the same when it comes to programming. If you Add a document without reference to the template, which is what you had originally, then a new document is created from the normal template. Essentially a blank document unrelated to your document with the button. If you want to create a new document that has the button then you need to refer to the template e.g.

Set objDoc = Documents.Add(Template:="C:\WordTrial\cmdBtnWord.d ocm")

This will create a new document that is an unnamed facsimile of the original. Or as it is a macro enabled document, you can simply open it as in your subsequent attempt.

Having got the document open/created your code then writes a text value to the document range, which overwrites everything in the document body. You need instead to set a range to the start (or end) of the document and write to that e.g.

Code:
Dim txtword As String
Dim objDoc As Document
Dim objRange As Range
Dim objTable As Table
Dim intRows As Integer
Dim intCols As Integer
    txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr
    Set objDoc = Documents.Add(Template:=ThisDocument.FullName)
    Set objRange = objDoc.Range
    objRange.Collapse Direction:=wdCollapseStart
    objRange.Text = txtword
    objRange.Collapse Direction:=wdCollapseEnd
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    objTable.Borders.Enable = True
AutoNew and AutoOpen are built-in macro names that run when the document is opened or created and go in a standard module. They perform similar roles to Document_New and Document_Open

You would need to modify the ribbon in order to add a button there. See Customize the Office Ribbon (It doesn't take rocket science)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com

Last edited by gmayor; 08-12-2019 at 12:12 AM.
Reply With Quote
  #5  
Old 08-11-2019, 09:24 PM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Hello

I tried as per your guidance but what happens when

Quote:
Set objDoc = Documents.Add(Template:="C:\WordTrial\cmdBtnWord.d ocm")
Quote:
This will create a new document that is an unnamed facsimile of the original. Or as it is a macro enabled document, you can simply open it as in your subsequent attempt.
It opens totally a new blank file. Where as i wanted the content to display on Page 2 because on Page 1 already command button is there to click and get the content on Page 2 of the .docm file

i tried the above without the below comments in Marked RED but added with syntax marked in Blue
Code:
Dim txtword As String
Dim objDoc As Document
Dim objRange As Range
Dim objTable As Table
Dim intRows As Integer
Dim intCols As Integer
    txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr
    Set objDoc = Documents.Add(Template:="C:\WordTrial\cmdBtnWord.docm")
With objDoc
             .Range.Text = txtword
             .Range.Font.Name = "Tahoma"
             .Range.Font.Size = "12"
             .Paragraphs.SpaceAfter = 0 
        End With 

    Set objRange = objDoc.Range
    objRange.Collapse Direction:=wdCollapseStart
    objRange.Text = txtword
    
   objRange.Collapse Direction:=wdCollapseEnd
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    objTable.Borders.Enable = True
Then i tried with red marked syntaxes and completely without Blue Marked Syntaxes then i got the error Method or Data member not found at the following line
Quote:
objRange.Range.Text = txtword
Reply With Quote
  #6  
Old 08-12-2019, 12:32 AM
gmayor's Avatar
gmayor gmayor is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

As I explained, if you write to the document range you replace anything in the document range, which is what your blue text does and results in an empty one page document.

objrange is a range. It is set just above the red section, so objRange.Range.Text = txtword is incorrect. It should be objRange.Text = txtword which is what the code shows.

I assume there is nothing on page 2, so you can set the range to the document then collapse the range to its end, rather than the start, and that will write to the empty page 2 which is at the end of the document.

The macro below does not need any alteration to run from CommandButton1

Code:
Private Sub CommandButton1_Click()
Dim txtword As String
Dim objDoc As Document
Dim objRange As Range
Dim objTable As Table
Dim intRows As Integer
Dim intCols As Integer

    intRows = 8: intCols = 5
    txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr
    Set objDoc = Documents.Add(Template:=ThisDocument.FullName)
    Set objRange = objDoc.Range    'set the range to the document
    With objRange
        .Collapse Direction:=wdCollapseEnd    'collapse the range
        .Text = txtword    'write to the range
        .Font.Name = "Tahoma"    'format the range
        .Font.Size = "12"
        .ParagraphFormat.SpaceAfter = 0
        .Collapse Direction:=wdCollapseEnd    'collapse the range
    End With
    'add a table
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    'Format the table here
    objTable.Borders.Enable = True
    objTable.Select    'select the table
    Selection.Collapse Direction:=wdCollapseEnd    'collapse the selection
    'The document is displayed with the focus at the selection
    'Clean up
    Set objDoc = Nothing
    Set objRange = Nothing
    Set objTable = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 08-12-2019, 05:02 AM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Tried with both wdCollapseEnd and wdCollapseStart ' RED and Blue Respectively
Code:
With objRange
        .Collapse Direction:=wdCollapseEnd    'collapse the range
        .Text = txtword    'write to the range
        .Font.Name = "Tahoma"    'format the range
        .Font.Size = "12"
        .ParagraphFormat.SpaceAfter = 0
        .Collapse Direction:=wdCollapseEnd    'collapse the range
    End With
As coded by you With above, the range started immediately after CommandButton but completely New As Document1

but when i changed as below, the range was seen in completely New As Document1
Code:
With objRange
        .Collapse Direction:=wdCollapseStart    'starts the Range in NewPage
        .Text = txtword    'write to the range
        .Font.Name = "Tahoma"    'format the range
        .Font.Size = "12"
        .ParagraphFormat.SpaceAfter = 0
        .Collapse Direction:=wdCollapseEnd    'collapse the range
    End With
Any Possibility to add page 2 and to start the Range in Page 2 of the current document "C:\WordTrial\cmdBtnWord.docm"

Also with above coding trials. Observed one thing whenever Document1 or new document Opens with that one more document opens but without a Page and item in Ribbons are disabled. Could you explain why this happens ?
Reply With Quote
  #8  
Old 08-12-2019, 06:27 AM
gmayor's Avatar
gmayor gmayor is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code doesn't create a second page. It writes to a second page that you implied existed in your document. If you want a second page you need to add it then reset the range e.g. as attached.
Attached Files
File Type: docm Example.docm (22.7 KB, 8 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 08-13-2019, 07:17 AM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Thank you so much for your attached code. I've tried the same It worked perfectly as you mentioned
Quote:
The code doesn't create a second page. It writes to a second page that you implied existed in your document. If you want a second page you need to add it then reset the range e.g. as attached.
Quote:
This will create a new document that is an unnamed facsimile of the original. Or as it is a macro enabled document, you can simply open it as in your subsequent attempt.
There should be some way to work on the exisiting Document rather than adding New document. just like in excel you can work on Exsiitng Macro Enabled Workbooks, its worksheets rather than adding new workbook and new worksheet
Reply With Quote
  #10  
Old 08-13-2019, 08:54 PM
gmayor's Avatar
gmayor gmayor is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can work on the original document if you open it rather than add it, but you can only work on it once as the code alters the document. So if you do that retain an unaltered original.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #11  
Old 08-13-2019, 10:55 PM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

The .docm file is always opened as Original Document.
Quote:
but you can only work on it once as the code alters the document.
This too was observed as when code changes for some desired output. new document with number seems to be added as Facsmille of Original with the desired output. but then this keeps on going unless i close Each the numbered document without saving it
So how to retain the original opened .docm with a code to add the page.
Reply With Quote
  #12  
Old 08-14-2019, 04:17 AM
gmayor's Avatar
gmayor gmayor is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You have lost me now.

You can either use the original document as a template and create a new document from it, or you can open the original document and process that.

If you change the original you will have to save it or the changes will be lost.

Much depends on what the aim of the exercise is, because so far I have seen no reason at all for the macro or the button as you could simply save the table in the document to start off with, which makes more sense than creating the table using a macro.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #13  
Old 08-19-2019, 09:54 PM
SamDsouza SamDsouza is offline ConmandButton dissappears when Content dispalyed on Page 1 Windows 10 ConmandButton dissappears when Content dispalyed on Page 1 Office 2013
Advanced Beginner
ConmandButton dissappears when Content dispalyed on Page 1
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Thanks Gmayor
Sorry for very Late reply.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
using styles to generate content page TYPO Word 2 12-20-2015 12:48 PM
ConmandButton dissappears when Content dispalyed on Page 1 Insert the page before table of content without altering it kbhalakiya Word 2 08-29-2012 03:53 AM
ConmandButton dissappears when Content dispalyed on Page 1 Rotate Page and Content Within It. walker140 Word 1 11-13-2011 11:08 PM
ConmandButton dissappears when Content dispalyed on Page 1 Page number in table of Content Ritu Office 1 09-19-2011 05:10 AM
ConmandButton dissappears when Content dispalyed on Page 1 Paragraph Disappears when Tabbing it over GWalkaa Word 11 07-22-2011 03:19 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:14 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft