如何交叉引用宏生成的书签

时间:2017-10-23 07:36:20

标签: vba word-vba

我有一个单词输入表单,当用户根据模板创建新文档时会弹出该表单。用户填写所需信息,然后通过书签将此信息正确放置在模板中所需的位置。下面的代码收集并填充所需的信息。然后,我使用插入选项卡下的交叉引用选项在模板的不同位置交叉引用这些书签。但是,交叉引用字段不会更新以匹配提供的信息。

以下是我用来从表单中收集信息并将其填充到书签中的代码:

Private Sub OK_Click()

    Dim UnitName As Range
    Set UnitName = ActiveDocument.Bookmarks("UnitName").Range
    UnitName.Text = Me.AgisanangUnitNameInput.Value

    Dim OrderNo As Range
    Set OrderNo = ActiveDocument.Bookmarks("OrderNo").Range
    OrderNo.Text = Me.OrderNoInput.Value

    Dim ItemNo As Range
    Set ItemNo = ActiveDocument.Bookmarks("ItemNo").Range
    ItemNo.Text = Me.ItemNoInput.Value

    Dim Reference As Range
    Set Reference = ActiveDocument.Bookmarks("Reference").Range
    Reference.Text = Me.ReferenceInput.Value

    Dim DocumentNo As Range
    Set DocumentNo = ActiveDocument.Bookmarks("DocumentNo").Range
    DocumentNo.Text = Me.DocumentNoInput.Value

    Dim RevisionNo As Range
    Set RevisionNo = ActiveDocument.Bookmarks("RevisionNo").Range
    RevisionNo.Text = Me.RevisionNoInput.Value

    Dim ProjectName As Range
    Set ProjectName = ActiveDocument.Bookmarks("ProjectName").Range
    ProjectName.Text = Me.ProjectNameInput.Value

    Dim PreparedFor As Range
    Set PreparedFor = ActiveDocument.Bookmarks("PreparedFor").Range
    PreparedFor.Text = Me.PreparedForInput.Value

    Dim Classification As Range
    Set Classification = ActiveDocument.Bookmarks("Classification").Range
    Classification.Text = Me.ClassificationInput.Value

    Dim DocumentType As Range
    Set DocumentType = ActiveDocument.Bookmarks("DocumentType").Range
    DocumentType.Text = Me.DocumentTypeInput.Value

    Dim TitleOfReport As Range
    Set TitleOfReport = ActiveDocument.Bookmarks("TitleOfReport").Range
    TitleOfReport.Text = Me.TitleOfReportInput.Value

    Me.Repaint
    ReportInputForm.Hide

End Sub

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。

Dim Rng As Range

For Each Rng In ActiveDocument.StoryRanges
    With Rng
        If .Fields.Count Then .Fields.Update
    End With
Next Rng

您可以通过排除一些StoryRanges(例如页眉和页脚)和/或仅更新选定的类型甚至仅更新单个字段来限制此原则。

BTW,一种更传统的编码格式将所有Dim语句放在代码的顶部,就像正在处理的内容的概述一样。如果您随后为块中的对象赋值,则会为此目的使用循环打开门。在这样做的过程中,你最终会在第三个块中得到声明,所有声明都是你当前安排的精确转换。

答案 1 :(得分:0)

我认为问题在于,当您添加文本时,您无意中删除了书签 - 因此错误。您可以通过单步执行代码(F8)并在将文本分配到书签范围之前和之后计算书签数来检查这一点。

通过一种模式来保护'书签,你可以这样做:

@Override
    public void onBindViewHolder(PictureViewHolder holder, final int position) {
        PictureListPizza pictureListPizza = pictureListPizzas.get(position);
        holder.product_name.setText(pictureListPizza.getProduct_name());
        holder.product_ingredients.setText(pictureListPizza.getProduct_ingredients());
        holder.product_price.setText(pictureListPizza.getProduct_price());
        Picasso.with(activity).load(pictureListPizza.getPicture()).into(holder.product_photo);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (PizzaActivity.class != null){
                    if(position == 0){
                        Intent intent = new Intent(activity, Pizza1Activity.class);
                        activity.startActivity(intent);
                    }
                    if(position == 1){
                        Intent intent = new Intent(activity, Pizza2Activity.class);
                        activity.startActivity(intent);
                    }
                    if(position == 2){
                        Intent intent = new Intent(activity, Pizza3Activity.class);
                        activity.startActivity(intent);
                    }
                }
            }
        });

    }